C # socket programming example

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Using system. net;
Using system. net. Sockets;
Using system. Threading;

Namespace test4_2
{
Public partial class form1: Form
{
Socket connectsocket;
// Socket Client;
Byte [] bytes = new byte [1, 1024];
Delegate void listboxdel (string S );
Listboxdel;
Public form1 ()
{
Initializecomponent ();
Textboxcontent. Focus ();
Listboxdel = new listboxdel (ListBox );
// Assign a thread for the connection
Thread threadconnect = new thread (New threadstart (CONNECT ));
Threadconnect. Start ();

}
Public void ListBox (string Str)
{
Listbox1.items. Add (STR );
Listbox1.selectedindex = listbox1.items. Count-1;
Listbox1.clearselected ();
}

// Connection method
Public void connect ()
{

Try
{

// Establish a connection socket
Connectsocket = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );

// Start asynchronous connection
Connectsocket. beginconnect (IPaddress. parse ("172.16.94.152 "),
82,
New asynccallback (connectcallback), // defines the callback function proxy
Connectsocket); // The status passed to the callback function
}
Catch (exception E)
{
MessageBox. Show (E. Message );
}
}

// Callback function of the Connection Method
Private void connectcallback (iasyncresult AR)
{
Try
{
// Obtain the socket from the passed status and create a client socket
Socket Client = (socket) Ar. asyncstate;

// Complete the pending connection operation
Client. endconnect (AR );
Listbox1.invoke (listboxdel, "the connection to the server is successful. You can start the call! ");
Client. beginreceive (bytes, 0, 1000, 0, new asynccallback (receivecallback), client );
}
Catch (exception E)
{
Console. writeline (E. tostring ());
}
}
Public void extends ecallback (iasyncresult AR)
{
Try
{
Socket Client = (socket) Ar. asyncstate;
Int length = client. endreceive (AR );
Listbox1.invoke (listboxdel, encoding. utf8.getstring (bytes, 0, length ));
Client. beginreceive (bytes, 0, 1000, 0, new asynccallback (receivecallback), client );
}
Catch
{
}

}
// Sending Method
Private void send (string data)
{
// Use ASCII to convert a string into a byte sequence
Byte [] bytedata = encoding. utf8.getbytes (data); // converts a string to a byte sequence

// Start sending data to the remote device
Connectsocket. beginsend (bytedata, 0, bytedata. length, socketflags. None,
New asynccallback (sendcallback), connectsocket );
}

// Callback function of the sending Method
Private void sendcallback (iasyncresult AR)
{
Try
{
// Obtain the socket from the passed status and create a client socket
Socket Client = (socket) Ar. asyncstate;

// End asynchronous data transmission and return the number of transmitted bytes
Int bytessent = client. endsend (AR );
Listbox1.invoke (listboxdel, textboxuser. Text + ":" + textboxcontent. Text );

}
Catch (exception E)
{
MessageBox. Show (E. tostring ());
}
}

Private void buttonsend_click (Object sender, eventargs E)
{

Send (textboxuser. Text + ":" + textboxcontent. Text );

}
}
}

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.