using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;namespace Server2
{ public partial class Server : Form
{ private Socket socket;
private IPAddress myIP;
private IPEndPoint endPoint;
private int point;
private Socket clientSocket;
private Assembly ass = null;
// private System.IO.Stream stream = null;
// private System.Media.SoundPlayer pl;
private string txt;
private byte[] cbs = new byte[512];
private byte[] sbs = new byte[512];
public Thread ThRec;
public Thread th1;
public Thread onFocus;
public int send;
public string recString; public void frmServer()
{
InitializeComponent();
ass = Assembly.GetExecutingAssembly();
//stream = ass.GetManifestResourceStream("msgServer.msg.wav");
// pl = new System.Media.SoundPlayer(stream);
} public void setItOnFocus()//发送窗口 获取焦点
{
rtxtSend.Focus();
} public Server()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)//加载事件
{
Control.CheckForIllegalCrossThreadCalls = false;
cobSelectSend.SelectedIndex = 0;
GetHostIp();
} private void GetHostIp()//获取本机IP
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); //取得本机IP
foreach (System.Net.IPAddress ip in ipEntry.AddressList)
{
txtIP.Text = ip.ToString();
myIP = IPAddress.Parse(txtIP.Text.Trim());
}
} private void btnFont_Click(object sender, EventArgs e)//字体按钮
{
ChooseFont(); } private void ChooseFont() //字体选择
{
FontDialog font = new FontDialog();
font.ShowEffects = true;
font.ShowColor = true;
font.MinSize = 12;
font.Font = new Font("楷体_GB2312", 18, FontStyle.Bold);
font.Color = Color.Blue; if (font.ShowDialog() == DialogResult.OK)
{
rtxtSend.Font = font.Font;
rtxtSend.ForeColor = font.Color;
}
} private void btnStart_Click(object sender, EventArgs e)//启动服务器按钮
{
th1 = new System.Threading.Thread(ToConnect);
th1.Start();
} private void ToConnect()//启动服务
{
try
{
//myIP = IPAddress.Parse(txtIP.Text.Trim());
point = Convert.ToInt32(txtPoint.Text.Trim());//Point
endPoint = new IPEndPoint(myIP, point); //终端 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(endPoint);//绑定
socket.Listen(3); //最多3人同时连接
lblStatus.Text = "服务器已经运行,等待连接..."; clientSocket = socket.Accept();//等待接受
//连接上时
if (clientSocket.Connected)
{
lblStatus.Text = "已经建立连接."; ThRec = new Thread(new ThreadStart(rec));
ThRec.Start();
ThRec.Abort();
}
}
catch (SocketException)
{
//txtIP.Focus(); txtIP.SelectAll();
MessageBox.Show("IP或者端口错误!");
}
catch (Exception e)
{
MessageBox.Show("Connect的Exception:" + e.ToString());
}
} private void btnStop_Click(object sender, EventArgs e)//停止监听
{
stopSoket();
} private void btnSend_Click(object sender, EventArgs e)//发送消息
{
if (rtxtSend.Text.Trim() == "")
return;
else if (rtxtSend.Text.Trim().ToLower() == "clear()")
{
rtxtMsg.Clear();
rtxtSend.Text = "";
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[zoom(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
int size = Convert.ToInt32(str.Substring(str.LastIndexOf('(') + 1,
str.IndexOf(')') - str.LastIndexOf('(') - 1));
rtxtMsg.Font = new Font("宋体", size, FontStyle.Bold);
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[down(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1); if (Convert.ToInt32(size) > rtxtMsg.Text.Length)
return; }
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[up(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1); }
else if (rtxtSend.Text.Trim().ToLower() == "exit(0)")
{
this.Close();
return;
} try
{
byte[] sbs = new byte[512];
string user = null;
if (txtUser.Text.Trim() == "在此输入你的名字" || txtUser.Text.Trim() == "")
{
sbs = null;
user = "我";
sbs = Encoding.Unicode.GetBytes(string.Format("对方 {0}\n{1}\n", DateTime.Now.ToString(), rtxtSend.Text.Trim()));
}
else
{
sbs = null;
sbs = Encoding.Unicode.GetBytes(string.Format("{0} {1}\n{2}\n", txtUser.Text.Trim(), DateTime.Now.ToString(), rtxtSend.Text.Trim()));
user = txtUser.Text.Trim();
} clientSocket.Send(sbs, sbs.Length, 0);//发送套接字 txt = string.Format("{0} {1}\n{2}\n", user, DateTime.Now.ToString(), rtxtSend.Text.Trim());//显示自己说的话
int tempLen = rtxtMsg.Text.Length;
rtxtMsg.AppendText(txt);
rtxtMsg.Select(tempLen, txt.Length);
rtxtMsg.SelectionFont = new Font("宋体", 10);
rtxtMsg.SelectionColor = Color.Green;
rtxtSend.Clear();
}
catch { MessageBox.Show("未建立连接,无法发送数据!"); }
} private void frmServer_FormClosing(object sender, FormClosingEventArgs e)//关闭窗体、进程
{
exitApp();
} private void frmServer_Load(object sender, EventArgs e)//加载事件,检测进程是否重复启动
{
checkAppStart();
topWinform.Checked = true;
} public void checkAppStart()//检查进程是否重复启动方法
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProductName)//已经启动
{
if (p.Id != Process.GetCurrentProcess().Id)//不是当前进程
{
p.Kill();
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;namespace Server2
{ public partial class Server : Form
{ private Socket socket;
private IPAddress myIP;
private IPEndPoint endPoint;
private int point;
private Socket clientSocket;
private Assembly ass = null;
// private System.IO.Stream stream = null;
// private System.Media.SoundPlayer pl;
private string txt;
private byte[] cbs = new byte[512];
private byte[] sbs = new byte[512];
public Thread ThRec;
public Thread th1;
public Thread onFocus;
public int send;
public string recString; public void frmServer()
{
InitializeComponent();
ass = Assembly.GetExecutingAssembly();
//stream = ass.GetManifestResourceStream("msgServer.msg.wav");
// pl = new System.Media.SoundPlayer(stream);
} public void setItOnFocus()//发送窗口 获取焦点
{
rtxtSend.Focus();
} public Server()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)//加载事件
{
Control.CheckForIllegalCrossThreadCalls = false;
cobSelectSend.SelectedIndex = 0;
GetHostIp();
} private void GetHostIp()//获取本机IP
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); //取得本机IP
foreach (System.Net.IPAddress ip in ipEntry.AddressList)
{
txtIP.Text = ip.ToString();
myIP = IPAddress.Parse(txtIP.Text.Trim());
}
} private void btnFont_Click(object sender, EventArgs e)//字体按钮
{
ChooseFont(); } private void ChooseFont() //字体选择
{
FontDialog font = new FontDialog();
font.ShowEffects = true;
font.ShowColor = true;
font.MinSize = 12;
font.Font = new Font("楷体_GB2312", 18, FontStyle.Bold);
font.Color = Color.Blue; if (font.ShowDialog() == DialogResult.OK)
{
rtxtSend.Font = font.Font;
rtxtSend.ForeColor = font.Color;
}
} private void btnStart_Click(object sender, EventArgs e)//启动服务器按钮
{
th1 = new System.Threading.Thread(ToConnect);
th1.Start();
} private void ToConnect()//启动服务
{
try
{
//myIP = IPAddress.Parse(txtIP.Text.Trim());
point = Convert.ToInt32(txtPoint.Text.Trim());//Point
endPoint = new IPEndPoint(myIP, point); //终端 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(endPoint);//绑定
socket.Listen(3); //最多3人同时连接
lblStatus.Text = "服务器已经运行,等待连接..."; clientSocket = socket.Accept();//等待接受
//连接上时
if (clientSocket.Connected)
{
lblStatus.Text = "已经建立连接."; ThRec = new Thread(new ThreadStart(rec));
ThRec.Start();
ThRec.Abort();
}
}
catch (SocketException)
{
//txtIP.Focus(); txtIP.SelectAll();
MessageBox.Show("IP或者端口错误!");
}
catch (Exception e)
{
MessageBox.Show("Connect的Exception:" + e.ToString());
}
} private void btnStop_Click(object sender, EventArgs e)//停止监听
{
stopSoket();
} private void btnSend_Click(object sender, EventArgs e)//发送消息
{
if (rtxtSend.Text.Trim() == "")
return;
else if (rtxtSend.Text.Trim().ToLower() == "clear()")
{
rtxtMsg.Clear();
rtxtSend.Text = "";
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[zoom(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
int size = Convert.ToInt32(str.Substring(str.LastIndexOf('(') + 1,
str.IndexOf(')') - str.LastIndexOf('(') - 1));
rtxtMsg.Font = new Font("宋体", size, FontStyle.Bold);
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[down(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1); if (Convert.ToInt32(size) > rtxtMsg.Text.Length)
return; }
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[up(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1); }
else if (rtxtSend.Text.Trim().ToLower() == "exit(0)")
{
this.Close();
return;
} try
{
byte[] sbs = new byte[512];
string user = null;
if (txtUser.Text.Trim() == "在此输入你的名字" || txtUser.Text.Trim() == "")
{
sbs = null;
user = "我";
sbs = Encoding.Unicode.GetBytes(string.Format("对方 {0}\n{1}\n", DateTime.Now.ToString(), rtxtSend.Text.Trim()));
}
else
{
sbs = null;
sbs = Encoding.Unicode.GetBytes(string.Format("{0} {1}\n{2}\n", txtUser.Text.Trim(), DateTime.Now.ToString(), rtxtSend.Text.Trim()));
user = txtUser.Text.Trim();
} clientSocket.Send(sbs, sbs.Length, 0);//发送套接字 txt = string.Format("{0} {1}\n{2}\n", user, DateTime.Now.ToString(), rtxtSend.Text.Trim());//显示自己说的话
int tempLen = rtxtMsg.Text.Length;
rtxtMsg.AppendText(txt);
rtxtMsg.Select(tempLen, txt.Length);
rtxtMsg.SelectionFont = new Font("宋体", 10);
rtxtMsg.SelectionColor = Color.Green;
rtxtSend.Clear();
}
catch { MessageBox.Show("未建立连接,无法发送数据!"); }
} private void frmServer_FormClosing(object sender, FormClosingEventArgs e)//关闭窗体、进程
{
exitApp();
} private void frmServer_Load(object sender, EventArgs e)//加载事件,检测进程是否重复启动
{
checkAppStart();
topWinform.Checked = true;
} public void checkAppStart()//检查进程是否重复启动方法
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProductName)//已经启动
{
if (p.Id != Process.GetCurrentProcess().Id)//不是当前进程
{
p.Kill();
}
}
}
}
解决方案 »
- URL地址参数获取某节点之后的所有字符串
- 极度崩溃,关于jquery中uploadify批量上传的问题,哪位达人救救我吧。
- C#的project在自编译的时候无法取得web的project内web.config的值
- [跪等]水晶报表页面提示:provider: 命名管道提供程序, error 40
- dataGridView中如何判断单元格中的值为数字还是字符类型?
- 正则表达式求救呀!!!!
- 高分请教数字证书的问题~~~
- WCF中Binding的Timeout和ReliableSession的讨论
- 如何用C#将序列化为json的字符串导入到sql server中数据库
- 老问题新想法:获取焦点问题????
- 如何用程序关闭WebBrowser中的网页(javascript)弹出的对话框
- C#如何使用Zlib实现压缩和解压缩? 我用的是vs2005...
{
this.TopMost = topWinform.Checked;
} public void rec()//接收数据 方法
{
try
{
while (true)
{
clientSocket.Receive(cbs, cbs.Length, 0);
recString = System.Text.Encoding.Unicode.GetString(cbs);
int tempLength = rtxtMsg.Text.Length;
rtxtMsg.Select(tempLength, recString.Length);
rtxtMsg.SelectionFont = new Font("宋体", 10);
rtxtMsg.SelectionColor = Color.Blue;
rtxtMsg.AppendText(recString);
Thread.Sleep(200);
} //pl.Play();
}
catch (SocketException)
{
//lblStatus.Text = "对不起,连接已经断开!";
stopSoket();
}
} public void stopSoket()//停止soket
{
try
{
socket.Close();
lblStatus.Text = "监听已经停止.";
}
catch
{
lblStatus.Text = "目前未建立连接.";
}
} private void Server_FormClosing(object sender, FormClosingEventArgs e)//FormClosing事件
{
exitApp();
} public void exitApp()//关闭进程
{
try
{
socket.Close(3);
}
catch (Exception) { }
finally
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProductName)
p.Kill();
}
}
} private void rtxtSend_KeyDown(object sender, KeyEventArgs e)//发送
{
selectSend(sender, e);
} private void selectSend(object sender, KeyEventArgs e)//判断cobSelectSend的值然后再选择调哪个发送方法
{
if (send == 0)
{
SendByEnter(sender, e);
}
else if (send == 1)
{
SendByAltAndEnter(sender, e);
}
else
{
MessageBox.Show("选择发送按钮错误,请重启程序!");
}
} private void SendByEnter(object sender, KeyEventArgs e)//用Enter键消息
{
if ((e.KeyData & Keys.Control) == Keys.Control &&
(e.KeyData & Keys.Enter) == Keys.Enter)
{
rtxtSend.AppendText("\n");
e.Handled = true; }
else if (e.KeyData == Keys.Enter)
{
//代码
e.Handled = true;
btnSend_Click(sender, e);
}
} private void SendByAltAndEnter(object sender, KeyEventArgs e)//用Alt+Enter键发送消息
{
if ((e.KeyData & Keys.Control) == Keys.Control &&
(e.KeyData & Keys.Enter) == Keys.Enter)
{
rtxtSend.AppendText("\n");
e.Handled = true;
btnSend_Click(sender, e);
}
else if (e.KeyData == Keys.Enter)
{
//代码
e.Handled = true; }
} private void cleanScreen_Click(object sender, EventArgs e)//接收窗口 清屏
{
rtxtMsg.Clear();
} private void copy_Click(object sender, EventArgs e)//接收窗口 复制
{
this.rtxtMsg.Copy();
} private void selectAll_Click(object sender, EventArgs e)//接收窗口 全选
{
this.rtxtMsg.SelectAll();
} private void copy2_Click(object sender, EventArgs e)//发送窗口 复制
{
rtxtSend.Copy();
} private void paste2_Click(object sender, EventArgs e)//发送窗口 粘贴
{
rtxtSend.Paste();
} private void cut_Click(object sender, EventArgs e)//发送窗口 剪切
{
rtxtSend.Cut();
} private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)//发送窗口 全选
{
this.rtxtSend.SelectAll();
} private void cobSelectSend_SelectedIndexChanged(object sender, EventArgs e)//选择用Enter或Alt+Enter键发送
{
if (cobSelectSend.SelectedIndex == 0)
{
send = 0;
}
else
{
send = 1;
}
} }
}
具体错误描述是:
//-----1-----//启动客户端后,客户端就死了。
//-----2-----//接收的数据偶尔会重复附加上一条或多条的数据。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;namespace Client2
{
public partial class Client : Form
{
private IPAddress IP;//ip
private EndPoint endpoint;//point
private Socket socket;//socket
//private System.IO.Stream stream = null;
private System.Reflection.Assembly ass = null;
// private System.Media.SoundPlayer pl = null;
private string txt;
private byte[] cbs = new byte[512];
private byte[] sbs = new byte[512];
public int send;
public string recString; public Client()
{
InitializeComponent();
ass = System.Reflection.Assembly.GetExecutingAssembly();
// stream = ass.GetManifestResourceStream("smgClient.msg.wav");
// pl = new System.Media.SoundPlayer(stream);
}
private void ToConnect()//连接服务器
{
try
{
IP = IPAddress.Parse(txtIP.Text.Trim());
int point = Convert.ToInt32(txtPoint.Text.Trim());
endpoint = new IPEndPoint(IP, point);
//建立Socket
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//建立连接 socket.Connect(endpoint);
//连接成功
lblStatus.Text = "连接成功!"; while (true)
{
try
{
Thread ThRecMsg = new Thread(new ThreadStart(recMsg));
ThRecMsg.Start();
ThRecMsg.Join(); }
catch (ThreadStartException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
catch (FormatException)
{ MessageBox.Show("IP error OR Point error"); }
catch (ArgumentNullException)
{ MessageBox.Show("IP error OR Point error"); }
catch (SocketException)
{
lblStatus.Text = "连接失败,原因: 服务器已停止.";
}
} private void btnFont_Click(object sender, EventArgs e) //字体
{
FontDialog font = new FontDialog();
font.ShowEffects = true;
font.ShowColor = true;
font.MinSize = 12;
font.Font = new Font("楷体_GB2312", 18, FontStyle.Bold);
font.Color = Color.Blue; if (font.ShowDialog() == DialogResult.OK)
{
rtxtSend.Font = font.Font;
rtxtSend.ForeColor = font.Color;
}
} private void frmClient_FormClosing(object sender, FormClosingEventArgs e)//窗体关闭
{
CloseForm();
} private void CloseForm()//进程关闭判断
{
try
{
socket.Close();
}
catch (Exception) { }
finally
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProductName)
p.Kill();
}
}
} private void checkBox1_CheckedChanged(object sender, EventArgs e)//置顶
{
this.TopMost = checkBox1.Checked;
} private void Form1_Load(object sender, EventArgs e)//判断系统进程唯一性
{ Control.CheckForIllegalCrossThreadCalls = false;
txtUser.Focus();
cobSelectSend.SelectedIndex = 0;
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); //取得本机IP
foreach (System.Net.IPAddress ip in ipEntry.AddressList)
{
txtIP.Text = ip.ToString();
IP = IPAddress.Parse(txtIP.Text.Trim());
} OpenNewToReplaceProcess();
} private static void OpenNewToReplaceProcess()//进程唯一性判断
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == Application.ProductName)//有进程
{
if (p.Id != Process.GetCurrentProcess().Id)//不是当前进程
{
p.Kill();
}
}
}
} private void button1_Click(object sender, EventArgs e)//连接按钮
{
try
{
ToConnect();
}
catch (Exception e2)
{ rtxtMsg.Text = "[" + e2.ToString() + "]";
} } private void btnSend_Click_1(object sender, EventArgs e)//发送消息按钮
{
if (rtxtSend.Text.Trim() == "")
return;
else if (rtxtSend.Text.Trim().ToLower() == "clear()")
{
rtxtMsg.Clear();
rtxtSend.Text = "";
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[zoom(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
int size = Convert.ToInt32(str.Substring(str.LastIndexOf('(') + 1,
str.IndexOf(')') - str.LastIndexOf('(') - 1));
rtxtMsg.Font = new Font("宋体", size, FontStyle.Bold);
return;
}
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[down(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1); if (Convert.ToInt32(size) > rtxtMsg.Text.Length)
return; }
else if (Regex.IsMatch(rtxtSend.Text.Trim().ToLower(), @"^[up(]+[\d]+[)]$"))
{
string str = rtxtSend.Text.ToLower();
string size = str.Substring(str.LastIndexOf('(') + 1, str.IndexOf(')') - str.LastIndexOf('(') - 1); }
else if (rtxtSend.Text.Trim().ToLower() == "exit(0)")
{
this.Close();
return;
} try
{
byte[] cbs = new byte[512];
string user = null;
if (txtUser.Text.Trim() == "在此输入你的名字" || txtUser.Text.Trim() == "")
{
cbs = null;
user = "我";
Encoding.Unicode.GetBytes(rtxtSend.Text.Trim());
cbs = Encoding.Unicode.GetBytes(string.Format("对方 {0}\n{1}\n", DateTime.Now.ToString(), rtxtSend.Text.Trim()));
}
else
{
cbs = null;
cbs = Encoding.Unicode.GetBytes(string.Format("{0} {1}\n{2}\n", txtUser.Text.Trim(), DateTime.Now.ToString(), rtxtSend.Text.Trim()));
user = txtUser.Text.Trim();
} socket.Send(cbs, cbs.Length, 0);//发送套接字
txt = string.Format("{0} {1}\n{2}\n", user, DateTime.Now.ToString(), rtxtSend.Text.Trim());//自己说的话
int tempLen = rtxtMsg.Text.Length;
rtxtMsg.AppendText(txt);//显示 自己说的话
rtxtMsg.Select(tempLen, txt.Length);
rtxtMsg.SelectionFont = new Font("宋体", 10);
rtxtMsg.SelectionColor = Color.Green;
rtxtSend.Clear();
}
catch (Exception ex)
{
MessageBox.Show("连接尚未建立!无法发送数据!" + ex.Message);
closeConnect();
}
}
private void btnCloseConnect_Click_1(object sender, EventArgs e)//关闭连接按钮
{
closeConnect();
} public void closeConnect()//关闭连接方法
{
try
{
socket.Close();
lblStatus.Text = "连接已经关闭.";
}
catch
{
lblStatus.Text = "目前未建立连接.";
}
} public void recMsg()//接收数据 方法
{ try
{
socket.Receive(sbs, sbs.Length, 0);
recString = System.Text.Encoding.Unicode.GetString(sbs);
int tempLength = rtxtMsg.Text.Length;
rtxtMsg.Select(tempLength, recString.Length);
rtxtMsg.SelectionFont = new Font("宋体", 10);
rtxtMsg.SelectionColor = Color.Blue;
rtxtMsg.AppendText(recString);
Thread.Sleep(200); }
catch (SocketException)
{
MessageBox.Show("对不起,服务器已经关闭!");
//Client.ActiveForm.Close();
} // pl.Play();
} private void checkBox1_CheckedChanged_1(object sender, EventArgs e)//窗口置顶?
{
this.TopMost = checkBox1.Checked;
} private void rtxtSend_KeyDown(object sender, KeyEventArgs e)//发送
{
SendMessage(sender, e); } private void SendMessage(object sender, KeyEventArgs e)//选择发送按键 调用
{
if (send == 0)
{
SendByEnter(sender, e);
}
else if (send == 1)
{
SendByAltAndEnter(sender, e);
}
else
{
MessageBox.Show("选择发送消息错误,请重启程序");
OpenNewToReplaceProcess();
}
} private void SendByEnter(object sender, KeyEventArgs e)//用Enter键发送
{
if ((e.KeyData & Keys.Control) == Keys.Control &&
(e.KeyData & Keys.Enter) == Keys.Enter)
{
rtxtSend.AppendText("\n");
e.Handled = true; }
else if (e.KeyData == Keys.Enter)
{
//代码
e.Handled = true;
btnSend_Click_1(sender, e);
}
} private void SendByAltAndEnter(object sender, KeyEventArgs e)//用Alt+Enter键发送
{
if ((e.KeyData & Keys.Control) == Keys.Control &&
(e.KeyData & Keys.Enter) == Keys.Enter)
{
rtxtSend.AppendText("\n");
e.Handled = true;
btnSend_Click_1(sender, e);
}
else if (e.KeyData == Keys.Enter)
{
//代码
e.Handled = true; }
} private void clean_Click(object sender, EventArgs e)//接收 清屏
{
this.rtxtMsg.Clear();
} private void copy_Click(object sender, EventArgs e)//接收 复制
{
this.rtxtMsg.Copy();
} private void selectAll_Click_1(object sender, EventArgs e)//接收 全选
{
this.rtxtMsg.SelectAll();
} private void copy2_Click(object sender, EventArgs e)//发送 复制
{
this.rtxtSend.Copy();
} private void cut2_Click(object sender, EventArgs e)//发送 剪切
{
this.rtxtSend.Cut();
} private void paste_Click(object sender, EventArgs e)//发送 粘贴
{
this.rtxtSend.Paste();
} private void selectAll_Click(object sender, EventArgs e)//发送 全选
{
this.rtxtSend.SelectAll();
} private void cobSelectSend_SelectedIndexChanged(object sender, EventArgs e)//判断哪个键发送
{
if (cobSelectSend.SelectedIndex == 0)
{
send = 0;
}
else
{
send = 1;
}
} private void timer1_Tick(object sender, EventArgs e)
{
recMsg();
}
private void txtUser_MouseClick(object sender, MouseEventArgs e)
{
this.txtUser.Focus();
this.txtUser.Text = "";
} private void button2_Click(object sender, EventArgs e)//接收按钮
{ } }
}