C # implement Socket communication

Source: Internet
Author: User

 

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

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.