Axwinsock listening to multiple clients in. Net

Source: Internet
Author: User

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

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.