The client initiates a connection to the server:
[Csharp]
// Set the service IP address and UDP port number
Ipep = new IPEndPoint (IPAddress. Parse (textBox1.Text), int. Parse (textBox2.Text); // The first parameter is the Server IP address, and the second parameter is the server port number.
// Define the network type, data connection type, and network protocol UDP
Server = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp );
Thread th = new Thread (new ThreadStart (myListen); <span style = "white-space: pre"> </span> // create a Thread to listen to messages from the server
Th. isBackground = true; <span style = "white-space: pre"> </span> // If IsBackground is set to true, the thread ends when the program exits, otherwise it will be enabled all the time
Th. Start ();
// Set the service IP address and UDP port number
Ipep = new IPEndPoint (IPAddress. Parse (textBox1.Text), int. Parse (textBox2.Text); // The first parameter is the Server IP address, and the second parameter is the server port number.
// Define the network type, data connection type, and network protocol UDP
Server = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp );
Thread th = new Thread (new ThreadStart (myListen); <span style = "white-space: pre"> </span> // create a Thread to listen to messages from the server
Th. isBackground = true; <span style = "white-space: pre"> </span> // If IsBackground is set to true, the thread ends when the program exits, otherwise it will be enabled all the time
Th. Start ();
The client listens to messages from the server:
[Csharp]
Private void myListen ()
{
IPEndPoint sender = new IPEndPoint (IPAddress. Any, 0 );
EndPoint Remote = (EndPoint) sender;
While (true)
{
If (ena) <span style = "white-space: pre"> </span> // The connection can be established only when the connection is sent first, therefore, ena is set to true after the first message is sent.
{
Byte [] data = new byte [1024];
Int recv = server. ReceiveFrom (data, ref Remote );
RichTextBox1.AppendText (Encoding. Default. GetString (data ));
}
}
}
Private void myListen ()
{
IPEndPoint sender = new IPEndPoint (IPAddress. Any, 0 );
EndPoint Remote = (EndPoint) sender;
While (true)
{
If (ena) <span style = "white-space: pre"> </span> // The connection can be established only when the connection is sent first, therefore, ena is set to true after the first message is sent.
{
Byte [] data = new byte [1024];
Int recv = server. ReceiveFrom (data, ref Remote );
RichTextBox1.AppendText (Encoding. Default. GetString (data ));
}
}
}
The client sends messages to the server:
[Csharp]
Byte [] B = Encoding. Default. GetBytes (textBox3.Text + ": \ n" + textBox7.Text + "\ n ");
Server. SendTo (B, B. Length, SocketFlags. None, ipep );
RichTextBox1.AppendText (textBox3.Text + ": \ n" + textBox7.Text + "\ n ");
Ena = true;
Byte [] B = Encoding. Default. GetBytes (textBox3.Text + ": \ n" + textBox7.Text + "\ n ");
Server. SendTo (B, B. Length, SocketFlags. None, ipep );
RichTextBox1.AppendText (textBox3.Text + ": \ n" + textBox7.Text + "\ n ");
Ena = true;
The server starts listening:
[Csharp]
// Obtain the local IP address and set the UDP port number
Ipep2 = new IPEndPoint (IPAddress. Any, int. Parse (textBox9.Text ));
Newsock2 = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp );
// Bind the network address
Newsock2.Bind (ipep2 );
Thread thread = new Thread (new ThreadStart (serverListen); <span style = "white-space: pre"> </span> // listen to messages sent from the client
Thread. IsBackground = true;
Thread. Start ();
// Obtain the local IP address and set the UDP port number
Ipep2 = new IPEndPoint (IPAddress. Any, int. Parse (textBox9.Text ));
Newsock2 = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp );
// Bind the network address
Newsock2.Bind (ipep2 );
Thread thread = new Thread (new ThreadStart (serverListen); <span style = "white-space: pre"> </span> // listen to messages sent from the client
Thread. IsBackground = true;
Thread. Start ();
The server listens to messages sent from clients:
[Csharp]
// Obtain the Client IP Address
IPEndPoint sender = new IPEndPoint (IPAddress. Any, 0 );
Remote = (EndPoint) (sender );
While (true)
{
Byte [] data = new byte [1024];
Newsock2.ReceiveFrom (data, ref Remote );
Button4.Enabled = true;
// TextBox4.Text = sender. Address. ToString (); // Client ip Address and port number cannot be obtained in this way
// TextBox5.Text = sender. Port + "";
RichTextBox2.AppendText (Encoding. Default. GetString (data ));
}
// Obtain the Client IP Address
IPEndPoint sender = new IPEndPoint (IPAddress. Any, 0 );
Remote = (EndPoint) (sender );
While (true)
{
Byte [] data = new byte [1024];
Newsock2.ReceiveFrom (data, ref Remote );
Button4.Enabled = true;
// TextBox4.Text = sender. Address. ToString (); // Client ip Address and port number cannot be obtained in this way
// TextBox5.Text = sender. Port + "";
RichTextBox2.AppendText (Encoding. Default. GetString (data ));
} The server wants the client to send a message:
[Csharp]
Byte [] B = Encoding. Default. GetBytes (textBox6.Text + ": \ n" + textBox8.Text + "\ n ");
// Send information
Newsock2.SendTo (B, B. Length, SocketFlags. None, Remote );
RichTextBox2.AppendText (textBox6.Text + ": \ n" + textBox8.Text + "\ n ");
Byte [] B = Encoding. Default. GetBytes (textBox6.Text + ": \ n" + textBox8.Text + "\ n ");
// Send information
Newsock2.SendTo (B, B. Length, SocketFlags. None, Remote );
RichTextBox2.AppendText (textBox6.Text + ": \ n" + textBox8.Text + "\ n ");
As follows:
Source Code address: http://www.bkjia.com/uploadfile/2012/0407/20120407092136239.rar
Excerpted from Night's Dream