
CSharp中Socket网络编程(四)全部代码附UI图
监听功能:服务器启动并监听指定端口,等待客户端连接。 消息接收与处理:接收客户端发送的消息,并记录日志。 文件传输功能:支持客户端发送文件并保存到本地目录。 震动效果:当接收到特定消息类型时,客户端窗口产生震动效果。 连接到服务器并验证登录。 支持文本消息和文件传输功能。 处理特定消息类型的震动效果。 文件保存与路径选择。
发布日期:2021-05-08 04:51:54
浏览次数:22
分类:精选文章
本文共 6703 字,大约阅读时间需要 22 分钟。
C# 实现 TCP/IP 协议的服务器端与客户端
服务器端架构
服务器端采用 TCP 协议接收客户端连接,并支持消息传输和文件传输功能。代码主要包括以下几个部分:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Windows.Forms;namespace server{ public partial class Form1 : Form { private Socket socketWatch; private DictionarydicSocket; private Thread th; public Form1() { InitializeComponent(); dicSocket = new Dictionary (); } private void btnStart_Click(object sender, EventArgs e) { socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint port = new IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPort.Text)); socketWatch.Bind(port); socketWatch.Listen(10); ShowMsg("开始监听"); th = new Thread(Listen); th.IsBackground = true; th.Start(socketWatch); } private void Listen(object o) { socketWatch = o as Socket; try { while (true) { socketSend = socketWatch.Accept(); ShowMsg(socketSend.RemoteEndPoint.ToString() + ": 进入成功"); dicSocket.Add(socketSend.RemoteEndPoint.ToString(), socketSend); cboUsers.Items.Add(socketSend.RemoteEndPoint.ToString()); th = new Thread(Receive); th.IsBackground = true; th.Start(); } } catch { } } private void Receive() { try { while (true) { byte[] buffer = new byte[1024 * 1024 * 10]; int r = socketSend.Receive(buffer); if (r == 0) break; string str = Encoding.UTF8.GetString(buffer, 0, r); ShowMsg(socketSend.RemoteEndPoint.ToString() + ": " + str); } } catch { } } private void btnSend_Click(object sender, EventArgs e) { string str = txtMsg.Text; byte[] buffer = Encoding.UTF8.GetBytes(str); dicSocket[cboUsers.SelectedItem.ToString()].Send(buffer); } private void btnSelect_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = @"C:\Users\futurecloud\Desktop"; ofd.Title = "请选择你要传输的文件"; ofd.Filter = "所有文件|*.*"; ofd.ShowDialog(); txtPath.Text = ofd.FileName; } private void btnSendFile_Click(object sender, EventArgs e) { string path = txtPath.Text; using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 10]; int r = fsRead.Read(buffer, 0, buffer.Length); dicSocket[cboUsers.SelectedItem.ToString()].Send(buffer, 0, r + 1, SocketFlags.None); } } private void btnZD_Click(object sender, EventArgs e) { byte[] buffer = new byte[1]; buffer[0] = 2; dicSocket[cboUsers.SelectedItem.ToString()].Send(buffer); } }}
客户端架构
客户端主要功能包括:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace Client{ public partial class Form1 : Form { private Socket socketSend; private Thread th; public Form1() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint port = new IPEndPoint(IPAddress.Parse(txtServer.Text), Convert.ToInt32(txtPort.Text)); socketSend.Connect(port); ShowMsg("连通服务器"); th = new Thread(Receive); th.IsBackground = true; th.Start(); } private void Receive() { try { while (true) { byte[] buffer = new byte[1024 * 1024 * 10]; int r = socketSend.Receive(buffer); if (r == 0) break; if (buffer[0] == 0) { string str = Encoding.UTF8.GetString(buffer, 1, r - 1); ShowMsg(socketSend.RemoteEndPoint.ToString() + ": " + str); } else if (buffer[0] == 1) { SaveFileDialog sfd = new SaveFileDialog(); sfd.InitialDirectory = @"C:\Users\futurecloud\Desktop"; sfd.Title = "请选择你要保存的文件路径:"; sfd.Filter = "所有文件|*.*"; sfd.ShowDialog(this); string path = sfd.FileName; using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) { fsWrite.Write(buffer, 1, r - 1); } MessageBox.Show("文件写入成功"); } else if (buffer[0] == 2) { ZD(); } } } catch { } } private void ZD() { for (int i = 0; i < 500; i++) { this.Location = new Point(this.Location.X + 10, this.Location.Y + 10); this.Location = new Point(this.Location.X - 10, this.Location.Y - 10); } } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } }}
消息类型编码
本篇介绍了服务器端和客户端的消息传输机制,支持以下消息类型编码:
- 消息类型 0:文本消息
- 消息类型 1:文件内容
- 消息类型 2:震动效果
文件传输功能
客户端可通过选择本地文件路径上传文件到服务器,并保存到指定目录。服务器端接收文件内容后,将其写入本地文件中。
总结
以上代码实现了一个基于 TCP/IP 协议的通信系统,支持消息传输和文件传输功能。通过合理配置端口号和服务器地址,可以实现跨平台的网络通信。
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年04月11日 09时44分48秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
云计算之路-阿里云上:0:25~0:40网络存储故障造成网站不能正常访问
2019-03-06
网站故障公告1:使用阿里云RDS之后一个让人欲哭无泪的下午
2019-03-06
上周热点回顾(12.31-1.6)
2019-03-06
上周热点回顾(1.21-1.27)
2019-03-06
上周热点回顾(6.3-6.9)
2019-03-06
上周热点回顾(8.12-8.18)
2019-03-06
【故障公告】升级阿里云 RDS SQL Server 实例故障经过
2019-03-06
蹒跚来迟:新版博客后台上线公测
2019-03-06
上周热点回顾(9.16-9.22)
2019-03-06
上周热点回顾(11.4-11.10)
2019-03-06
[网站公告]11月26日00:00-04:00阿里云RDS升级
2019-03-06
[网站公告]又拍云API故障造成图片无法上传(已恢复)
2019-03-06
上周热点回顾(12.16-12.22)
2019-03-06
云计算之路-阿里云上:对“黑色30秒”问题的猜想
2021-05-09
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了
2021-05-09
云计算之路-阿里云上:奇怪的CPU 100%问题
2021-05-09
云计算之路-阿里云上:2014年6月12日12点IIS请求到达量突降
2021-05-09
上周热点回顾(6.9-6.15)
2021-05-09
上周热点回顾(6.16-6.22)
2021-05-09
上周热点回顾(6.23-6.29)
2021-05-09