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;
Code
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); // converts an ip address string to an instance of the IPAddress type.
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 listening
Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp); // create a socket image. If udp is used, use a Socket of the SocketType. Dgram type.
S. Bind (ipe); // Bind the EndPoint to the image (port 2000 and IP address)
S. Listen (0); // start listening
Console. WriteLine ("waiting for client connection ");
/** // Accept the client connection, create a new socket for the connection, and accept the information
Socket temp = s. Accept (); // create a new socket for the new connection
Console. WriteLine ("establish a connection ");
String recvStr = "";
Byte [] recvBytes = new byte [1, 1024];
Int bytes;
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 information sent from the client
String sendStr = "OK! Client send message successful! ";
Byte [] bs = Encoding. ASCII. GetBytes (sendStr );
Temp. Send (bs, bs. Length, 0); // return information to the client
Temp. Close ();
S. Close ();
Console. ReadLine ();
}
}
}
Server running result:
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;
Code: 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 an EndPoint
IPAddress ip = IPAddress. Parse (host );
// IPAddress ipp = new IPAddress ("127.0.0.1 ");
IPEndPoint ipe = new IPEndPoint (ip, port); // converts the ip address and port to an IPEndpoint instance.
/** // Create a socket and connect 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 a message to the server
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 information
/** // Accept the information returned from the server
String recvStr = "";
Byte [] recvBytes = new byte [1, 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 the socket after it is used up.
C. Close ();
}
Catch (ArgumentNullException e)
{
Console. WriteLine ("argumentNullException: {0}", e );
}
Catch (SocketException e)
{
Console. WriteLine ("SocketException: {0}", e );
}
Console. WriteLine ("Press Enter to Exit ");
}
}
}
Client result: