You should know about socket asynchronous communication (Code instance) on the. net platform)

Source: Internet
Author: User

1. First, add two windows form projects, one as the server and the other as the Client.

2. Then add the server code and namespace. The TextBox Control is added to the interface.

 System.Net.Sockets;

3. Add code using the FormLoad Method

  Form1_Load(=  IPEndPoint(IPAddress.Parse(), );            Socket socket =  Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            socket.Bind(epServer);            socket.Listen();            socket.BeginAccept( AsyncCallback(RecVing), socket);        }

4. Add the callback method code.

  cmdstr = ==[] bytes =  [ i == Encoding.UTF8.GetString(bytes, 

5. Add the showstrs method,

Define private delegate void shixudong_invoke (string invokefun );

  showstrs( (recv_strs.Length !=  (= .Invoke(invoke, (=

6. By now, the server has been added

7. client code. Add a namespace and add a button event on the interface, just like the server.

  button1_Click(=  IPEndPoint(IPAddress.Parse(), );            Socket socket =  AsyncCallback(Sending), socket);        }

8. Add the callback method. The Code is as follows:

 =[] msgBuff =, , 

9. The client code has been completed so far. Start the server before debugging.

10. Run the following command. before clicking the button, enter shixudong in the text box.

11. click the button as follows:

Test successful, if you have any questions, please contact me shixudong3@yeah.net, hope you can help more!

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.