1. Open VS to create two console applications: leleapplication_socketserver and ConsoleApplication_socketClient.
2. Enter the following code in leleapplication_socketclient:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. Net. Sockets;
Namespace ConsoleApplication_socketClient
{
Class Program
{
Static Socket clientSocket;
Static void Main (string [] args)
{
// Indicates the network endpoint as an IP address and port used for socket listening.
IPEndPoint ipep = new IPEndPoint (IPAddress. parse ("*. *. *. * "), 3001); // enter the IP address of your computer or the IP address of another computer. If it is an IP address of another computer, place the leleapplication_socketserver project on the corresponding computer.
ClientSocket = new Socket (ipep. AddressFamily, SocketType. Stream, ProtocolType. Tcp );
// Connect the Socket to the server
Try
{
ClientSocket. Connect (ipep );
String outBufferStr;
Byte [] outBuffer = new Byte [1024];
Byte [] inBuffer = new Byte [1024];
While (true)
{
// Send a message
OutBufferStr = Console. ReadLine ();
OutBuffer = Encoding. ASCII. GetBytes (outBufferStr );
ClientSocket. Send (outBuffer, outBuffer. Length, SocketFlags. None );
// Receive Server Information
ClientSocket. Receive (inBuffer, 1024, SocketFlags. None); // If the received message is empty, the current loop is blocked.
Console. WriteLine ("the server says :");
Console. WriteLine (Encoding. ASCII. GetString (inBuffer ));
}
}
Catch
{
Console. WriteLine ("the service is not enabled! ");
Console. ReadLine ();
}
}
}
}
3. Enter the following code in leleapplication_socketserver:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Using System. Net;
Using System. Net. Sockets;
Using System. Threading;
Namespace ConsoleApplication_socketServer
{
Class Program
{
Static Socket serverSocket;
Static Socket clientSocket;
Static Thread thread;
Static void Main (string [] args)
{
IPEndPoint ipep = new IPEndPoint (ipaddresses. Any, 3001 );
ServerSocket = new Socket (ipep. AddressFamily, SocketType. Stream, ProtocolType. Tcp );
ServerSocket. Bind (ipep );
ServerSocket. Listen (10 );
While (true)
{
ClientSocket = serverSocket. Accept ();
Thread = new Thread (new ThreadStart (doWork ));
Thread. Start ();
}
}
Private static void doWork ()
{
Socket s = clientSocket; // client information
IPEndPoint ipEndPoint = (IPEndPoint) s. RemoteEndPoint;
String address = ipEndPoint. Address. ToString ();
String port = ipEndPoint. Port. ToString ();
Console. WriteLine (address + ":" + port + "connected ");
Byte [] inBuffer = new Byte [1024];
Byte [] outBuffer = new Byte [1024];
String inBufferStr;
String outBufferStr;
Try
{
While (true)
{
S. Receive (inBuffer, 1024, SocketFlags. None); // If the received message is empty, the current loop is blocked.
InBufferStr = Encoding. ASCII. GetString (inBuffer );
Console. WriteLine (address + ":" + port + "said :");
Console. WriteLine (inBufferStr );
OutBufferStr = Console. ReadLine ();
OutBuffer = Encoding. ASCII. GetBytes (outBufferStr );
S. Send (outBuffer, outBuffer. Length, SocketFlags. None );
}
}
Catch
{
Console. WriteLine ("the client is closed! ");
}
}
}
}
4. Run leleapplication_socketserver first, and then run leleapplication_socketclient to communicate.
5. Time is limited. I hope all of the above Code will be optimized, because there are some shortcomings, such as the order of speeches and so on.
Author liyongquanlongliang