赞
踩
网上的TCP/IP创建服务端和客户端的方法鱼龙混杂,自己把在使用项目中的TCP服务端和客户端的代码抽了出来,做了demo,以供大家使用参考。
为了方便调用,我把一些基础方法都整合封装到了Reader.dll中,直接调用方法即可,非常方便(伸手党直接下源码运行即可)
源码下载地址:https://download.csdn.net/download/horseroll/10444188
无积分付费下载地址:https://download.csdn.net/download/HorseRoll/21113276
源码中包含dll文件,直接下载源码即可
dll文件下载地址:https://download.csdn.net/download/horseroll/10444167
无积分付费下载地址:https://download.csdn.net/download/HorseRoll/21113250
源码程序运行使用方法:先打开服务端,再打开客户端,然后即可互相发送信息
效果图:
客户端连接方法使用readerCamera.ConnectServer方法,监听事件为ReceiveDataCamera,发送信息为readerCamera.ServerSendMessage方法
- public partial class Client : Form
- {
- private Reader.ReaderMethod readerCamera;//摄像头采集类
- public Client()
- {
- InitializeComponent();
- //初始化访问实例
- readerCamera = new Reader.ReaderMethod();
- //回调函数
- readerCamera.ReceiveCallback = ReceiveDataCamera;
- }
-
- private void btnopenclient_Click(object sender, EventArgs e)
- {
- string strLog = string.Empty;
- string strException = string.Empty;
- IPAddress ipAddress = IPAddress.Parse("127.0.0.1");//监听的ip地址
- int nPort = 55;//监听的端口号
- int nRet = readerCamera.ConnectServer(ipAddress, nPort, out strException);
- if (nRet != 0)//如果连接摄像头失败
- {
- strLog = "连接摄像头失败,请确认是否已打开服务端,失败原因: " + strException;
- }
- else
- {
- strLog = "成功连接摄像头"+ipAddress+":"+nPort;
- }
- WriteLog(richTextBox1, strLog);
- }
- /// <summary>
- /// 客户端接收数据
- /// </summary>
- /// <param name="btAryReceiveData"></param>
- private void ReceiveDataCamera(byte[] btAryReceiveData)
- {
- string str = System.Text.Encoding.Default.GetString(btAryReceiveData);//数据接收转string
- //richTextBox1.AppendText("接收到数据:" + str);直接调用该方法会出现跨线程调用问题
- WriteLog(richTextBox1, "接收到数据:" + str);
- }
-
- private void btnclientsend_Click(object sender, EventArgs e)//发送数据
- {
- readerCamera.ServerSendMessage(System.Text.Encoding.Default.GetBytes(tbclient.Text));//发送数据
- }
- }
服务端连接方法使用readerCameraOpenServer方法,监听事件为ReceiveDataCamera,发送信息为readerCamera.ServerSendMessage方法
- public partial class Server : Form
- {
- private Reader.ReaderMethod readerCamera;//摄像头采集
- public Server()
- {
- InitializeComponent();
- Client client = new Client();
- client.Show();
- //初始化访问读写器实例
- readerCamera = new Reader.ReaderMethod();
- //回调函数
- readerCamera.ReceiveCallback = ReceiveDataCamera;
- }
- private void btnopenserver_Click(object sender, EventArgs e)
- {
- readerCamera.OpenServer(55);//打开服务端,监听端口55
- WriteLog(richTextBox1,"打开端口55成功");//点击事件中调用该写入方法不会报错
- }
- /// <summary>
- /// 服务器接收数据
- /// </summary>
- /// <param name="btAryReceiveData"></param>
- private void ReceiveDataCamera(byte[] btAryReceiveData)
- {
- string str = System.Text.Encoding.Default.GetString(btAryReceiveData);//数据接收转string
- //richTextBox1.AppendText("接收到数据:" + str);直接调用该方法会出现跨线程调用问题
- WriteLog(richTextBox1,"接收到数据:" + str);
- }
- /// <summary>
- /// 发消息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnserversend_Click(object sender, EventArgs e)
- {
- readerCamera.ServerSendMessage(System.Text.Encoding.Default.GetBytes(tbserver.Text));//发送数据
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。