C#网络编程TCP通信实例程序简单设计
C#网络编程TCP通信实例程序简单设计
采用自带 TcpClient和TcpListener设计一个Tcp通信的例子
只实现了TCP通信
通信程序截图:
压力测试服务端截图:
俩个客户端链接服务端测试截图:
服务端:
客户端
运行动态图
C#程序设计代码
BenXHSocket.dll主要代码设计
SocketObject类
/*****************************************************
* ProjectName: BenXHSocket
* Description:
* ClassName: SocketObject
* CLRVersion: 4.0.30319.18408
* Author: JiYF
* NameSpace: BenXHSocket
* MachineName: JIYONGFEI
* CreateTime: 2017/3/31 12:13:06
* UpdatedTime: 2017/3/31 12:13:06
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net; namespace BenXHSocket
{
/// <summary>
/// Socket基础类
/// </summary>
public abstract class SocketObject
{
/// <summary>
/// 初始化Socket方法
/// </summary>
/// <param name="ipAddress"></param>
/// <param name="port"></param>
public abstract void InitSocket(IPAddress ipAddress,int port);
public abstract void InitSocket(string ipAddress,int port); /// <summary>
/// Socket启动方法
/// </summary>
public abstract void Start(); /// <summary>
/// Sockdet停止方法
/// </summary>
public abstract void Stop(); }
}
sockets类
/*****************************************************
* ProjectName: BenXHSocket
* Description:
* ClassName: Sockets
* CLRVersion: 4.0.30319.18408
* Author: JiYF
* NameSpace: BenXHSocket
* MachineName: JIYONGFEI
* CreateTime: 2017/3/31 12:16:10
* UpdatedTime: 2017/3/31 12:16:10
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets; namespace BenXHSocket
{
public class Sockets
{
/// <summary>
/// 接收缓冲区大小8k
/// </summary>
public byte[] RecBuffer = new byte[ * ]; /// <summary>
/// 发送缓冲区大小8k
/// </summary>
public byte[] SendBuffer = new byte[ * ]; /// <summary>
/// 异步接收后包的大小
/// </summary>
public int Offset { get;set;} /// <summary>
/// 当前IP地址,端口号
/// </summary>
public IPEndPoint Ip { get; set; }
/// <summary>
/// 客户端主通信程序
/// </summary>
public TcpClient Client { get; set; }
/// <summary>
/// 承载客户端Socket的网络流
/// </summary>
public NetworkStream nStream { get; set; } /// <summary>
/// 发生异常时不为null.
/// </summary>
public Exception ex { get; set; } /// <summary>
/// 新客户端标识.如果推送器发现此标识为true,那么认为是客户端上线
/// 仅服务端有效
/// </summary>
public bool NewClientFlag { get; set; } /// <summary>
/// 客户端退出标识.如果服务端发现此标识为true,那么认为客户端下线
/// 客户端接收此标识时,认为客户端异常.
/// </summary>
public bool ClientDispose { get; set; } /// <summary>
/// 空参构造
/// </summary>
public Sockets() { } /// <summary>
/// 构造函数
/// </summary>
/// <param name="ip">ip节点</param>
/// <param name="client">TCPClient客户端</param>
/// <param name="ns">NetworkStream </param>
public Sockets(IPEndPoint ip,TcpClient client,NetworkStream ns)
{
this.Ip = ip;
this.Client = client;
this.nStream = ns;
}
}
}
SocketsHandler类
/*****************************************************
* ProjectName: BenXHSocket
* Description:
* ClassName: SocketsHandler
* CLRVersion: 4.0.30319.18408
* Author: JiYF
* NameSpace: BenXHSocket
* MachineName: JIYONGFEI
* CreateTime: 2017/3/31 13:42:48
* UpdatedTime: 2017/3/31 13:42:48
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BenXHSocket
{
/// <summary>
/// 推送器
/// </summary>
/// <param name="sockets"></param>
public delegate void PushSockets(Sockets sockets); }
BXHTcpServer类 tcp服务端监听类
/*****************************************************
* ProjectName: BenXHSocket
* Description:
* ClassName: TcpServer
* CLRVersion: 4.0.30319.18408
* Author: JiYF
* NameSpace: BenXHSocket
* MachineName: JIYONGFEI
* CreateTime: 2017/3/31 12:24:08
* UpdatedTime: 2017/3/31 12:24:08
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets; namespace BenXHSocket
{
/// <summary>
/// TCPServer类 服务端程序
/// </summary>
public class BXHTcpServer:SocketObject
{
private bool IsStop = false;
object obj = new object();
public static PushSockets pushSockets; /// <summary>
/// 信号量
/// </summary>
private Semaphore semap = new Semaphore(,); /// <summary>
/// 客户端列表集合
/// </summary>
public List<Sockets> ClientList = new List<Sockets>(); /// <summary>
/// 服务端实例对象
/// </summary>
public TcpListener Listener; /// <summary>
/// 当前的ip地址
/// </summary>
private IPAddress IpAddress; /// <summary>
/// 初始化消息
/// </summary>
private string InitMsg = "JiYF笨小孩TCP服务端"; /// <summary>
/// 监听的端口
/// </summary>
private int Port; /// <summary>
/// 当前ip和端口节点对象
/// </summary>
private IPEndPoint Ip; /// <summary>
/// 初始化服务器对象
/// </summary>
/// <param name="ipAddress">IP地址</param>
/// <param name="port">端口号</param>
public override void InitSocket(IPAddress ipAddress, int port)
{
this.IpAddress = ipAddress;
this.Port = port;
this.Listener = new TcpListener(IpAddress,Port);
} /// <summary>
/// 初始化服务器对象
/// </summary>
/// <param name="ipAddress"></param>
/// <param name="port"></param>
public override void InitSocket(string ipAddress, int port)
{
this.IpAddress = IPAddress.Parse(ipAddress);
this.Port = port;
this.Ip = new IPEndPoint(IpAddress,Port);
this.Listener = new TcpListener(IpAddress,Port);
} /// <summary>
/// 服务端启动监听,处理链接
/// </summary>
public override void Start()
{
try
{
Listener.Start();
Thread Accth = new Thread(new ThreadStart(
delegate
{
while(true)
{
if(IsStop != false)
{
break;
}
this.GetAcceptTcpClient();
Thread.Sleep();
}
}
));
Accth.Start();
}
catch(SocketException skex)
{
Sockets sks = new Sockets();
sks.ex = skex;
pushSockets.Invoke(sks);
}
} /// <summary>
/// 获取处理新的链接请求
/// </summary>
private void GetAcceptTcpClient()
{
try
{
if(Listener.Pending())
{
semap.WaitOne();
//接收到挂起的客户端请求链接
TcpClient tcpClient = Listener.AcceptTcpClient(); //维护处理客户端队列
Socket socket = tcpClient.Client;
NetworkStream stream = new NetworkStream(socket,true);
Sockets sks = new Sockets(tcpClient.Client.RemoteEndPoint as IPEndPoint,tcpClient,stream);
sks.NewClientFlag = true; //推送新的客户端连接信息
pushSockets.Invoke(sks); //客户端异步接收数据
sks.nStream.BeginRead(sks.RecBuffer,,sks.RecBuffer.Length,new AsyncCallback(EndReader),sks); //加入客户端队列
this.AddClientList(sks); //链接成功后主动向客户端发送一条消息
if(stream.CanWrite)
{
byte[] buffer = Encoding.UTF8.GetBytes(this.InitMsg);
stream.Write(buffer, , buffer.Length);
}
semap.Release();
}
}
catch
{
return;
}
} /// <summary>
/// 异步接收发送的的信息
/// </summary>
/// <param name="ir"></param>
private void EndReader(IAsyncResult ir)
{
Sockets sks = ir.AsyncState as Sockets;
if (sks != null && Listener != null)
{
try
{
if (sks.NewClientFlag || sks.Offset != )
{
sks.NewClientFlag = false;
sks.Offset = sks.nStream.EndRead(ir);
//推送到UI
pushSockets.Invoke(sks);
sks.nStream.BeginRead(sks.RecBuffer,,sks.RecBuffer.Length,new AsyncCallback(EndReader),sks);
}
}
catch(Exception skex)
{
lock (obj)
{
ClientList.Remove(sks);
Sockets sk = sks;
//标记客户端退出程序
sk.ClientDispose = true;
sk.ex = skex;
//推送至UI
pushSockets.Invoke(sks);
}
}
}
} /// <summary>
/// 客户端加入队列
/// </summary>
/// <param name="sk"></param>
private void AddClientList(Sockets sk)
{
lock (obj)
{
Sockets sockets = ClientList.Find(o => { return o.Ip == sk.Ip; });
if (sockets == null)
{
ClientList.Add(sk);
}
else
{
ClientList.Remove(sockets);
ClientList.Add(sk);
}
}
} /// <summary>
/// 服务端停止监听
/// </summary>
public override void Stop()
{
if (Listener != null)
{
Listener.Stop();
Listener = null;
IsStop = true;
pushSockets = null;
}
} /// <summary>
/// 向所有在线客户端发送消息
/// </summary>
/// <param name="SendData">消息内容</param>
public void SendToAll(string SendData)
{
for (int i = ; i < ClientList.Count; i++)
{
SendToClient(ClientList[i].Ip, SendData);
}
} /// <summary>
/// 向单独的一个客户端发送消息
/// </summary>
/// <param name="ip"></param>
/// <param name="SendData"></param>
public void SendToClient(IPEndPoint ip,string SendData)
{
try
{
Sockets sks = ClientList.Find(o => { return o.Ip == ip; });
if(sks == null || !sks.Client.Connected)
{
Sockets ks = new Sockets();
//标识客户端下线
sks.ClientDispose = true;
sks.ex = new Exception("客户端没有连接");
pushSockets.Invoke(sks);
}
if(sks.Client.Connected)
{
//获取当前流进行写入.
NetworkStream nStream = sks.nStream;
if (nStream.CanWrite)
{
byte[] buffer = Encoding.UTF8.GetBytes(SendData);
nStream.Write(buffer, , buffer.Length);
}
else
{
//避免流被关闭,重新从对象中获取流
nStream = sks.Client.GetStream();
if (nStream.CanWrite)
{
byte[] buffer = Encoding.UTF8.GetBytes(SendData);
nStream.Write(buffer, , buffer.Length);
}
else
{
//如果还是无法写入,那么认为客户端中断连接.
ClientList.Remove(sks);
Sockets ks = new Sockets();
sks.ClientDispose = true;//如果出现异常,标识客户端下线
sks.ex = new Exception("客户端无连接");
pushSockets.Invoke(sks);//推送至UI }
}
}
}
catch(Exception skex)
{
Sockets sks = new Sockets();
sks.ClientDispose = true;//如果出现异常,标识客户端退出
sks.ex = skex;
pushSockets.Invoke(sks);//推送至UI
}
}
}
}
BXHTcpClient 类 Tcp客户端类
/*****************************************************
* ProjectName: BenXHSocket
* Description:
* ClassName: BxhTcpClient
* CLRVersion: 4.0.30319.42000
* Author: JiYF
* NameSpace: BenXHSocket
* MachineName: JIYF_PC
* CreateTime: 2017/3/31 20:31:48
* UpdatedTime: 2017/3/31 20:31:48
*****************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading; namespace BenXHSocket
{
public class BXHTcpClient : SocketObject
{
bool IsClose = false; /// <summary>
/// 当前管理对象
/// </summary>
Sockets sk; /// <summary>
/// 客户端
/// </summary>
public TcpClient client; /// <summary>
/// 当前连接服务端地址
/// </summary>
IPAddress Ipaddress; /// <summary>
/// 当前连接服务端端口号
/// </summary>
int Port; /// <summary>
/// 服务端IP+端口
/// </summary>
IPEndPoint ip; /// <summary>
/// 发送与接收使用的流
/// </summary>
NetworkStream nStream; /// <summary>
/// 初始化Socket
/// </summary>
/// <param name="ipaddress"></param>
/// <param name="port"></param>
public override void InitSocket(string ipaddress, int port)
{
Ipaddress = IPAddress.Parse(ipaddress);
Port = port;
ip = new IPEndPoint(Ipaddress, Port);
client = new TcpClient();
} public static PushSockets pushSockets;
public void SendData(string SendData)
{
try
{ if (client == null || !client.Connected)
{
Sockets sks = new Sockets();
sks.ex = new Exception("客户端无连接..");
sks.ClientDispose = true; pushSockets.Invoke(sks);//推送至UI
}
if (client.Connected) //如果连接则发送
{
if (nStream == null)
{
nStream = client.GetStream();
}
byte[] buffer = Encoding.UTF8.GetBytes(SendData);
nStream.Write(buffer, , buffer.Length);
}
}
catch (Exception skex)
{
Sockets sks = new Sockets();
sks.ex = skex;
sks.ClientDispose = true;
pushSockets.Invoke(sks);//推送至UI
}
}
/// <summary>
/// 初始化Socket
/// </summary>
/// <param name="ipaddress"></param>
/// <param name="port"></param>
public override void InitSocket(IPAddress ipaddress, int port)
{
Ipaddress = ipaddress;
Port = port;
ip = new IPEndPoint(Ipaddress, Port);
client = new TcpClient();
}
private void Connect()
{
client.Connect(ip);
nStream = new NetworkStream(client.Client, true);
sk = new Sockets(ip, client, nStream);
sk.nStream.BeginRead(sk.RecBuffer, , sk.RecBuffer.Length, new AsyncCallback(EndReader), sk);
}
private void EndReader(IAsyncResult ir)
{ Sockets s = ir.AsyncState as Sockets;
try
{
if (s != null)
{ if (IsClose && client == null)
{
sk.nStream.Close();
sk.nStream.Dispose();
return;
}
s.Offset = s.nStream.EndRead(ir);
pushSockets.Invoke(s);//推送至UI
sk.nStream.BeginRead(sk.RecBuffer, , sk.RecBuffer.Length, new AsyncCallback(EndReader), sk);
}
}
catch (Exception skex)
{
Sockets sks = s;
sks.ex = skex;
sks.ClientDispose = true;
pushSockets.Invoke(sks);//推送至UI } }
/// <summary>
/// 重写Start方法,其实就是连接服务端
/// </summary>
public override void Start()
{
Connect();
}
public override void Stop()
{
Sockets sks = new Sockets();
if (client != null)
{
client.Client.Shutdown(SocketShutdown.Both);
Thread.Sleep();
client.Close();
IsClose = true;
client = null;
}
else
{
sks.ex = new Exception("客户端没有初始化.!");
}
pushSockets.Invoke(sks);//推送至UI
} }
}
服务端和客户端窗体应用程序主要方法设计实现
服务端窗体应用程序主要方法代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Net;
using BenXHSocket;
using System.Threading; namespace BenXHSocketTcpServer
{
public partial class FrmTCPServer : Form
{
private static string serverIP;
private static int port;
object obj = new object();
private int sendInt = ;
private static Dictionary<TreeNode, IPEndPoint> DicTreeIPEndPoint = new Dictionary<TreeNode, IPEndPoint>(); public FrmTCPServer()
{
InitializeComponent(); serverIP = ConfigurationManager.AppSettings["ServerIP"];
port = int.Parse(ConfigurationManager.AppSettings["ServerPort"]);
Control.CheckForIllegalCrossThreadCalls = false;
init();
} private void init()
{
treeViewClientList.Nodes.Clear();
TreeNode tn = new TreeNode();
tn.Name = "ClientList";
tn.Text = "客户端列表";
tn.ImageIndex = ;
tn.ContextMenuStrip = contextMenuStripClientAll;
treeViewClientList.Nodes.Add(tn);
DicTreeIPEndPoint.Clear(); //自已绘制
this.treeViewClientList.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.treeViewClientList.DrawNode += new DrawTreeNodeEventHandler(treeViewClientList_DrawNode);
} private BenXHSocket.BXHTcpServer tcpServer; /// <summary>
/// 绘制颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeViewClientList_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.DrawDefault = true; //我这里用默认颜色即可,只需要在TreeView失去焦点时选中节点仍然突显
return;
//or 自定义颜色
if ((e.State & TreeNodeStates.Selected) != )
{
//演示为绿底白字
e.Graphics.FillRectangle(Brushes.DarkBlue, e.Node.Bounds); Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(e.Bounds, , ));
}
else
{
e.DrawDefault = true;
} if ((e.State & TreeNodeStates.Focused) != )
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = e.Node.Bounds;
focusBounds.Size = new Size(focusBounds.Width - ,
focusBounds.Height - );
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
} } /// <summary>
/// 开启服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartServerToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (serverIP != null && serverIP != "" && port != null && port >= )
{
tcpServer.InitSocket(IPAddress.Parse(serverIP), port);
tcpServer.Start();
listBoxServerInfo.Items.Add(string.Format("{0}服务端程序监听启动成功!监听:{1}:{2}",DateTime.Now.ToString(), serverIP, port.ToString()));
StartServerToolStripMenuItem.Enabled = false;
} }
catch(Exception ex)
{
listBoxServerInfo.Items.Add(string.Format("服务器启动失败!原因:{0}",ex.Message));
StartServerToolStripMenuItem.Enabled = true;
}
} /// <summary>
/// 停止服务监听
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StopServerToolStripMenuItem_Click(object sender, EventArgs e)
{
tcpServer.Stop();
listBoxServerInfo.Items.Add("服务器程序停止成功!");
StartServerToolStripMenuItem.Enabled = true; } private void FrmTCPServer_Load(object sender, EventArgs e)
{
if(tcpServer ==null)
listBoxServerInfo.Items.Add(string.Format("服务端监听程序尚未开启!{0}:{1}",serverIP,port));
treeViewClientList.ExpandAll();
BXHTcpServer.pushSockets = new PushSockets(Rev);
tcpServer = new BXHTcpServer();
} /// <summary>
/// 处理接收到客户端的请求和数据
/// </summary>
/// <param name="sks"></param>
private void Rev(BenXHSocket.Sockets sks)
{
this.Invoke(new ThreadStart(
delegate
{
if (treeViewClientList.Nodes[] != null)
{ } if (sks.ex != null)
{
if (sks.ClientDispose)
{
listBoxServerInfo.Items.Add(string.Format("{0}客户端:{1}下线!",DateTime.Now.ToString(), sks.Ip));
if (treeViewClientList.Nodes[].Nodes.ContainsKey(sks.Ip.ToString()))
{
if (DicTreeIPEndPoint.Count != )
{
removTreeIPEndPoint(sks.Ip);
treeViewClientList.Nodes[].Nodes.RemoveByKey(sks.Ip.ToString()); toolStripStatusLabelClientNum.Text = (int.Parse(toolStripStatusLabelClientNum.Text) - ).ToString();//treeViewClientList.Nodes[0].Nodes.Count.ToString(); } }
}
listBoxServerInfo.Items.Add(sks.ex.Message);
}
else
{
if (sks.NewClientFlag)
{
listBoxServerInfo.Items.Add(string.Format("{0}新的客户端:{0}链接成功",DateTime.Now.ToString(), sks.Ip)); TreeNode tn = new TreeNode();
tn.Name = sks.Ip.ToString();
tn.Text = sks.Ip.ToString();
tn.ContextMenuStrip = contextMenuStripClientSingle;
tn.Tag = "客户端";
tn.ImageIndex = ; treeViewClientList.Nodes[].Nodes.Add(tn); //treeview节点和IPEndPoint绑定
DicTreeIPEndPoint.Add(tn,sks.Ip); if (treeViewClientList.Nodes[].Nodes.Count > )
{
treeViewClientList.ExpandAll();
}
toolStripStatusLabelClientNum.Text = (int.Parse(toolStripStatusLabelClientNum.Text)+).ToString();
}
else if (sks.Offset == )
{
listBoxServerInfo.Items.Add(string.Format("{0}客户端:{1}下线.!",DateTime.Now.ToString(), sks.Ip));
if (treeViewClientList.Nodes[].Nodes.ContainsKey(sks.Ip.ToString()))
{
if (DicTreeIPEndPoint.Count != )
{
removTreeIPEndPoint(sks.Ip);
treeViewClientList.Nodes[].Nodes.RemoveByKey(sks.Ip.ToString()); toolStripStatusLabelClientNum.Text = (int.Parse(toolStripStatusLabelClientNum.Text) - ).ToString(); }
}
}
else
{
byte[] buffer = new byte[sks.Offset];
Array.Copy(sks.RecBuffer, buffer, sks.Offset);
string str = Encoding.UTF8.GetString(buffer);
listBox1.Items.Add(string.Format("{0}客户端{1}发来消息:{2}",DateTime.Now.ToString(), sks.Ip, str));
}
}
}
)
);
} /// <summary>
/// 关闭程序钱停止服务器实例
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmTCPServer_FormClosing(object sender, FormClosingEventArgs e)
{
tcpServer.Stop();
} private void treeViewClientList_AfterSelect(object sender, TreeViewEventArgs e)
{
//
} private void treeViewClientList_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
treeViewClientList.Focus();
treeViewClientList.SelectedNode = treeViewClientList.GetNodeAt(e.X,e.Y);
} } private void toolStripMenuSendSingle_Click(object sender, EventArgs e)
{
if (treeViewClientList.SelectedNode != null)
{
tcpServer.SendToClient(DicTreeIPEndPoint[treeViewClientList.SelectedNode], string.Format("服务端单个消息...{0}", sendInt.ToString()));
sendInt++;
}
} private void toolStripMenuSendAll_Click(object sender, EventArgs e)
{
tcpServer.SendToAll("服务端全部发送消息..." + sendInt);
sendInt++;
} private void removTreeIPEndPoint(IPEndPoint ipendPoint)
{ if (DicTreeIPEndPoint.Count <= ) return;
//foreach遍历Dictionary时候不能对字典进行Remove
TreeNode[] keys = new TreeNode[DicTreeIPEndPoint.Count];
DicTreeIPEndPoint.Keys.CopyTo(keys,);
lock (obj)
{
foreach (TreeNode item in keys)
{
if (DicTreeIPEndPoint[item] == ipendPoint)
{
DicTreeIPEndPoint.Remove(item);
}
}
}
} }
}
客户端窗体应用程序主要代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using BenXHSocket;
using System.Threading; namespace BenXHSocketClient
{
public partial class FrmTCPClient : Form
{
BXHTcpClient tcpClient; string ip = string.Empty;
string port = string.Empty;
private int sendInt = ;
public FrmTCPClient()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void btnConnServer_Click(object sender, EventArgs e)
{
try
{
this.ip = txtServerIP.Text.Trim();
this.port = txtServerPort.Text.Trim(); tcpClient.InitSocket(ip, int.Parse(port));
tcpClient.Start();
listBoxStates.Items.Add("连接成功!");
btnConnServer.Enabled = false; }
catch (Exception ex)
{ listBoxStates.Items.Add(string.Format("连接失败!原因:{0}", ex.Message));
btnConnServer.Enabled = true;
}
} private void FrmTCPClient_Load(object sender, EventArgs e)
{
//客户端如何处理异常等信息参照服务端 BXHTcpClient.pushSockets = new PushSockets(Rec); tcpClient = new BXHTcpClient();
this.ip = txtServerIP.Text.Trim();
this.port = txtServerPort.Text.Trim(); } /// <summary>
/// 处理推送过来的消息
/// </summary>
/// <param name="rec"></param>
private void Rec(BenXHSocket.Sockets sks)
{
this.Invoke(new ThreadStart(delegate
{
if (listBoxText.Items.Count > )
{
listBoxText.Items.Clear();
}
if (sks.ex != null)
{
if (sks.ClientDispose == true)
{
//由于未知原因引发异常.导致客户端下线. 比如网络故障.或服务器断开连接.
listBoxStates.Items.Add(string.Format("客户端下线.!"));
}
listBoxStates.Items.Add(string.Format("异常消息:{0}", sks.ex));
}
else if (sks.Offset == )
{
//客户端主动下线
listBoxStates.Items.Add(string.Format("客户端下线.!"));
}
else
{
byte[] buffer = new byte[sks.Offset];
Array.Copy(sks.RecBuffer, buffer, sks.Offset);
string str = Encoding.UTF8.GetString(buffer);
listBoxText.Items.Add(string.Format("服务端{0}发来消息:{1}", sks.Ip, str));
}
}));
} private void btnDisConn_Click(object sender, EventArgs e)
{
tcpClient.Stop();
btnConnServer.Enabled = true;
} private void btnSendData_Click(object sender, EventArgs e)
{
tcpClient.SendData("客户端消息!" + sendInt);
sendInt++; } private void btnConnTest_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(o =>
{
for (int i = ; i < ; i++)
{
BenXHSocket.BXHTcpClient clientx= new BenXHSocket.BXHTcpClient();//初始化类库
clientx.InitSocket(ip, int.Parse(port));
clientx.Start();
}
MessageBox.Show("完成.!");
});
} }
}
运行程序下载:
BenXHSocket程序下载
源代码工程文件下载
内容:
BenXHSocket.dll 主要程序动态库
BenXHSocketClient.exe 客户端应用程序
BenXHSocketTcpServer.exe 服务端应用程序
BenXHSocketTcpServer.exe.config 服务端应用程序配置文件
其中:BenXHSocketTcpServer.exe.config为配置文件,可以设置监听的ip地址和端口号,默认ip地址:127.0.0.1 默认的端口号:4455
C#网络编程TCP通信实例程序简单设计的更多相关文章
- 第5章-unix网络编程 TCP/服务端程序示例
这一章主要是完成一个完整的tcp客户/服务器程序.通过一很简单的例子.弄清客户和服务器如何启动,如何终止,发生了某些错误会发生什么.这些事很重要的 客户端代码 #include "unp. ...
- UNIX网络编程---TCP客户/服务器程序示例(五)
一.概述 客户从标准输入读入一行文本,并写给服务器 服务器从网络输入读入这行文本,并回射给客户 客户从网络输入读入这行回射文本,并显示在标准输出上 二.TCP回射服务器程序:main函数 这里给了函数 ...
- JAVA网络编程TCP通信
Socket简介: Socket称为"套接字",描述IP地址和端口.在Internet上的主机一般运行多个服务软件,同时提供几种服务,每种服务都打开一个Socket,并绑定在一个端 ...
- Socket网络编程(TCP/IP/端口/类)和实例
Socket网络编程(TCP/IP/端口/类)和实例 原文:C# Socket网络编程精华篇 转自:微冷的雨 我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: TCP/IP层次 ...
- 网络编程:Http通信与Socket通信
http://note.youdao.com/share/?id=f14d304548003f65e34255d3ddf9df31&type=note 网络编程:Http通信与Socket通信 ...
- 网络编程TCP/IP详解
网络编程TCP/IP详解 1. 网络通信 中继器:信号放大器 集线器(hub):是中继器的一种形式,区别在于集线器能够提供多端口服务,多口中继器,每个数据包的发送都是以广播的形式进行的,容易阻塞网络. ...
- Java网络编程UDP通信原理
前言 继续今天我们的Java网络编程--TCP和UDP通信 一.TCP和UDP概述 传输层通常以TCP和UDP协议来控制端点与端点的通信 TCP UDP 协议名称 传输控制协议 用户数据包协议 是 ...
- 36 - 网络编程-TCP编程
目录 1 概述 2 TCP/IP协议基础 3 TCP编程 3.1 通信流程 3.2 构建服务端 3.3 构建客户端 3.4 常用方法 3.4.1 makefile方法 3.5 socket交互 3.4 ...
- 网络编程——TCP协议、UDP协议、socket套接字、粘包问题以及解决方法
网络编程--TCP协议.UDP协议.socket套接字.粘包问题以及解决方法 TCP协议(流式协议) 当应用程序想通过TCP协议实现远程通信时,彼此之间必须先建立双向通信通道,基于该双向通道实现数 ...
随机推荐
- Python——pyiso8601
该模块不是Python内建的模块,为Python补充了 ISO 8601 解析——将常见的 ISO 8601 日期字符创转化为 Python 的 datetime 对象. 安装 $ pip insta ...
- mysql的字符串函数
From: http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/12/13/1904330.html 对于针对字符串位置的操作,第一个位置被标 ...
- iOS:ODRefreshControl
https://github.com/Sephiroth87/ODRefreshControl Important note if your project doesn’t use ARC: you ...
- 浅谈Linux系统中如何查看进程
进程是一个其中运行着一个或多个线程的地址空间和这些线程所需要的系统资源.一般来说,Linux系统会在进程之间共享程序代码和系统函数库,所以在任何时刻内存中都只有代码的一份拷贝. 1,ps命令 作用:p ...
- MySql数据库恢复(*frm)文件
mysql数据库恢复(*frm)文件 WorkBench 在使用虚拟服务器时,服务器提供商一般不会像我们使用本地数据库一样:使用导入导出(这样的文件后缀是*.sql).大部分时候提供的是一个文件夹,里 ...
- UNIX环境编程学习笔记(13)——文件I/O之标准I/O流
lienhua342014-09-29 1 标准 I/O 流 之前学习的都是不带缓冲的 I/O 操作函数,直接针对文件描述符的,每调用一次函数可能都会触发一次系统调用,单次调用可能比较快捷.但是,对于 ...
- lua中实现倒计时
今天在开发的时候,涉及到了使用倒计时来显示. 首先自己的思路是: 1.设计显示的Lable. 2.对传入的时间进行处理,转成字符串00:00:00.通过调用回调函数来控制一秒刷新一次. 转换算法: h ...
- BarTender中每个标签提示手动输入的设置方法
我们知道手动输入数据进行标签打印,可以利用BarTender中的数据输入表单来实现.表单的创建有利于提示程序员,输入要打印标签的的数据.如果手动输入数据的标签较多,可以设置表单提示为每个标签提示,减去 ...
- Android开发学习笔记-自定义组合控件
为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...
- Three-js 创建第一个3D场景
1.一个场景至少需要的三种类型组件 相机/决定哪些东西将在屏幕上渲染 光源/他们会对材质如何显示,以及生成阴影时材质如何使用产生影响 物体/他们是在相机透视图里主要的渲染队形:方块.球体等 ...