Socket TCP 完整实例
2020-08-04 17:03
796 查看
(提示:CSDN资源下载会自动增长积分,本来自己写的Socket.Helper 想免费分享一下,结果积分一直在涨,所以把实例发出来。遇到问题可以直接留言)
Soket 基本操作:
1、socket()函数
2、bind()函数
3、listen()、connect()函数
4、accept()函数
5、read()、write()函数等
6、close()函数
一 客户端:
using Server; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; namespace Client { public partial class Client : Form { public Client() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; SocketConnection(); } Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1024); //socket连接服务端 public void SocketConnection() { client.Connect(point); ShowMsg("连接成功!"); Thread th = new Thread(Receive); th.IsBackground = true; th.Start(); } //接收数据 void Receive() { while (true) { byte[] buff = new byte[1024 * 1024]; int count = client.Receive(buff); string resuit = Encoding.ASCII.GetString(buff, 0, count); ShowMsg(client.RemoteEndPoint.ToString() + ":" + resuit); } } //数据展示 void ShowMsg(string msg) { txtListen.AppendText(msg + "\r\n"); } //数据发送 private void btnSend_Click(object sender, EventArgs e) { byte[] buff = new byte[1024 * 1024]; buff = Encoding.ASCII.GetBytes(txtSend.Text); client.Send(buff); } } }
二 服务端:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; 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 Server { public partial class btnDic : Form { public btnDic() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { timer1.Start(); Control.CheckForIllegalCrossThreadCalls = false; SocketConnection(); } public static Dictionary<string, Socket> dic = new Dictionary<string, Socket>(); public void SocketConnection() { Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1024); server.Bind(point); server.Listen(10); ShowMsg("开始监听......"); Thread th = new Thread(Listen); th.IsBackground = true; th.Start(server); } void ShowMsg(string msg) { txtListen.AppendText(msg + "\r\n"); } void Listen(object o) { Socket server = o as Socket; while (true) { Socket client = server.Accept(); string point = client.RemoteEndPoint.ToString(); ShowMsg(point + ":连接成功!"); dic[point] = client; GetTreeViewData(); Thread th = new Thread(Receive); th.IsBackground = true; th.Start(client); } } void Receive(object o) { Socket server = o as Socket; while (true) { byte[] buff = new byte[1024 * 1024]; int count = server.Receive(buff); string resuit = Encoding.ASCII.GetString(buff, 0, count); ShowMsg(server.RemoteEndPoint + ":" + resuit); } } private void btnSend_Click(object sender, EventArgs e) { dic[treeView1.SelectedNode.Text].Send(Encoding.ASCII.GetBytes(txtSend.Text)); } private void timer1_Tick(object sender, EventArgs e) { } private void huoq_Click(object sender, EventArgs e) { GetTreeViewData(); } private void GetTreeViewData() { if (this.InvokeRequired) { CreateNodeDelegate createNodeDelegate = new CreateNodeDelegate(CreateNode); this.Invoke(createNodeDelegate); } } public delegate void CreateNodeDelegate(); private void CreateNode() { treeView1.Nodes.Clear(); foreach (var item in dic) { TreeNode Tn = new TreeNode(); Tn.Text = item.Key; treeView1.Nodes.Add(Tn); } } } }
三 界面样式:
多个客户端连接服务器时 ,“获取连接”可以得到连接列表
四 实现实例预览:
相关文章推荐
- python网络编程之TCP通信实例和socketserver框架使用例子
- socket 接口测试实例 tcp连接 Python 2.7
- 02.4 socket实例_tcp的client与sever
- VC++使用socket进行TCP、UDP通信实例总结
- Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例
- python网络编程之TCP通信实例和socketserver框架使用例子
- java学习之路——基于TCP的Socket网络通信实例
- socket TCP编程模型实例1
- Socket编程(六)---TCP/NIO实例讲解
- Linux 下socket通信终极指南(附TCP、UDP完整代码)
- unix环境IPC通信之socket(三),tcp采用并发服务器编程实例,多进程方式 4000
- Socket TCP【简单聊天实例】
- socket编程技巧(1)tcp接收接口(变长数据定长数据)的编写实例
- Linux下TCP Socket编程C语言小实例
- windows Socket + tcp 简单实例
- socket 编程入门教程(一)TCP server 端:8、本章的完整源代码
- Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例
- Linux C Socket TCP编程介绍及实例
- socket 编程入门教程(一)TCP server 端:8、本章的完整源代码
- Linux C Socket TCP编程介绍及实例