The sample program is a synchronous socket program. The function is very simple, but the client sends a message to the server, and the server returns a message to the client. Here is just a simple example, which is the most basic socket programming process, in the following article, the synchronization and Asynchronization of sockets and their differences will be recorded in sequence.
The following describes the simple steps of the sample program.
Server:
Step 1: Create an EndPoint image with the specified port number and the server ip address;
Step 2: Create a Socket image;
Step 3: Bind an EndPoint using the Bind () method of the socket image;
Step 4: Use the socket to start listening to the Listen () method of the image;
Step 5: Accept the connection to the client, and use the socket to create a new socket to communicate with the requested client using the Accept () method of the image;
Step 6: Close the socket after the communication ends;
Using System; using System. collections. generic; using System. text; using System. net; using System. net. sockets; namespace server {class Program {static void Main (string [] args) {int port = 2000; string host = "127.0.0.1 "; /** // create an EndPoint) IPAddress ip = IPAddress. parse (host); // convert the ip address string to IPAddress type instance IPEndPoint ipe = new IPEndPoint (ip, port ); // use the specified port and ip address to initialize a new instance of the IPEndPoint class/*** // create a socket and start Listen to Socket s = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // create a socket image. If udp is used, use SocketType. dgram-type socket s. bind (ipe); // Bind the EndPoint to port 2000 and IP address s. listen (0); // starts listening to the Console. writeLine ("waiting for client connection");/*** // accept the client connection, create a new socket for this connection, and accept the information Socket temp = s. accept (); // create a new socket Console for the new connection. writeLine ("establish connection"); string recvStr = ""; byte [] recvBytes = new byte [1024]; int Tes; bytes = temp. receive (recvBytes, recvBytes. length, 0); // receive information from the client recvStr + = Encoding. ASCII. getString (recvBytes, 0, bytes);/** // return information to the client Console. writeLine ("server get message: {0}", recvStr); // display the client information string sendStr = "OK! Client send message successful! "; Byte [] bs = Encoding. ASCII. getBytes (sendStr); temp. send (bs, bs. length, 0); // return the information to the client temp. close (); s. close (); Console. readLine ();}}}
Client:
Step 1: Create an EndPoint image with the specified port number and the server ip address;
Step 2: Create a Socket image;
Step 3: Use the socket Connect () method of the image to establish an EndPoint object as a parameter and send a connection request to the server;
Step 4: If the connection is successful, use the Send () method of the socket to Send information to the server;
Step 5: Use the socket-to-image Receive () method to Receive information sent from the server;
Step 6: Close the socket after the communication ends;
Using System; using System. collections. generic; using System. text; using System. net; using System. net. sockets; namespace Client {class Program {static void Main (string [] args) {try {int port = 2000; string host = "127.0.0.1 "; /** // create the EndPoint IPAddress ip = IPAddress. parse (host); // IPAddress ipp = new IPAddress ("127.0.0.1"); IPEndPoint ipe = new IPEndPoint (ip, port); // convert ip and port to IPEndpoint instance /** // Create a socket and connect it to the server Socket c = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // create a Socket Console. writeLine ("Conneting... "); C. Connect (ipe); // Connect to the server/*** // send the message string sendStr =" hello! This is a socket test "; byte [] bs = Encoding. ASCII. getBytes (sendStr); // encode the string as a byte Console. writeLine ("Send Message"); c. send (bs, bs. length, 0); // send message/** // accept the message returned from the Server string recvStr = ""; byte [] recvBytes = new byte [1024]; int bytes; bytes = c. receive (recvBytes, recvBytes. length, 0); // The slave server accepts the returned information recvStr + = Encoding. ASCII. getString (recvBytes, 0, bytes); Console. writeLine ("client get message: {0}", recvStr); // display the Server Response Information/** // remember to close c after socket is used up. close ();} catch (ArgumentNullException e) {Console. writeLine ("argumentNullException: {0}", e) ;}catch (SocketException e) {Console. writeLine ("SocketException: {0}", e);} Console. writeLine ("Press Enter to Exit ");}}}
This article is from the "Better_Power_Wisdom" blog, please be sure to keep this source http://aonaufly.blog.51cto.com/3554853/1300069