用c#实现类似QQ的简单通讯程序
本文介绍了用c#实现的一个类似QQ的局域网通讯程序,当点击最小化程序跑到系统托盘里,双击托盘可以可以显示主页面。5 s& P9 u0 E# Y/ {
% D7 X3 z/ i/ d" G: V3 y' s- B- l
程序运行界面如下:
( Z3 T. J0 ]/ z) v! K! P- Q2 J5 Q6 F
6 s9 c# G# w2 n4 V9 p9 M3 Y/ ~ 托盘里的菜单如下:
: h, [. Y0 C& \
7 d9 U) @$ S, _! \' l3 h c#作为微软.Net战略的重要棋子,对网络编程提供了很好的支持和优化。实现起来特别方便,还是看代码吧,已经注释的很清楚了。
0 @. [0 T7 e% J代码如下:
H) `' w, {* Y \( |using System;- N$ e5 L4 n3 }5 r4 G1 m
using System.Drawing;
8 Y& d5 J* e% Y* Qusing System.Collections;" v l6 x& ^4 O) r( G$ Z- T* E
using System.ComponentModel;
' t( W% w' M. L2 Pusing System.Windows.Forms;! g7 E' m$ |( k6 l/ V9 ~2 f' d
using System.Data;% n3 {; M& {$ X6 B; x. H8 D& R! s7 b) O
using System.IO;! w5 T0 H) p9 j$ |+ ^) k* v6 s
using System.Net.Sockets; d7 B8 C+ `- W% z$ D6 w$ o8 H; c, a
using System.Threading;" K& F, ?" @( J' x3 U" ~
namespace p2pChat/ P1 {; U: g9 o. t' l
{* w0 C$ I m$ @- i% B3 s
///
* G* Y* r& O z5 f, o3 r3 J% F2 b /// MainForm 的摘要说明。
- q9 B( p9 C U- p5 J1 j ///
4 f2 {8 N: f- Y5 D: K% s public class MainForm : System.Windows.Forms.Form
! j* ~0 `8 Z) h4 x( K' P {
( c' ~. l a( _. V& X2 W7 \2 S" [ private System.Windows.Forms.Label label1;
1 K1 J" a# Y7 r" f. S* H private System.Windows.Forms.Label label2;! j ~: v/ ]* j( Y$ S" }/ M
private System.Windows.Forms.TextBox txtIp;+ e7 l5 S+ b0 u5 v
private System.Windows.Forms.TextBox txtRecord;
: O# @# q# T& P private System.Windows.Forms.TextBox txtName;
d, ]' M, h" j& L, k private System.Windows.Forms.Button btnSend;
) W' o# ?7 c" | private System.Windows.Forms.TextBox txtContent;" B& G5 P5 M. z6 C, P2 t N
# f- A/ _. K9 }7 R K
private TcpListener tcpLister = new TcpListener(5566);
9 a) s" N# V" m: ^$ U& T0 B System.Threading.ThreadStart listenPort;, \# a) Y" p8 K! p4 e: k2 ?- K
System.Threading.Thread lister;/ A( N; o2 M3 L4 }+ r
private System.Windows.Forms.Label label3;
8 k/ U: _1 x# w0 _9 b. M System.Windows.Forms.NotifyIcon NotifyIcon1; F3 S( L' o2 p, M4 `" F
//托盘里显示的图标,我用的是QQ里的宠物狗的图标
1 p$ G g, V5 x, a$ C- B private Icon img = new Icon(@"C:\OpenPet.ico");
7 l2 ^5 w k$ { System.Windows.Forms.ContextMenu nMenu;% ?' \# W: C2 N" v% r# |
///
, F* [9 x! F7 z4 T /// 必需的设计器变量。$ f" d# J* @/ \, V+ C- Y
/// 4 m3 S* Q2 k+ C: Z9 ]
private System.ComponentModel.Container components = null;
& T2 @1 [3 P( U5 ?0 q" T: g public MainForm(). T. a% T# J2 E1 U3 \' g' p
{
1 X+ H0 p0 m( r D& r //- [/ z$ W* b, A! H: m. }
// Windows 窗体设计器支持所必需的- y; P3 C8 ~* P* i, u8 m$ T G- K* b
//- D- Q7 w8 H" }9 \) m1 i( i
InitializeComponent();
/ l$ t& h+ n( r7 K+ j7 U" Z* v+ O; b6 p \& u- R$ t' J
//不显示最大化按钮
' I; Y; c4 N( g3 s8 l( d this.MaximizeBox = false;
& O8 C" g/ J. Y //最小化时不显示在任务栏
& G- i) y2 s! b! O- i# p `# X% a this.ShowInTaskbar = false;2 {! ^ R5 G7 k! V+ r" f+ [
listenPort += new ThreadStart(this.Listen);
! J6 O3 w7 }1 P lister = new Thread(listenPort);' D/ Y3 N, x1 R3 D4 i# P5 ]
this.Closing += new System.ComponentModel.CancelEventHandler(abortLister);2 I3 [2 F- X' Q
Initializenotifyicon();) e; x& Y8 h3 w3 r
//
: f$ | g1 l5 f // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
. O6 _5 ?) V0 ]$ E1 ^7 U4 Q6 v //
. f! {( ]3 K2 j7 G2 E* j8 h }
: D* y! \% N3 y9 O- Z& T #region 初始化托盘组件
* x) ]2 @; W2 _" p4 ?% p private void Initializenotifyicon()' l; G/ I" V$ z- e' G# E
{
) ~! r* M3 l# s1 J" K* z7 G: v8 d NotifyIcon1 = new NotifyIcon();
3 Z2 m$ w) h5 u' c* P6 V, D K NotifyIcon1.Icon = img;
$ z2 `; }- x f+ H8 w0 ~ NotifyIcon1.Text = "局域网聊天程序";4 s; _+ G) H1 r! O3 O1 B, O, N
NotifyIcon1.Visible = true;
# G4 f9 T1 E! L& C- m NotifyIcon1.DoubleClick += new EventHandler(this.showMainForm);/ R. m- ]1 K G; B, `: \
# `! J [6 z/ C. k5 j8 L MenuItem [] menuArray = new MenuItem[3];$ l V. S! R) s) L
menuArray[0] = new MenuItem();. r2 T! I1 V, {9 u/ p
menuArray[0].Text = "显示主窗口";
/ H; i7 H" T: S% R menuArray[0].Click += new EventHandler(this.showMainForm);0 B) D, [1 w- b9 B. E/ a, d
menuArray[0].DefaultItem = true;
. k) o0 ^( e& N/ @8 T' f( p* H" [, L4 `6 \! _+ n
menuArray[1] = new MenuItem("-");" O. H9 Y: }3 `
; b& W# H4 |0 R( k- U, r
menuArray[2] = new MenuItem();3 p! d) q v3 q& V
menuArray[2].Text = "退出";/ l2 P* |/ o. q/ y
menuArray[2].Click += new EventHandler(this.exitSystem);
$ Z. F. B9 z$ C6 X ^9 T) H# D+ P6 }& z# j. R. Z% X; r
nMenu = new ContextMenu(menuArray);. z" b( E1 k9 t- B$ i% Y
NotifyIcon1.ContextMenu = nMenu;, ~5 b2 v5 g# ^
} 4 c- m) C" q4 k9 [
//显示主窗口
. x5 [( W1 y5 ], E e private void showMainForm(object sender,System.EventArgs e)+ [7 i$ l w4 P* w
{
, l1 Q! a2 B, @5 s this.WindowState = System.Windows.Forms.FormWindowState.Normal;
6 x+ X' @ d } }7 V. {( B" T) e; J
//退出程序9 W+ G& H. |3 r* A4 ?: d
private void exitSystem(object sender,System.EventArgs e)0 ~; x& T+ L$ {/ J3 u3 }1 \
{
( |5 E; c2 E. ?' g% g: u: G NotifyIcon1.Visible = false;/ N, Q8 V) |7 r9 ~0 p
this.Close();8 I8 E0 A* n5 s# i8 P
}/ G; {- p) }, e. L& ?
#endregion: Y7 ~1 T+ I2 K9 v" h6 i, F4 n
///
0 h% o+ a$ M# ~' v9 A /// 清理所有正在使用的资源。
. E2 h2 A' a$ b5 T$ y% e ///
8 h9 z; w% N1 D# C: F# M protected override void Dispose( bool disposing )
- n( u# I, z% O" B {
) `/ h6 b5 |$ {' v( h if( disposing )2 y( h6 G' F% g' P8 q
{1 O1 P7 F' L3 { u% b
if (components != null)
/ b, ]. J& ]2 l2 v {
" o+ q* A& u/ [) [, R components.Dispose();
n: R1 N5 ^1 r5 K4 ?& l }
# R6 a9 `: H' ?+ _6 T% W- N. W }
( ^! P/ A |" G; d: P# F$ a base.Dispose( disposing ); 6 [7 k' \( S& I1 Z0 M/ }7 p' G0 A* G, p
}* b: j- O( v, q- l8 `
#region Windows 窗体设计器生成的代码3 ~$ X# Z( F* i: w. o+ ?( |
///
/ r( e5 ^* u; ~( I% O( u /// 设计器支持所需的方法 - 不要使用代码编辑器修改
5 ?2 g; V! L6 n8 T t4 X, ^ /// 此方法的内容。
8 n* o5 ]+ Z R ///
& r; g+ Z* D u/ s8 m$ e private void InitializeComponent()7 B4 I4 |3 n# x0 e, _
{+ H, b8 [5 }# W3 [6 ~9 T( i
this.label1 = new System.Windows.Forms.Label();
2 z2 e" \4 B$ B( U3 i* d9 Y this.txtIp = new System.Windows.Forms.TextBox();
" [6 G' p2 H0 A this.txtRecord = new System.Windows.Forms.TextBox();
4 ? y, R- Y, ]/ a this.label2 = new System.Windows.Forms.Label();
9 N+ i. y! X4 u4 W0 ^! l this.txtName = new System.Windows.Forms.TextBox();
, i1 D3 t1 Z; v$ S this.btnSend = new System.Windows.Forms.Button();
* V: ?2 y$ r: S5 D: d/ I; X0 i this.txtContent = new System.Windows.Forms.TextBox();
" W0 a2 B i9 M; S" w4 T' I8 X9 j this.label3 = new System.Windows.Forms.Label(); G2 Y5 m+ g( x6 X" M! G
this.SuspendLayout();0 @0 u+ p1 Q" ^ g8 l
// * y& b% ^' S/ N1 a& _. n$ k
// label1, D3 G! c' R5 F/ }8 ^4 O7 X
//
C! @/ w5 [- A this.label1.Location = new System.Drawing.Point(16, 232);% z0 c) d( I: k
this.label1.Name = "label1";
4 [2 V; A; K6 s- K- o this.label1.Size = new System.Drawing.Size(72, 23);/ w4 m+ B8 l& L3 f# G9 [/ \( K% M% B3 {
this.label1.TabIndex = 0;
( `5 c6 e- u% ^3 @; a- E) x this.label1.Text = "目标地址:";# j; k7 {4 L, L" ]2 o
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
' e0 H |: L+ q" B2 x //
; b& ? v, o" V/ W // txtIp# r: {4 R( D0 D5 [
// % X( n, a" v4 ^( y
this.txtIp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
+ M& X% n) C1 O this.txtIp.Location = new System.Drawing.Point(80, 232);$ T8 U5 F. C$ ?+ w* b
this.txtIp.Name = "txtIp";
: |9 S, b9 Q6 B+ L; h this.txtIp.Size = new System.Drawing.Size(200, 21);7 `: Y% p3 X. A6 ~
this.txtIp.TabIndex = 1;
( i7 w3 N/ T8 S5 b8 K3 U this.txtIp.Text = "";+ {" k: }% F; V- r/ I" c
// & E2 T# t7 g2 _: J
// txtRecord
2 w( v6 ]) w2 r% h1 e- M // : G* ~' W: x$ v1 H4 t
this.txtRecord.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
8 S8 U8 m' x3 [' j* ^3 l7 N1 p this.txtRecord.Location = new System.Drawing.Point(16, 32);
7 v( C& D/ Y" J1 ` this.txtRecord.Multiline = true;+ l/ `4 \% A1 [7 C$ [
this.txtRecord.Name = "txtRecord";
% B- |0 N$ B, A$ ?+ F P this.txtRecord.ReadOnly = true;. j7 y5 B2 v% U, o6 O+ k4 p& a
this.txtRecord.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;8 M' Y R+ W1 B% e1 }& i/ e- ?
this.txtRecord.Size = new System.Drawing.Size(264, 176);
* w& q- s- w+ Y- y5 } this.txtRecord.TabIndex = 4;- ?+ H4 E8 o9 a7 W" c* m) H
this.txtRecord.Text = "";
' s' ]& @7 K1 s4 i! S //
# E1 v% S- r U$ u // label2# M1 H: b; e( |9 H# o+ U
//
# T- e% x* z$ H& n" f! ~+ z, v this.label2.Location = new System.Drawing.Point(24, 256);
5 l7 h, ^5 i/ a3 H1 l K this.label2.Name = "label2";
0 i6 K" P! h. G this.label2.Size = new System.Drawing.Size(48, 23);
. E. {4 S) k: c0 a; P this.label2.TabIndex = 5;
$ P, V0 S& u) s) N o this.label2.Text = "呢 称:";
7 L8 E0 G7 L6 o: f this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;0 J5 U( R2 f8 F
//
* A! `+ [ ^6 j0 Z4 {) a0 o) i7 p // txtName
& B# q" k8 c; j" U" h9 K //
+ p# `% F9 Z5 H) V- ~ this.txtName.Location = new System.Drawing.Point(80, 256);
[: l! n+ F) a+ B this.txtName.Name = "txtName";% }0 D/ j+ q) U8 O$ Y1 z) W
this.txtName.Size = new System.Drawing.Size(88, 21);
1 M, @: H( l, k# A t2 x this.txtName.TabIndex = 6;) s8 @$ `# T: H' E- S
this.txtName.Text = ""; t+ F4 ~9 P; O2 F4 y$ V, N
// 5 ?" v6 ?! h4 P$ V; h4 t* S
// btnSend. d6 j4 ^' c8 r$ p
//
/ `) ~. e- ]& ? this.btnSend.Location = new System.Drawing.Point(200, 256);& u6 g/ M" T9 C
this.btnSend.Name = "btnSend";
+ K4 J) g( A8 j this.btnSend.Size = new System.Drawing.Size(64, 23); z! j! x2 I' F" M
this.btnSend.TabIndex = 0;+ o; e' C* i: ~% h- n" K; H1 b
this.btnSend.Text = "发 送";
- n0 W/ O R, Y1 U$ _! | this.btnSend.Click += new System.EventHandler(this.btnSend_Click); F# F( ^' k' d9 a" a) L# N- |+ K) U
//
/ k& W9 O& o7 B0 Y9 N // txtContent+ p4 A; l8 E' Z. f: x* q
// * \+ N2 T0 G5 C# ]
this.txtContent.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
0 W8 L5 U. C0 h8 t# i) z+ Y | System.Windows.Forms.AnchorStyles.Right)));: X- ]4 [7 ^ B. i
this.txtContent.Location = new System.Drawing.Point(16, 288);! R7 c# U+ |' [2 o! W* a% ]
this.txtContent.Multiline = true;
. d* s1 ]. G2 N6 { this.txtContent.Name = "txtContent";
* p+ h) j3 B/ ^" o, m( U this.txtContent.Size = new System.Drawing.Size(264, 152);% l z% {% F1 j* m a
this.txtContent.TabIndex = 8;
+ m2 I' k: K( H0 a( M% j; } this.txtContent.Text = "";% k, z+ N/ V a
//
2 j) j0 k+ e0 M- l% y( L // label3
1 J/ F4 B: m X7 o# I- P // 3 ^% o3 n# B& W7 E9 W* g0 r
this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
# B- R O& x. P1 f+ d4 W, l+ u | System.Windows.Forms.AnchorStyles.Right)));
6 L* c0 P$ t. U- m this.label3.Location = new System.Drawing.Point(16, 8);
1 |' |& X% n8 l; E this.label3.Name = "label3";- f0 T: U& {8 O" c& J0 A, a
this.label3.Size = new System.Drawing.Size(100, 16);
( c" o0 \- T! V4 N& }$ T7 {$ v this.label3.TabIndex = 9;0 t6 ]+ p; B. x- Q) v% j
this.label3.Text = "聊天记录:";9 L- K- E3 T _) @) s3 Z- \9 S1 u1 Y
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
3 `9 v1 ]8 I; d# K! x //
2 G4 S0 B; V6 c, t // MainForm s5 x2 y b$ h, @0 F
//
1 p" [ b! k2 I* d this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
& D8 B+ I4 G0 l- X$ o/ N this.ClientSize = new System.Drawing.Size(292, 453);
' x5 `/ J- U* y. r1 h) A this.Controls.Add(this.label3);
3 q; t+ q7 P z- o( K" { this.Controls.Add(this.txtContent);; d1 m3 Q6 D0 K, k( K( H
this.Controls.Add(this.btnSend);$ O( \' m& L. ]+ b2 v
this.Controls.Add(this.txtName);$ z( `: x. `' [4 s+ `$ F" c. o
this.Controls.Add(this.label2);% A( @7 r1 \4 k# Z8 |) t
this.Controls.Add(this.txtRecord);
+ N- z, a% E) i9 o' g this.Controls.Add(this.txtIp);/ q# ]0 Y$ ?, ~9 t/ R
this.Controls.Add(this.label1);/ Y) N4 I0 S# Z/ t( ?
this.Name = "MainForm"; " ^1 m% O5 Z( W3 @; w
this.Text = "局域网聊天程序";
) ]: a# q" z5 @7 E+ F& M this.Load += new System.EventHandler(this.MainForm_Load);4 z( |% ]: g! q
this.ResumeLayout(false);2 Z) c" B# s7 U4 a
}( U$ `1 K( O- B4 c, z4 z
#endregion$ v0 X7 E9 @6 e) r; u6 b) `1 G
/// # m8 b$ \9 U. B& O
/// 应用程序的主入口点。5 Q( @- D- c7 I% d8 ]
///
- |& X. ]# T! n [STAThread]
: ?$ J" ?4 y! x* ?2 O- R0 H static void Main()
% D+ Z3 j2 E2 L; P7 D {
W+ ~6 [' A6 j& f% q Application.Run(new MainForm()); _; G; S) z9 Q5 s/ k" q) m
}/ X6 O! N, D7 W1 I9 a0 Q4 J
//开始监听
2 Q) ^0 l/ V" }* Q1 G {. g* J private void Listen(): M$ a r0 ~6 ?3 ~* U
{
3 {7 }* r s7 h( H! I0 P$ t4 s+ K5 U0 R try2 O; w3 D! h, U' D) J% @% @
{
; j1 X! w+ N) N9 o# w tcpLister.Start();
0 a4 |% D" z2 K+ a* g while(true)# V# \& T* ?) c2 ~- p- u& i7 R3 F
{
* F* C' A5 ]/ v0 W) C Socket s = tcpLister.AcceptSocket();
4 K* \8 R" |6 ^ N. B) Q Byte[] stream = new Byte[80];& h7 _9 E' G' }6 d1 z8 ?3 k1 J5 Q
int i = s.Receive(stream);
+ ^2 {6 J t2 Q6 B string message = System.Text.Encoding.UTF8.GetString(stream);# c7 x* ^$ g4 @# ]/ j- r
this.txtRecord.AppendText(message); % u& j( z( S' @% k& H: f# [# C
}
8 d5 l: i- ]$ h0 f: q }
2 z& Y2 n" T4 Y* e/ M catch(System.Security.SecurityException)# N+ t0 G1 C0 P3 l3 u+ |
{% P3 w# P% q) S- n9 f
MessageBox.Show("防火墙安全错误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);5 c6 |% V! z( w, m- c7 R
}. w! N r$ I0 h# l8 ^* g
catch(System.Exception)
& z* W5 n4 I P {
: l5 l6 e# `; d1 I0 J! k3 \ //this.txtRecord.AppendText("已停止监听!");
. r- G9 H* ?( b2 R }
/ ~) X( d/ y) |, a) r2 B u }
9 J6 ~1 u! Y( E. Y2 g
6 D9 f+ s5 M- \ private void abortLister(object sender,System.ComponentModel.CancelEventArgs e)
9 Y) s9 `$ a! Y8 u* g7 G {
. _$ Y; j: k8 \2 f! n' E5 L, R this.tcpLister.Stop();1 X7 T7 z4 G" M/ d2 h. j9 ^! A
}& [' h3 e7 S ~
//发送- ~* h }" z: ~
private void btnSend_Click(object sender, System.EventArgs e)
6 i/ T9 H% h, O7 p- d{8 m4 s( j1 Y- j3 y
if(this.txtContent.Text=="")
6 v3 l$ t: C3 ~' _{
( w8 |0 u, _4 p1 H) I! |2 VMessageBox.Show("不能发送空信息!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);- S0 F5 f0 I2 u7 B/ L* V4 U
}. x: ^8 C* ]/ p5 y' Z" |
else" c4 z: W& K- J) S
{: a- r0 c+ J/ {* O/ G: f
this.Send();
4 G# Q/ ?0 f" A+ A6 U7 L$ A! a+ q }
* J! V3 J( u2 T6 w3 s/ u }5 \7 M1 w4 E9 E, }# d: w3 q
//发送消息9 y& U* s# _* g# \2 H+ P
private void Send()5 W5 f) o9 v! b: e* T( U
{$ Z% B3 a( w t$ }3 \4 L, E) w/ a
try
- W( Y3 q# e4 d {% _3 C" T1 v {* ]0 G5 P# S2 {
string msg = this.txtName.Text+" ("+System.DateTime.Now.ToString()+")\r\n"+this.txtContent.Text+"\r\n";9 w) ?, p; _5 f: W
TcpClient client = new TcpClient(this.txtIp.Text,5566);2 P$ y! J9 t+ P9 p- _# Q
NetworkStream sendStream = client.GetStream();0 X8 O! X/ K7 n
StreamWriter writer = new StreamWriter(sendStream);" h3 r( @8 @2 o+ K6 a3 ^
writer.Write(msg); V& S- } f+ x- D+ F
writer.Flush();
, A1 j1 Q6 c+ i' O% z sendStream.Close();
- p" B/ ?' D, u client.Close();
( Q7 k' `7 {0 E6 M7 _ this.txtRecord.AppendText(msg);
; x. `4 J' J$ I n- Y: P" [ this.txtContent.Clear();8 _- X; E x' o4 u m5 a9 j- |! }
}
- x5 l4 i( [9 q; a' \ catch(System.Exception)! } E$ u" T. k% _4 y6 O( G
{6 C* c) a! {3 B& B% c! B% q0 d$ h
this.txtRecord.AppendText("目标计算机拒绝连接请求!\r\n");1 g4 C2 W/ n/ f+ q
}/ k# z' s" c; j/ ?& A
}* W" j+ A7 X3 z, ? q8 `2 w
private void MainForm_Load(object sender, System.EventArgs e)
6 e# R# L6 e c1 W) t+ t$ s' { {2 R+ ]; r8 `* D) ]3 o: y
this.txtRecord.AppendText("正在监听...\r\n"); * ?# l) B$ E$ O# C* X4 K: u
lister.Name = "监听本地端口";& S r0 B, O2 K# C+ q
lister.Start();7 p( b/ z1 j w: k0 N4 _5 K
}
c7 S. A. W/ I# r4 N/ [. U" R }: l; Y0 a" \0 B
}
搜索更多相关主题的帖子:
编程文章