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