Use C # To implement a simple communication program similar to QQ

Source: Internet
Author: User

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. IO;
Using system. net. Sockets;
Using system. Threading;
Namespace p2pchat
{
///
/// Summary of mainform.
///
Public class mainform: system. Windows. Forms. Form
{
Private system. Windows. Forms. Label label1;
Private system. Windows. Forms. Label label2;
Private system. Windows. Forms. textbox txtip;
Private system. Windows. Forms. textbox txtrecord;
Private system. Windows. Forms. textbox txtname;
Private system. Windows. Forms. Button btnsend;
Private system. Windows. Forms. textbox txtcontent;

Private tcplistener tcplster = new tcplistener (5566 );
System. Threading. threadstart listenport;
System. Threading. Thread Lister;
Private system. Windows. Forms. Label label3;
System. Windows. Forms. policyicon policyicon1;
// The icon displayed in the tray. I use the pet dog icon in QQ.
Private icon IMG = new icon (@ "C: \ openpet. ICO ");
System. Windows. Forms. contextmenu nmenu;
///
/// Required designer variables.
///
Private system. componentmodel. Container components = NULL;
Public mainform ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

// Do not display the maximize button
This. maximizebox = false;
// The task bar is not displayed when the value is minimized.
This. showintaskbar = false;
Listenport + = new threadstart (this. Listen );
Lister = new thread (listenport );
This. Closing + = new system. componentmodel. canceleventhandler (abortlister );
Initializenotifyicon ();
//
// Todo: add Any constructor code after initializecomponent calls
//
}
# Region initialize the tray component
Private void initializenotifyicon ()
{
Yyicon1 = new notifyicon ();
Policyicon1.icon = IMG;
Policyicon1.text = "LAN chat program ";
Policyicon1.visible = true;
Policyicon1.doubleclick + = new eventhandler (this. showmainform );

Menuitem [] menuarray = new menuitem [3];
Menuarray [0] = new menuitem ();
Menuarray [0]. Text = "display Main Window ";
Menuarray [0]. Click + = new eventhandler (this. showmainform );
Menuarray [0]. defaultitem = true;

Menuarray [1] = new menuitem ("-");

Menuarray [2] = new menuitem ();
Menuarray [2]. Text = "quit ";
Menuarray [2]. Click + = new eventhandler (this. exitsystem );

Nmenu = new contextmenu (menuarray );
Policyicon1.contextmenu = nmenu;
}
// Display the Main Window
Private void showmainform (Object sender, system. eventargs E)
{
This. windowstate = system. Windows. Forms. formwindowstate. normal;
}
// Exit the program
Private void exitsystem (Object sender, system. eventargs E)
{
Policyicon1.visible = false;
This. Close ();
}
# Endregion
///
/// Clear all resources in use.
///
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///
Private void initializecomponent ()
{

This. label1 = new system. Windows. Forms. Label ();
This.txt IP = new system. Windows. Forms. Textbox ();
This.txt record = new system. Windows. Forms. Textbox ();
This. label2 = new system. Windows. Forms. Label ();
This.txt name = new system. Windows. Forms. Textbox ();
This. btnsend = new system. Windows. Forms. Button ();
This.txt content = new system. Windows. Forms. Textbox ();
This. label3 = new system. Windows. Forms. Label ();
This. suspendlayout ();
//
// Label1
//
This. label1.location = new system. Drawing. Point (16,232 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (72, 23 );
This. label1.tabindex = 0;
This. label1.text = "target address :";
This. label1.textalign = system. Drawing. contentalignment. middlecenter;
//
// Txtip
//
This.txt IP. Anchor = (system. Windows. Forms. anchorstyles) (system. Windows. Forms. anchorstyles. Left | system. Windows. Forms. anchorstyles. Right )));
This.txt IP. Location = new system. Drawing. Point (80,232 );
This.txt IP. Name = "txtip ";
This.txt IP. size = new system. Drawing. Size (200, 21 );
This.txt IP. tabindex = 1;
This.txt IP. Text = "";
//
// Txtrecord
//
This.txt record. Anchor = (system. Windows. Forms. anchorstyles) (system. Windows. Forms. anchorstyles. Left | system. Windows. Forms. anchorstyles. Right )));
This.txt record. Location = new system. Drawing. Point (16, 32 );
This.txt record. multiline = true;
This.txt record. Name = "txtrecord ";
This.txt record. readonly = true;
This.txt record. scrollbars = system. Windows. Forms. scrollbars. vertical;
This.txt record. size = new system. Drawing. Size (264,176 );
This.txt record. tabindex = 4;
This.txt record. Text = "";
//
// Label2
//
This. label2.location = new system. Drawing. Point (24,256 );
This. label2.name = "label2 ";
This. label2.size = new system. Drawing. Size (48, 23 );
This. label2.tabindex = 5;
This. label2.text = :";
This. label2.textalign = system. Drawing. contentalignment. middlecenter;
//
// Txtname
//
This.txt name. Location = new system. Drawing. Point (80,256 );
This.txt name. Name = "txtname ";
This.txt name. size = new system. Drawing. Size (88, 21 );
This.txt name. tabindex = 6;
This.txt name. Text = "";
//
// Btnsend
//
This. btnsend. Location = new system. Drawing. Point (200,256 );
This. btnsend. Name = "btnsend ";
This. btnsend. size = new system. Drawing. Size (64, 23 );
This. btnsend. tabindex = 0;
This. btnsend. Text = "send ";
This. btnsend. Click + = new system. eventhandler (this. btnsend_click );
//
// Txtcontent
//
This.txt content. Anchor = (system. Windows. Forms. anchorstyles) (system. Windows. Forms. anchorstyles. Bottom | system. Windows. Forms. anchorstyles. Left)
| System. Windows. Forms. anchorstyles. Right )));
This.txt content. Location = new system. Drawing. Point

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.