For point-to-point chat programs written by UDP, IP addresses must be written.

Source: Internet
Author: User
If the following code is incorrect or you do not understand it, contact QQ: 41305987.

Code
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. net. Sockets;
Using system. net;
Using system. Threading;

Namespace point_talk_to_face
{
Public partial class form1: Form
{

Public form1 ()
{
Initializecomponent ();
}
Private thread th;
Udpclient UDP, udp2;
Bool L = true;
Private void textbox2_textchanged (Object sender, eventargs E)
{

}

Private void form1_load (Object sender, eventargs E)
{
System. Windows. Forms. Control. checkforillegalcrossthreadcils = false;
Th = new thread (New threadstart (Listen ));
Th. Start ();
}

Private void button#click (Object sender, eventargs E)
{
/**************************** Send ********* **************************************** ***/
Udp2. = new udpclient (textbox3.text, 9999 );
Byte [] DATA = encoding. ASCII. getbytes (textbox2.text );
Udp2.send (data, data. Length );
Textbox1.appendtext ("to me" + textbox3.text + ":" + textbox2.text + "\ n ");
Udp2.close ();
This. textbox2.clear ();
Textbox2.focus ();
/********************************** Send **** **************************************** *********/
}
Private void listen ()
{
/*************************************** ********************************** ****************/
UDP = new udpclient (9999 );
Ipendpoint remoteiep = new ipendpoint (ipaddresses. Any, 9999 );

While (l)
{
Try
{
Byte [] DATA = UDP. Receive (ref remoteiep );

If (data. length> 0)
{
String word = encoding. ASCII. getstring (data );
Label1.text = remoteiep. Address. tostring ();
Label2.text = remoteiep. Port. tostring ();
Textbox1.appendtext (label1.text + "to me:" + word. tostring () + "\ n ");
}
Else
{

}
}
Catch (exception ex)
{
// Textbox1.appendtext (ex. Message );
// MessageBox. Show (ex. Message. tostring ());
}
}
/*************************************** *********************************** ************************/
}

Private void textbox1_textchanged (Object sender, eventargs E)
{
}

Private void close (Object sender, formclosingeventargs E)
{
Th. Abort ();
L = false;
UDP. Close ();
}

Private void form=formclosed (Object sender, formclosedeventargs E)
{
}
}
}

 

 

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.