C # Write a basic Socket communication

Source: Internet
Author: User

I want to write something in the evening. I want to write down the Sockt communication I just learned from the company. It's easy to understand. New people can use the console to learn from it. First, let's talk about the relationship between Socket and TCP/UDP. I 've been talking about Socket communication and TCP communication, and I didn't understand what it meant at first, reference the concept most people on the internet. "The Socket interface is a TCP/IP network API. The Socket interface defines many functions or routines, programmers can use them to develop applications on TCP/IP networks. To learn TCP/IP network programming on the Internet, you must understand the Socket interface ." I understand that SOCKET is the implementation method of TCP and UDP, and SOCKET programming can realize TCP and UDP communication. Next, let's look at Socket as a pipe. Different protocols are used to transmit liquids or solids in the pipe. Okay. Let's look at the Code in the following topic. The code is not long and should be easy to understand. Server: static void Main (string [] args) {int recv; // used to indicate the length of information sent by the client. byte [] data = new byte [1024]; // used to cache the information sent by the client. The information transmitted through socket must be a byte array IPEndPoint ipep = new IPEndPoint (IPAddress. any, 9050); // the IP address and port Socket newsock = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); newsock. bind (ipep); // Bind newsock. listen (10); // listens to the Console. writeLine ("waiting for a client"); Socket client = Newsock. accept (); // executed when an available client connection attempt is made and a new socket is returned for communication with the client IPEndPoint clientip = (IPEndPoint) client. remoteEndPoint; Console. writeLine ("connect with client:" + clientip. address + "at port:" + clientip. port); string welcome = "welcome here! "; Data = Encoding. ASCII. getBytes (welcome); client. send (data, data. length, SocketFlags. none); // send information try {while (true) {// use an endless loop to continuously obtain information from the client data = new byte [1024]; recv = client. receive (data); Console. writeLine ("recv =" + recv); if (recv = 0) // when the information length is 0, the client is disconnected from break; Console. writeLine (Encoding. ASCII. getString (data, 0, recv); client. send (data, recv, SocketFlags. none); // send message} catch {Console. write Line ("Disconnected from" + clientip. address);} client. close (); newsock. close ();} client: static void Main (string [] args) {byte [] data = new byte [1024]; Socket newclient = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); Console. write ("please input the server ip:"); string ipadd = Console. readLine (); Console. writeLine (); Console. write ("please input the server port:"); in T port = Convert. toInt32 (Console. readLine (); IPEndPoint ie = new IPEndPoint (IPAddress. parse (ipadd), port); // server IP address and port try {// because the client is only used to send information to a specific server, you do not need to bind the local IP address and port. You do not need to listen. Newclient. connect (ie);} catch (SocketException e) {Console. writeLine ("unable to connect to server"); Console. writeLine (e. toString (); return;} newclient. send (Encoding. default. getBytes ("watchdog"); int recv = newclient. receive (data); string stringdata = Encoding. ASCII. getString (data, 0, recv); Console. writeLine (stringdata); while (true) {string input = Console. readLine (); if (input = "exit ") Break; newclient. send (Encoding. ASCII. getBytes (input); data = new byte [1024]; recv = newclient. receive (data); stringdata = Encoding. ASCII. getString (data, 0, recv); Console. writeLine (stringdata);} Console. writeLine ("disconnect from sercer"); newclient. shutdown (SocketShutdown. both); newclient. close ();} the client runtime diagram first runs the server and then the client. This program is easy to learn. You can also create a simple chat room.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.