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! ");
}