|
|
Posted on 2006-09-12 11:20 怡文圣美的空间 阅读(91) 评论(0) 编辑 收藏 引用
以下是MainForm.cs的源代码
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Threading;
using
System.Net.Sockets;
using
System.IO;
using
System.Net;


namespace
MyQ

{

/**/
///
<summary>
///
Form1 的摘要说明。
///
</summary>
public
class
MainForm : System.Windows.Forms.Form

{
//
类成员变量
private
System.ComponentModel.Container components
=
null
;
private
bool
isMainComputer;
private
string
strUserName;
private
string
strIP;
private
int
intLocPort;
private
int
intRemotePort;
private
Thread th;
private
TcpListener tlListen1;
private
bool
listenRun;
private
NetworkStream tcpStream;
private
StreamWriter sw;
private
TcpClient tcpc;
private
System.Windows.Forms.RichTextBox rtbRemoteMessage;
private
System.Windows.Forms.RichTextBox rtbLocalMessage;
private
System.Windows.Forms.Button buttonClose;
private
System.Windows.Forms.Button buttonSend;
private
System.Windows.Forms.MainMenu mainMenu1;
private
System.Windows.Forms.MenuItem menuItem1;
private
System.Windows.Forms.MenuItem menuItem2;
private
System.Windows.Forms.Panel panelBottom;
private
System.Windows.Forms.Panel panelFill;
private
System.Windows.Forms.Splitter splitter1;
private
System.Windows.Forms.Panel panelBotton_Right;
private
Socket skSocket;
//
构造函数
public
MainForm(
string
n,
string
ip,
string
locport,
string
report,
bool
loginstyle)

{
this
.strUserName
=
n;
this
.strIP
=
ip;
this
.intLocPort
=
Int32.Parse(locport);
this
.intRemotePort
=
Int32.Parse(report);
this
.isMainComputer
=
loginstyle;
InitializeComponent();
}
protected
override
void
Dispose(
bool
disposing )

{
try
{
this
.listenRun
=
false
;
th.Abort();
th
=
null
;
this
.tlListen1.Stop();
this
.skSocket.Close();
this
.tcpc.Close();
this
.sw.Close();
}
catch
{}
if
( disposing )

{
if
(components
!=
null
)

{
components.Dispose();
}
}
base
.Dispose( disposing );
}
Windows 窗体设计器生成的代码
#region
Windows 窗体设计器生成的代码
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()

{
System.Resources.ResourceManager resources
=
new
System.Resources.ResourceManager(
typeof
(MainForm));
this
.rtbRemoteMessage
=
new
System.Windows.Forms.RichTextBox();
this
.rtbLocalMessage
=
new
System.Windows.Forms.RichTextBox();
this
.buttonClose
=
new
System.Windows.Forms.Button();
this
.buttonSend
=
new
System.Windows.Forms.Button();
this
.mainMenu1
=
new
System.Windows.Forms.MainMenu();
this
.menuItem1
=
new
System.Windows.Forms.MenuItem();
this
.menuItem2
=
new
System.Windows.Forms.MenuItem();
this
.panelBottom
=
new
System.Windows.Forms.Panel();
this
.panelBotton_Right
=
new
System.Windows.Forms.Panel();
this
.panelFill
=
new
System.Windows.Forms.Panel();
this
.splitter1
=
new
System.Windows.Forms.Splitter();
this
.panelBottom.SuspendLayout();
this
.panelBotton_Right.SuspendLayout();
this
.panelFill.SuspendLayout();
this
.SuspendLayout();
//
//
rtbRemoteMessage
//
this
.rtbRemoteMessage.BackColor
=
System.Drawing.Color.White;
this
.rtbRemoteMessage.Dock
=
System.Windows.Forms.DockStyle.Fill;
this
.rtbRemoteMessage.Location
=
new
System.Drawing.Point(
0
,
0
);
this
.rtbRemoteMessage.Name
=
"
rtbRemoteMessage
"
;
this
.rtbRemoteMessage.ReadOnly
=
true
;
this
.rtbRemoteMessage.Size
=
new
System.Drawing.Size(
416
,
211
);
this
.rtbRemoteMessage.TabIndex
=
0
;
this
.rtbRemoteMessage.TabStop
=
false
;
this
.rtbRemoteMessage.Text
=
""
;
//
//
rtbLocalMessage
//
this
.rtbLocalMessage.Dock
=
System.Windows.Forms.DockStyle.Bottom;
this
.rtbLocalMessage.Location
=
new
System.Drawing.Point(
0
,
217
);
this
.rtbLocalMessage.Name
=
"
rtbLocalMessage
"
;
this
.rtbLocalMessage.Size
=
new
System.Drawing.Size(
416
,
64
);
this
.rtbLocalMessage.TabIndex
=
1
;
this
.rtbLocalMessage.Text
=
""
;
//
//
buttonClose
//
this
.buttonClose.FlatStyle
=
System.Windows.Forms.FlatStyle.System;
this
.buttonClose.Location
=
new
System.Drawing.Point(
32
,
8
);
this
.buttonClose.Name
=
"
buttonClose
"
;
this
.buttonClose.Size
=
new
System.Drawing.Size(
80
,
23
);
this
.buttonClose.TabIndex
=
2
;
this
.buttonClose.Text
=
"
关闭(C)
"
;
this
.buttonClose.Click
+=
new
System.EventHandler(
this
.buttonClose_Click);
//
//
buttonSend
//
this
.buttonSend.Enabled
=
false
;
this
.buttonSend.FlatStyle
=
System.Windows.Forms.FlatStyle.System;
this
.buttonSend.Location
=
new
System.Drawing.Point(
136
,
8
);
this
.buttonSend.Name
=
"
buttonSend
"
;
this
.buttonSend.Size
=
new
System.Drawing.Size(
80
,
23
);
this
.buttonSend.TabIndex
=
3
;
this
.buttonSend.Text
=
"
发送(S)
"
;
this
.buttonSend.Click
+=
new
System.EventHandler(
this
.buttonSend_Click);
//
//
mainMenu1
//
this
.mainMenu1.MenuItems.AddRange(
new
System.Windows.Forms.MenuItem[]
{
this
.menuItem1}
);
//
//
menuItem1
//
this
.menuItem1.Index
=
0
;

this
.menuItem1.MenuItems.AddRange(
new
System.Windows.Forms.MenuItem[]
{
this
.menuItem2}
);
this
.menuItem1.Text
=
"
文件
"
;
//
//
menuItem2
//
this
.menuItem2.Index
=
0
;
this
.menuItem2.Text
=
"
退出
"
;
this
.menuItem2.Click
+=
new
System.EventHandler(
this
.menuItem2_Click);
//
//
panelBottom
//
this
.panelBottom.Controls.Add(
this
.panelBotton_Right);
this
.panelBottom.Dock
=
System.Windows.Forms.DockStyle.Bottom;
this
.panelBottom.Location
=
new
System.Drawing.Point(
0
,
281
);
this
.panelBottom.Name
=
"
panelBottom
"
;
this
.panelBottom.Size
=
new
System.Drawing.Size(
416
,
40
);
this
.panelBottom.TabIndex
=
4
;
//
//
panelBotton_Right
//
this
.panelBotton_Right.Controls.Add(
this
.buttonSend);
this
.panelBotton_Right.Controls.Add(
this
.buttonClose);
this
.panelBotton_Right.Dock
=
System.Windows.Forms.DockStyle.Right;
this
.panelBotton_Right.Location
=
new
System.Drawing.Point(
184
,
0
);
this
.panelBotton_Right.Name
=
"
panelBotton_Right
"
;
this
.panelBotton_Right.Size
=
new
System.Drawing.Size(
232
,
40
);
this
.panelBotton_Right.TabIndex
=
4
;
//
//
panelFill
//
this
.panelFill.Controls.Add(
this
.rtbRemoteMessage);
this
.panelFill.Controls.Add(
this
.splitter1);
this
.panelFill.Controls.Add(
this
.rtbLocalMessage);
this
.panelFill.Dock
=
System.Windows.Forms.DockStyle.Fill;
this
.panelFill.Location
=
new
System.Drawing.Point(
0
,
0
);
this
.panelFill.Name
=
"
panelFill
"
;
this
.panelFill.Size
=
new
System.Drawing.Size(
416
,
281
);
this
.panelFill.TabIndex
=
5
;
//
//
splitter1
//
this
.splitter1.Dock
=
System.Windows.Forms.DockStyle.Bottom;
this
.splitter1.Location
=
new
System.Drawing.Point(
0
,
211
);
this
.splitter1.Name
=
"
splitter1
"
;
this
.splitter1.Size
=
new
System.Drawing.Size(
416
,
6
);
this
.splitter1.TabIndex
=
2
;
this
.splitter1.TabStop
=
false
;
//
//
MainForm
//
this
.AutoScaleBaseSize
=
new
System.Drawing.Size(
6
,
14
);
this
.ClientSize
=
new
System.Drawing.Size(
416
,
321
);
this
.Controls.Add(
this
.panelFill);
this
.Controls.Add(
this
.panelBottom);
this
.Icon
=
((System.Drawing.Icon)(resources.GetObject(
"
$this.Icon
"
)));
this
.KeyPreview
=
true
;
this
.Menu
=
this
.mainMenu1;
this
.Name
=
"
MainForm
"
;
this
.StartPosition
=
System.Windows.Forms.FormStartPosition.CenterScreen;
this
.Text
=
"
Form1
"
;
this
.KeyDown
+=
new
System.Windows.Forms.KeyEventHandler(
this
.MainForm_KeyDown);
this
.Load
+=
new
System.EventHandler(
this
.MainForm_Load);
this
.panelBottom.ResumeLayout(
false
);
this
.panelBotton_Right.ResumeLayout(
false
);
this
.panelFill.ResumeLayout(
false
);
this
.ResumeLayout(
false
);

}
#endregion
[STAThread]
static
void
Main()

{
try
{
Login f
=
new
Login();
if
(f.ShowDialog()
==
DialogResult.OK)

{
if
(f.loginStyle.SelectedItem.ToString()
==
"
主机
"
)

{
f.boolLoginStyle
=
true
;
}
else
{
f.boolLoginStyle
=
false
;
}
MainForm frm
=
new
MainForm(f.textUserName.Text,f.textRemoteIP.Text,f.textLocPort.Text,f.textRePort.Text,f.boolLoginStyle);
f.Dispose();
Application.Run(frm);
}
}
catch
(Exception ex)

{
MessageBox.Show(ex.Message);
}
}
private
void
Listen()

{
this
.listenRun
=
true
;
try
{
this
.tlListen1
=
new
TcpListener(
this
.intLocPort);
this
.tlListen1.Start();
this
.skSocket
=
this
.tlListen1.AcceptSocket();
EndPoint tempRemoteEP
=
this
.skSocket.RemoteEndPoint;
while
(
this
.listenRun)

{
Byte[] stream
=
new
byte
[
80
];
int
i
=
this
.skSocket.ReceiveFrom(stream,
ref
tempRemoteEP);
string
strInf
=
System.Text.Encoding.UTF8.GetString(stream,
0
,i);
if
(strInf
==
""
)

{
this
.rtbRemoteMessage.AppendText(
"
\n
"
+
"
对方已挂断
"
);
this
.buttonSend.Enabled
=
false
;
break
;
}
switch
(strInf)

{
//
主机传给辅机的,告诉辅机主机已响应
case
"
cns
"
:
this
.rtbRemoteMessage.AppendText(
"
\n
"
+
"
恭喜,主机已成功响应,你们可以聊天了。
"
);
this
.buttonSend.Enabled
=
true
;
break
;
//
辅机对主机进行主动连接
case
"
cn
"
:
try
{
tcpc
=
new
TcpClient(
this
.strIP,
this
.intRemotePort);
tcpStream
=
tcpc.GetStream();
//
发送连接通知字符cns
string
cns
=
"
cns
"
;
sw
=
new
StreamWriter(
this
.tcpStream);
sw.Write(cns);
sw.Flush();
}
catch
(Exception)

{
this
.rtbRemoteMessage.AppendText(
"
\n
"
+
"
连接失败!可能对方还未登陆,请告知对方登陆。也可能对方所填端口与您填写的端口不一致!
"
);
}
this
.rtbRemoteMessage.AppendText(
"
\n
"
+
"
恭喜,辅机已成功连接上了主机,你们可以聊天了。
"
);
this
.buttonSend.Enabled
=
true
;
break
;
default
:
this
.rtbRemoteMessage.AppendText(
"
\n
"
+
"
对方:
"
+
strInf);
break
;
}
}
}
catch
(System.Security.SecurityException)

{
MessageBox.Show(
"
防火墙安全错误!
"
,
"
错误
"
,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
private
void
MainForm_Load(
object
sender, System.EventArgs e)

{
this
.listenRun
=
true
;
th
=
new
Thread(
new
ThreadStart(Listen));
th.Start();
if
(
!
this
.isMainComputer)

{
//
这里是辅机
this
.rtbRemoteMessage.AppendText(
"
正在连接主机
"
);
this
.Text
=
"
MyQ 辅机
"
;
try
{
tcpc
=
new
TcpClient(
this
.strIP,
this
.intRemotePort);
tcpStream
=
tcpc.GetStream();
//
发送连接通知字符cn
string
strInf
=
"
cn
"
;
sw
=
new
StreamWriter(
this
.tcpStream);
sw.Write(strInf);
sw.Flush();
}
catch
(Exception)

{
this
.rtbRemoteMessage.AppendText(
"
\n
"
+
"
连接失败!可能对方还未登陆,请告知对方登陆。也可能对方所填端口与您填写的端口不一致!
"
);
}
}
else
{
this
.Text
=
"
MyQ 主机
"
;
this
.rtbRemoteMessage.AppendText(
"
正在监听,等待辅机连接
"
);
}
}
private
void
buttonSend_Click(
object
sender, System.EventArgs e)

{
string
strInf
=
this
.rtbLocalMessage.Text;
if
(strInf
!=
""
)

{
this
.rtbRemoteMessage.AppendText(
"
\n
"
+
"
自己:
"
+
strInf);
sw
=
new
StreamWriter(
this
.tcpStream);
sw.Write(strInf);
sw.Flush();
this
.rtbLocalMessage.Clear();
}
else
{
MessageBox.Show(
"
不可以输入空字符串,否则对方会认为已断线!
"
);
}
}
private
void
menuItem2_Click(
object
sender, System.EventArgs e)

{
Application.Exit();
}
private
void
buttonClose_Click(
object
sender, System.EventArgs e)

{
Application.Exit();
}
private
void
MainForm_KeyDown(
object
sender, System.Windows.Forms.KeyEventArgs e)

{
if
((Control.ModifierKeys
==
Keys.Alt)
&&
(e.KeyCode
==
System.Windows.Forms.Keys.S))

{
this
.buttonSend_Click(
this
,e);
}
}
}
}
|