There is less information on the Axwinsock controls in. NET, and the case is less pathetic.
I've been torturing axwinsock a while ago how to listen to multiple clients. And now it's out, hoping to help the people who need it.
Server-side:
Add the Axwinsock control to the form. If you set the port LocalPort in the control properties such as: 1001,ip address Localip, the control is automatically bound.
1. Turn on AxWinsock1 monitoring in the form Load event
private void Form1_Load (object sender, EventArgs e)
{
Axwinsock1.listen ();//Turn on monitoring
}
2. Create two events in the form background code
(1) Receive Send event
private void Ax_dataarrival (object sender, Axmswinsocklib.dmswinsockcontrolevents_dataarrivalevent e)
{
Try
{
Object data = "";
Axmswinsocklib.axwinsock Sendsock = (axmswinsocklib.axwinsock) sender;
Sendsock. GetData (ref data);//Receive
Sendsock. SendData (Sendsock. Remotehostip + ":" + sendsock. RemotePort + ", know, client. I'm a server! "+" \ n ");//Send
}
catch (Exception ex)
{
MessageBox.Show (ex. ToString ());
}
}
(2) Axwinsock Disconnect 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 Axwinsock controls to bind different clients and add them 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;//to client bound receive send event
Axsock. Closeevent + = ax_closeevent;
}
The server has been created
============================================================================================================
Client:
1. Add the Axwinsock control to the client form
2. Open the Axwinsock connection server in the form load
private void Form1_Load (object sender, EventArgs e)
{
Axwinsock1.connect (server IP, server port);
}
3, to the Connectionrequest event in the Axwinsock control to receive the information sent by 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 am the client!" ");
}
Axwinsock listening to multiple clients in. Net