Client:
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. Sockets;
Using system. net;
Using system. Threading;
Namespace mytcpchat
{
Public partial class form1: Form
{
Private thread th;
Socket C;
Public form1 ()
{
Initializecomponent ();
Control. checkforillegalcrossthreadcils = false;
}
Private void form1_load (Object sender, eventargs E)
{
}
Public void client ()
{
Try
{
Ipendpoint serveripep = new ipendpoint (IPaddress. parse (this. combobox1.text. Trim (), 5656 );
C = new socket (serveripep. addressfamily, sockettype. Stream, protocoltype. TCP );
C. Connect (endpoint) serveripep );
C. Send (system. Text. encoding. Default. getbytes (this. sendmessage. Text. Trim ()));
Byte [] DATA = new byte [2048];
While (true)
{
Int rect = C. Receive (data );
Byte [] chat = new byte [rect];
Buffer. blockcopy (data, 0, chat, 0, rect );
This. Message. appendtext (system. Text. encoding. Default. getstring (CHAT ));
}
}
Catch (exception ex)
{
// MessageBox. Show (ex. Message );
}
}
Private void button#click (Object sender, eventargs E)
{
Try
{
Th = new thread (New threadstart (client); // create a thread for listening
Th. Start (); // open a new thread
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}
}
}
Server:
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. Sockets;
Using system. net;
Using system. Threading;
Using system. collections;
Namespace mytcpserver
{
Public partial class form1: Form
{
Socket S;
Ipendpoint serveripep;
Private thread th;
Socket UC;
Private arraylist alsock ;//
Public form1 ()
{
Initializecomponent ();
Control. checkforillegalcrossthreadcils = false;
}
Public void server ()
{
Int Port = 5656;
Serveripep = new ipendpoint (IPaddress. Any, Port );
S = new socket (serveripep. addressfamily, sockettype. Stream, protocoltype. TCP );
S. BIND (endpoint) serveripep );
S. Listen (10 );
Alsock = new arraylist ();
While (true)
{
Try
{
UC = S. Accept ();
Alsock. Add (UC );
This. textbox1.appendtext (system. Convert. tostring (UC ));
Byte [] DATA = new byte [2048];
Int rect = UC. Receive (data );
Byte [] chat = new byte [rect];
Buffer. blockcopy (data, 0, chat, 0, rect );
This. mymessage. appendtext ("[" + UC. remoteendpoint. tostring () + "]" + system. Text. encoding. Default. getstring (CHAT ));
}
Catch (exception ex)
{
// MessageBox. Show (ex. Message );
}
}
}
Private void form1_load (Object sender, eventargs E)
{
}
Private void button#click (Object sender, eventargs E)
{
This. button1.enabled = false;
This. button2.enabled = true;
Th = new thread (New threadstart (server); // create a thread for listening
Th. Start (); // open a new thread
}
Private void button2_click (Object sender, eventargs E)
{
Try
{
This. button2.enabled = false;
This. button1.enabled = true;
S. Close ();
Th. Abort (); // terminate the thread
}
Catch (exception ex)
{
// MessageBox. Show (ex. Message );
}
}
Private void button3_click (Object sender, eventargs E)
{
If (UC = NULL)
{
MessageBox. Show ("You cannot chat with anyone, please tell the server to chat ");
}
Else
{
Int Index = int. parse (this. textbox2.text. Trim ());
Socket SC = (socket) alsock [Index];
// Send data
SC. Send (system. Text. encoding. Default. getbytes (this. answermessage. Text. Trim ()));
}
}
}
}