In. net, axWinsock listens to multiple clients.

Source: Internet
Author: User

In. net, axWinsock listens to multiple clients.

There is little information on the axWinsock controls in. net on the Internet, and there are even fewer cases.

AxWinsock has been suffering some time ago from listening to multiple clients. Now I finally got it out, hoping to help people who need it.

Server:

Add the axWinsock control to the form. Set the port LocalPort such as 1001 and IP address LocalIP in the control properties. The control is automatically bound.

1. Enable the axWinsock1 listener in the form loading event.

Private void Form1_Load (object sender, EventArgs e)
{
AxWinsock1.Listen (); // enable listening
}

2. Create two events in the Form Background code

(1) receive send events

Private void ax_DataArrival (object sender, AxMSWinsockLib. DMSWinsockControlEvents_DataArrivalEvent e)
{
Try
{
Object data = "";
AxMSWinsockLib. AxWinsock sendsock = (AxMSWinsockLib. AxWinsock) sender;
Sendsock. GetData (ref data); // receives
Sendsock. SendData (sendsock. RemoteHostIP + ":" + sendsock. RemotePort + ", OK, client. I am a server! "+" \ N "); // send
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
}

(2) axWinsock disconnection event

Private void ax_CloseEvent (object sender, EventArgs e)
{
AxMSWinsockLib. AxWinsock sendsock = (AxMSWinsockLib. AxWinsock) sender;
Socklist. Remove (sendsock );
This. Controls. Remove (sendsock );
}

3. Add the following code to the axWinsock ConnectionRequest event:

Int k;

Private void axWinsock1_ConnectionRequest (object sender, AxMSWinsockLib. DMSWinsockControlEvents_ConnectionRequestEvent e)
{

// Create an AxWinsock control and bind it to different clients and add it to the form.
AxMSWinsockLib. AxWinsock axsock = new AxMSWinsockLib. AxWinsock ();
Axsock. Name = (axsock + e. requestID. ToString () + k. ToString ());
(System. ComponentModel. ISupportInitialize) (axsock). BeginInit ();
This. Controls. Add (axsock );
Int rows = this. Controls. Count;
(System. ComponentModel. ISupportInitialize) (axsock). EndInit ();
Axsock. Accept (e. requestID );
K ++;
Axsock. DataArrival + = ax_DataArrival; // bind to the client to receive and send events
Axsock. CloseEvent + = ax_CloseEvent;

}

The server has been created.

========================================================== ========================================================== ======================================

Client:

1. Add the axWinsock control to the client form

2. Enable axWinsock to connect to the server in form Loading

Private void Form1_Load (object sender, EventArgs e)
{
AxWinsock1.Connect (Server IP address and server port );
}

3. Send the ConnectionRequest event to the axWinsock control to receive the information sent from the server.

Private void axWinsock1_DataArrival (object sender, AxMSWinsockLib. DMSWinsockControlEvents_DataArrivalEvent e)
{
Object data = "";
AxWinsock1.GetData (ref data );
RichTextBox1.AppendText (String) data + "\ n ");
}

4. send information

// Send button event

Private void button2_Click (object sender, EventArgs e)
{
AxWinsock1.SendData ("I'm a client! ");
}

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.