程序简介
本聊天程序支持局域网内部客户端与服务端之间的互相通信.
原理
启动服务端后,服务端通过持续监听客户端发来的请求,一旦监听到客户端传来的信息后,两端便可以互发信息了.服务端需要绑定一个IP,用于客户端在网络中寻找并建立连接.信息发送原理:将手动输入字符串信息转换成机器可以识别的字节数组,然后调用套接字的Send()方法将字节数组发送出去.信息接收原理:调用套接字的Receive()方法,获取对端传来的字节数组,然后将其转换成人可以读懂的字符串信息.
界面设计 - *服务端*
IP文本框 name: txtIP port(端口号)文本框 name: txtPORT 聊天内容文本框 name: txtMsg 发送信息文本框 name:txtSendMsg
启动服务按钮 name: btnServerConn 发送信息按钮name: btnSendMsg
服务端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| public partial class FServer : Form { public FServer() { InitializeComponent(); TextBox.CheckForIllegalCrossThreadCalls = false; }
Thread threadWatch = null; Socket socketWatch = null;
private void btnServerConn_Click(object sender, EventArgs e) { socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipaddress = IPAddress.Parse(txtIP.Text.Trim()); IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(txtPORT.Text.Trim())); socketWatch.Bind(endpoint); socketWatch.Listen(20); threadWatch = new Thread(WatchConnecting); threadWatch.IsBackground = true; threadWatch.Start(); txtMsg.AppendText("开始监听客户端传来的信息!" + "\r\n");
}
Socket socConnection = null;
private void WatchConnecting() { while (true) { socConnection = socketWatch.Accept(); txtMsg.AppendText("客户端连接成功" + "\r\n"); ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg); Thread thr = new Thread(pts); thr.IsBackground = true; thr.Start(socConnection); } }
private void ServerSendMsg(string sendMsg) { byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg); socConnection.Send(arrSendMsg); txtMsg.AppendText("So-flash:" + GetCurrentTime() + "\r\n" + sendMsg + "\r\n"); }
private void ServerRecMsg(object socketClientPara) { Socket socketServer = socketClientPara as Socket; while (true) { byte[] arrServerRecMsg = new byte[1024 * 1024]; int length = socketServer.Receive(arrServerRecMsg); string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length); txtMsg.AppendText("天之涯:" + GetCurrentTime() + "\r\n" + strSRecMsg + "\r\n"); } }
private void btnSendMsg_Click(object sender, EventArgs e) { ServerSendMsg(txtSendMsg.Text.Trim()); }
private void txtSendMsg_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { ServerSendMsg(txtSendMsg.Text.Trim()); } }
private DateTime GetCurrentTime() { DateTime currentTime = new DateTime(); currentTime = DateTime.Now; return currentTime; }
|
界面设计 - 客户端
IP文本框 name: txtIP Port文本框 name: txtPort 聊天内容文本框 name:txtMsg 发送信息文本框 name: txtCMsg
连接到服务端按钮 name: btnBeginListen 发送消息按钮 name: btnSend
客户端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| public partial class FClient : Form { public FClient() { InitializeComponent(); TextBox.CheckForIllegalCrossThreadCalls = false; } Socket socketClient = null; Thread threadClient = null;
private void btnBeginListen_Click(object sender, EventArgs e) { socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipaddress = IPAddress.Parse(txtIP.Text.Trim()); IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(txtPort.Text.Trim())); socketClient.Connect(endpoint); threadClient = new Thread(RecMsg); threadClient.IsBackground = true; threadClient.Start(); }
private void RecMsg() { while (true) { byte[] arrRecMsg = new byte[1024 * 1024]; int length = socketClient.Receive(arrRecMsg); string strRecMsg = Encoding.UTF8.GetString(arrRecMsg, 0, length); txtMsg.AppendText("So-flash:" + GetCurrentTime() + "\r\n" + strRecMsg + "\r\n"); } }
private void ClientSendMsg(string sendMsg) { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(sendMsg); socketClient.Send(arrClientSendMsg); txtMsg.AppendText("天之涯:" + GetCurrentTime() + "\r\n" + sendMsg + "\r\n"); }
private void btnSend_Click(object sender, EventArgs e) { ClientSendMsg(txtCMsg.Text.Trim()); }
private void txtCMsg_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { ClientSendMsg(txtCMsg.Text.Trim()); } }
private DateTime GetCurrentTime() { DateTime currentTime = new DateTime(); currentTime = DateTime.Now; return currentTime; }
|
运行方法
获取电脑本机IP的方法: 例如:本机IP:192.168.0.3(可能变动) 端口号port可以随便写:1-65535之间的任意整数都行
1.打开程序 点击运行
2.在运行栏里输入cmd指令
3.输入查看IP指令: ipconfig
4.获取当前IP: 192.168.0.3. 当然不同的地方 本机IP有可能不一样
程序运行展示:
首先 点击服务端的 启动服务按钮 聊天内容出现”开始监听客户端传来的信息!”
然后 点击客户端上的”连接到服务端”按钮 可以看见服务端上又出现了一行字 “客户端连接成功”
之后 便可以 两端进行通信了
这样一个简单的聊天程序就完成了~~~~:)
源代码下载
客户端下载 ChatClient.zip 服务端下载 ChatServer.zip
转自:https://www.cnblogs.com/longwu/archive/2011/08/25/2153636.html