C# 简单的聊天通信
来源:互联网 发布:java教程pdf下载 编辑:程序博客网 时间:2024/04/29 03:31
服务器:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;namespace Servers{ public partial class Server : Form { public Server() { InitializeComponent(); //关闭对文本框的非法线程操作检查 TextBox.CheckForIllegalCrossThreadCalls = false;//还未懂! } private void Form1_Load(object sender, EventArgs e) { } Thread threadWatch = null; Socket socketWatch = null; private void Serverconn_Click(object sender, EventArgs e) { socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(txtIP.Text.Trim()); int port = Convert.ToInt32(txtPORT.Text.Trim()); IPEndPoint ipe = new IPEndPoint(ip, port); socketWatch.Bind(ipe); socketWatch.Listen(10); 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 ServerRecMsg(object obj) { Socket socketServer = obj as Socket; while (true) { byte[] RecvBytes = new byte[1024 * 1024]; int length = socketServer.Receive(RecvBytes); string RecvStr = Encoding.UTF8.GetString(RecvBytes, 0, length); txtMsg.AppendText("ZXY: " + GetTime() + "\r\n" + RecvStr + "\r\n"); } } private void SendMsg_Click(object sender, EventArgs e) { ServerSendMsg(txtSend.Text.Trim()); } private void ServerSendMsg(string SendStr) { byte[] SendString = Encoding.UTF8.GetBytes(SendStr); socConnection.Send(SendString); txtMsg.AppendText("PYT: " + GetTime() + "\r\n" + SendStr + "\r\n"); } private void txtSend_TextChanged(object sender, EventArgs e) { } //Enter快捷键,光有代码不行,还需要将KeyDown属性设置一下! private void txtSend_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { ServerSendMsg(txtSend.Text.Trim()); } } //获取当前的时间 private DateTime GetTime() { DateTime nowTime = new DateTime(); nowTime = DateTime.Now; return nowTime; } }}
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;namespace Clients{ public partial class Client : Form { public Client() { InitializeComponent(); TextBox.CheckForIllegalCrossThreadCalls = false; } private void Client_Load(object sender, EventArgs e) { } Socket socketClient = null; Thread threadClient = null; private void beginListen_Click(object sender, EventArgs e) { socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(txtIP.Text.Trim()); int port = Convert.ToInt32(txtPORT.Text.Trim()); IPEndPoint ipe = new IPEndPoint(ip, port); socketClient.Connect(ipe); threadClient = new Thread(RecvMsg); threadClient.IsBackground = true; threadClient.Start(); } private void RecvMsg() { while(true) { byte[] arrRecMsg = new byte[1024 * 1024]; int length = socketClient.Receive(arrRecMsg); string strRecMsg = Encoding.UTF8.GetString(arrRecMsg, 0, length); txtMsg.AppendText("PYT: " + GetTime() + "\r\n" + strRecMsg + "\r\n"); } } private void ClientSendMsg(string sendMsg) { byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(sendMsg); socketClient.Send(arrClientSendMsg); txtMsg.AppendText("ZXY: " + GetTime() + "\r\n" + sendMsg + "\r\n"); } private void Send_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 GetTime() { DateTime nowTime = new DateTime(); nowTime = DateTime.Now; return nowTime; } }}
0 0
- C# 简单的聊天通信
- C# SOCKET编写的简单聊天通信程序(客户端+服务端)
- 一个简单的socket通信聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#编写简单的聊天程序
- C#实现简单聊天程序的方法
- C#简单聊天程序
- C# Socket实现聊天通信
- Jedis使用
- 面试题——Session的get方法 与 load方法区别
- Android 循环录像,保留加锁视频
- Oracle 过程中的select into 中查询记录数不是1导致异常的解决办法
- 在CentOS上MySQL数据库服务器配置方法
- C# 简单的聊天通信
- Failed to connect to /XXXfor block, add to deadNodes and continue. java.net.NoRouteToHostException:
- Spring+Hibernate 零散知识点
- 笔试题引发的思考--装箱与拆箱
- 适当采用白盒测试
- Socket心跳包
- GCD--我的理解
- 同时发起多个intent
- ios swift开发中,使用键盘,tableview根据键盘调整高度