C #. net udp point-to-point chat program

Source: Internet
Author: User

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
 

Related Article

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.