AI智能
改变未来

Socket TCP 完整实例

(提示: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);}}}}

三 界面样式:

多个客户端连接服务器时 ,“获取连接”可以得到连接列表


四 实现实例预览:

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Socket TCP 完整实例