Write a TCP-based Socket communication Demo.

Source: Internet
Author: User

. Net Socket communication can use Socket classes, or TcpClient, TcpListener, and UdpClient classes. I am using the Socket class, Tcp protocol.

The program is very simple. It is a command line server and a command line client. After the server starts, it binds the local port based on the input port number and starts listening. After the client starts, it connects to the server based on the number of clients, service IP addresses, and service port number entered. After the client connection is successful, start a new thread to randomly send messages to the server and wait for receiving the messages returned by the server. After the server and the client have successfully created a connection, start the new thread to receive messages from the client and return a message from the client, this loop goes on ......

:

 

  

Key server code

Start listening:

  endPoint =  IPEndPoint(  socket =   socket.Listen(.MaxValue);

Start a new thread to receive client connections:

   socketThread =  Thread(OnSocketAccept) {IsBackground =   Show();

Receive client connection details:

           OnSocketAccept(                 socket = obj           (                                Socket clientSocket =                                    (!                                                                                                               ClientDict[key] =                                                                                                                                    }

 

Send/receive data:

           OnSendOrReceive(                 socket = obj            (                                                   receiveLength =                                    sendLength =                                                                                                     }

Close connection:

                   socket.Disconnect(    }

 

Key client code

Connect to the server:

 ( i = ; i <= clientNum; i++       ConnetServer(IPAddress address,  port,  clientSocket = 

The same code is used for sending/receiving data and disabling connection.

The thread pool thread is used first, and the Server accepts the client connection to use an independent thread (socketThread). This thread may need to be stopped manually.

Use Monitor. TryEnter (ClientDictLock, LockTimeOut) and Monitor. Exit (ClientDictLock). Consider that the lock may form a deadlock and use the lock.

I am a beginner. Thank you for your criticism!

 

Source code download

  

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.