This communication program does not have server and client points, LAN computer running the same program can communicate.
Because the level is limited, the current version is still very good, can only achieve basic chat function and display online user function.
Prepare to add features such as transfer files, chat logs, optional parameters, and more.
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
Using System.Threading;
Namespace Myqq
{
<summary>
Summary description of the Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
public bool Runing=false; Sign
Public udpclient listen=new UdpClient (2525);
Public IPEndPoint end;
Public IPAddress Groupaddress=ipaddress.parse ("255.255.255.255"); Broadcast Address
public int groupport=2525; Broadcast port
public string MachineName;
public string Machineip;
public string Romename;
public string Romeip;
public string Romecon;
Private System.Windows.Forms.ListBox box;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.Label Label2;
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.Button Button3;
Private System.Windows.Forms.Label label3;
Private System.Windows.Forms.TextBox T_rec;
Private System.Windows.Forms.TextBox t_send;
Private System.Windows.Forms.Button button2;
Private System.Windows.Forms.Button newmsg;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.box = new System.Windows.Forms.ListBox ();
This.t_rec = new System.Windows.Forms.TextBox ();
This.label1 = new System.Windows.Forms.Label ();
This.label2 = new System.Windows.Forms.Label ();
This.t_send = new System.Windows.Forms.TextBox ();
This.button1 = new System.Windows.Forms.Button ();
This.button3 = new System.Windows.Forms.Button ();
This.label3 = new System.Windows.Forms.Label ();
This.button2 = new System.Windows.Forms.Button ();
this.newmsg = new System.Windows.Forms.Button ();
This. SuspendLayout ();
//
Box
//
This.box.ItemHeight = 12;
This.box.Location = new System.Drawing.Point (0, 32);
This.box.Name = "box";
This.box.Size = new System.Drawing.Size (200, 352);
This.box.TabIndex = 0;
//
T_rec
//
This.t_rec. BackColor = System.Drawing.SystemColors.InactiveBorder;
This.t_rec. ForeColor = System.Drawing.Color.FromArgb ((System.Byte) (()), ((System.Byte) ()), ((System.Byte) (0));
This.t_rec. Location = new System.Drawing.Point (208, 56);
This.t_rec. Multiline = true;
This.t_rec. Name = "T_rec";
This.t_rec. ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
This.t_rec. Size = new System.Drawing.Size (328, 136);
This.t_rec. TabIndex = 1;
This.t_rec. Text = "";
//
Label1
//
This.label1.Location = new System.Drawing.Point (208, 40);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (120, 16);
This.label1.TabIndex = 2;
This.label1.Text = "Receive:";
//
Label2
//
This.label2.Location = new System.Drawing.Point (208, 200);
This.label2.Name = "Label2";
This.label2.Size = new System.Drawing.Size (120, 16);
This.label2.TabIndex = 2;
This.label2.Text = "Send:";
//
T_send
//
This.t_send. Location = new System.Drawing.Point (208, 216);
This.t_send. Multiline = true;
This.t_send. Name = "T_send";
This.t_send. ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
This.t_send. Size = new System.Drawing.Size (328, 112);
This.t_send. TabIndex = 1;
This.t_send. Text = "";
//
Button1
//
This.button1.Location = new System.Drawing.Point (440, 344);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (75, 32);
This.button1.TabIndex = 3;
This.button1.Text = "Send";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Button3
//
This.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
This.button3.Location = new System.Drawing.Point (88, 3);
This.button3.Name = "Button3";
This.button3.Size = new System.Drawing.Size (56, 23);
This.button3.TabIndex = 4;
This.button3.Text = "Refresh";
This.button3.Click + = new System.EventHandler (This.button3_click);
//
Label3
//
This.label3.Font = new System.Drawing.Font ("XXFarEastFont-Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte) (134));
This.label3.ForeColor = System.Drawing.Color.Blue;
This.label3.Location = new System.Drawing.Point (8, 8);
This.label3.Name = "Label3";
This.label3.Size = new System.Drawing.Size (80, 16);
This.label3.TabIndex = 5;
This.label3.Text = "Online User:";
//
Button2
//
This.button2.Location = new System.Drawing.Point (
This.button2.Name = "Button2";
This.button2.Size = new System.Drawing.Size (75, 32);
This.button2.TabIndex = 6;
This.button2.Text = "Empty";
This.button2.Click + = new System.EventHandler (This.button2_click);
//
Newmsg
//
This.newmsg.Location = new System.Drawing.Point (208, 56);
This.newmsg.Name = "Newmsg";
This.newmsg.Size = new System.Drawing.Size (328, 136);
This.newmsg.TabIndex = 7;
This.newmsg.Text = "There is new news!" ";
This.newmsg.Click + = new System.EventHandler (This.newmsg_click);
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (544, 389);
This. Controls.Add (This.button2);
This. Controls.Add (THIS.LABEL3);
This. Controls.Add (This.button3);
This. Controls.Add (This.button1);
This. Controls.Add (THIS.LABEL1);
This. Controls.Add (This.box);
This. Controls.Add (THIS.LABEL2);
This. Controls.Add (This.t_send);
This. Controls.Add (THIS.T_REC);
This. Controls.Add (THIS.NEWMSG);
This. Name = "Form1";
This. Text = "Small Y";
This. Closing + = new System.ComponentModel.CancelEventHandler (this. Form1_Closing);
This. Load + = new System.EventHandler (this. Form1_Load);
This. ResumeLayout (FALSE);
}
#endregion
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private void Form1_Load (object sender, System.EventArgs e)
{
End=new IPEndPoint (Groupaddress,groupport);
Initializing computer names and ports
Iphostentry Myentry=dns.gethostbyname (Dns.gethostname ());
IPAddress myaddress=new IPAddress (myentry. Addresslist[0]. address);
Machinename=dns.gethostname ();
Machineip=myaddress. ToString ();
Box. Items.Add ("IP host name");
Turn on the listener thread
Runing=true;
Thread Mythread=new Thread (the new ThreadStart (this. Listenport));
Mythread.start ();
Send online information
String sends= "0&" +machineip+ "&" +machinename+ "&" +MACHINEIP;
Sendpack (sends);
}
Listens for broadcast address UDP packets for a specified port
public void Listenport ()
{
IPEndPoint tempend=new IPEndPoint (ipaddress.any,2525);
while (runing)
{
Application.doevents ();
Try
{
Byte[] Recb=listen. Receive (ref tempend);
Check the information that is received
Checkmessage (RECB);
}
catch (Exception e)
{
MessageBox.Show (E.message.tostring ());
Break
}
}
Listen. Close ();
Box. Items.Add ("Thread has exited!") ");
Runing=false;
}
Loop Receive Package
public void Checkmessage (byte[] RECBB)
{
String recstr=encoding.default.getstring (RECBB);
String[] Rec=recstr.split (' & ');
Switch (rec[0])
{
Case "0"://Refresh
if (REC[3]==MACHINEIP)
{
If box. FindString (rec[1]+ "" +rec[2]) <=0)
Box. Items.Add (rec[1]+ "" +rec[2]);
}
else if (rec[1]==rec[3])
{
A refresh request sent from another machine
Return to your own information
String sendstr= "0&" +machineip+ "&" +machinename+ "&" +rec[3];
This. Sendpack (SENDSTR);
Add the user if it does not exist
If box. FindString (rec[1]+ "" +rec[2]) <=0)
Box. Items.Add (rec[1]+ "" +rec[2]);
}
Break
Case "1"://Speak
if (REC[3]==MACHINEIP)
{
ROMENAME=REC[2];
ROMEIP=REC[1];
ROMECON=REC[4];
Showne ();
}
Break
Case "2"://Transfer file
Break
Case "9"://Confirm Package
if (REC[3]==MACHINEIP)
MessageBox.Show ("Information from: rec[2] (" +rec[1]+ ") \ r \ n" +rec[4], "message");
Break
}
}
Send packet to broadcast address
public void Sendpack (string sendstr)
{
Byte[] Sendb=encoding.default.getbytes (SENDSTR);
Try
{
Listen. Send (sendb,sendb. Length,end);
}
catch (Exception e)
{
MessageBox.Show (E.message.tostring ());
}
}
Refresh
private void Button3_Click (object sender, System.EventArgs e)
{
Box. Items.clear ();
Box. Items.Add ("IP host name");
String temp2= "0&" +machineip+ "&" +machinename+ "&" +MACHINEIP;
This. Sendpack (TEMP2);
}
Turn off loops
private void Form1_Closing (object sender, System.ComponentModel.CancelEventArgs e)
{
Runing=false;
UdpClient mm=new udpclient ();
IPEndPoint tempipend=new IPEndPoint (Ipaddress.parse ("127.0.0.1"), 2525);
String temps= "0&0&0&0&0";
Byte[] Sendb=encoding.default.getbytes (temps);
Mm. Send (sendb,sendb. Length,tempipend);
}
Send
private void Button1_Click (object sender, System.EventArgs e)
{
if (t_send. Text==null)
{
MessageBox.Show ("Cannot send empty information!") ");
Return
}
If box. selecteditem==null| | Box. selectedindex==0)
{
MessageBox.Show ("Please select a user first!") ");
Return
}
String send= "1&" +machineip+ "&" +machinename+ "&" +box. Selecteditem.tostring (). Split (") [0]+" & "+t_send. Text;
This. Sendpack (send);
}
Empty
private void Button2_Click (object sender, System.EventArgs e)
{
T_send. Text=string. Empty;
T_rec. Text=string. Empty;
}
Click to display the received information and hide itself
private void Newmsg_click (object sender, System.EventArgs e)
{
T_rec. text= "Message from" +romename+ "(" +romeip+ ")" +datetime.now.toshortdatestring "();
T_rec. text+= "\r\n>" +ROMECON;
String back= "9&" +machineip+ "&" +machinename+ "&" +romeip+ "& Information is opened! ";
This. Sendpack (back);
Newmsg. SendToBack ();
}
Show prompt when information is available
public void Showne ()
{
form display
if (this. Windowstate!=formwindowstate.normal)
{
This. Visible=true;
This. Windowstate=formwindowstate.normal;
}
Newmsg. BringToFront ();
}
}
}