Using System;
Using System. Collections. Generic;
Using System.Net;
Using System.Net.Sockets;
using System.Text;
Using System. The Threading;
Using System. Windows. Forms;
The namespace server
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
Control. CheckForIllegalCrossThreadCalls=false;//set the thread monitoring, don't tell the main thread, the main thread to do two things will go wrong
}
Private void label1_Click (object sender, EventArgs e)
{
}
Private void textBox4_TextChanged (object sender, EventArgs e)
{
}
Private void button1_Click (object sender, EventArgs e)
{
//when click start listening on the server side to create a Socket is responsible for monitoring the IP address and port
Socket socketWatch=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
IPAddress IP=IPAddress. Any;//to monitor all client IP
IPEndPoint point=new IPEndPoint (IP, Convert ToInt32 (txtport. Text));//binding IP and port
SocketWatch. Bind (point);//bind listening
SocketWatch. Listen (10);//set to monitor lined up
ShowMsg (" to start listening to ");
Thread th=new Thread (Listen);//create a thread
Th. IsBackground=true;//set as a background thread
Th. Start (socketWatch);//tell the CPU to monitor socketWatch threads ready.
}
Void ShowMsg (string STR)
{
Txtlog. AppendText (STR + "\ r \ n");
}
///& lt; Summary>
///waiting for the client connection, and create communicate with Socket
///& lt;/summary>
///
The Socket socketSend;
Void Listen (object o)//call this method when the o as parameters of the inlet to
{
As the Socket Socket socketWatch="o.//as the strong o into the Socket, if success is true, otherwise empty
While (true)
{
SocketSend=socketWatch. The Accept ();//responsible and client communication socketSend
ShowMsg (socketSend. RemoteEndPoint. ToString () + ":" + "connection success");//get the client's IP and port
//to the client's IP address and Socket of the remote connection in key values in the collection
Dicsocket. Add (socketSend. RemoteEndPoint. ToString (), socketSend);
//to the client's IP address and Socket of the remote connection in the drop-down box
CboUsers. Items. The Add (socketSend. RemoteEndPoint. ToString ());
Thread th=new Thread (Recive);//create Recive thread receives the client data
Th. IsBackground=true;//set as a background thread
Th. Start (socketSend);//tell the CPU to monitor socketSend threads ready.
}
}
///& lt; Summary>
///the server constantly receive messages from the client
///& lt;/summary>
///& lt; Param name="o" & gt;
///
Void Recive (object o)
{
As the Socket Socket socketSend="o.//as the strong o into the Socket, if success is true, otherwise empty
While (true)//cycle to receive user data
{
Byte [] buffer=new byte [1024 * 1024 * 2];//set up buffer
Int r=socketSend. The Receive (buffer);//the received data to the buffer, and returns the int value
If (r==0)//judge the received data is empty then shut down, or had been circulating an empty message
{
break;
}
//use UTF8 and decoding Numbers start from 0 to the end of the r
String STR=Encoding. UTF8. Get string (buffer, 0, r);
//decoding complete added to txtlog
ShowMsg (socketSend RemoteEndPoint + ":" + STR);
}
Try
{
}
The catch (Exception)
{
Throw;
}
}
//create a key value for collection, will the client's IP address and Socket of the remote connection in the key value in the collection
Dictionary
Private void button4_Click (object sender, EventArgs e)
{
String STR=txtMsg. Text. The Trim ();//Trim is to remove space before and after the
Byte [] buffer=System. Text.. Encoding UTF8. GetBytes (STR);//the text string to bytes
List
List. Add (0);
List. The AddRange (buffer);
//convert generic collections to array
Byte [] newBuffer=list. ToArray ();
//user IP address and port in the drop-down box cboUsers
String IP=cboUsers. SelectedItem. ToString ();
Dicsocket [IP]. Send (newBuffer);
//socketSend. Send (buffer);
}
Private void Form1_Load (object sender, EventArgs e)
{
}
}
}
client:
Using System;
Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. Linq;
Using System.Net;
Using System.Net.Sockets;
using System.Text;
Using System. The Threading;
Using System. The Threading. The Tasks;
Using System. Windows. Forms;
The namespace client
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
Control. CheckForIllegalCrossThreadCalls=false;//set the thread monitoring, don't tell the main thread, the main thread to do two things will go wrong
}
The Socket socketSend;//create a responsible for the connection Socket
///& lt; Summary>
///connect to the server
///& lt;/summary>
///& lt; Param name="sender" & gt;
///& lt; Param name="e" & gt;
Private void button1_Click (object sender, EventArgs e)
{
//when click start listening on the server side to create a Socket is responsible for monitoring the IP address and port
SocketSend=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
IPAddress IP=IPAddress. Parse (txtServer. Text);//IP address
IPEndPoint point=new IPEndPoint (IP, Convert ToInt32 (txtport. Text));//binding IP and port
SocketSend. Connect (point);//to connect to a remote application server IP and port
ShowMsg (" connection success ");
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull