资源简介
本代码基于c#语言实现tcp双工通信,分为tcp客户端和tcp服务器端两部分,内含可执行文件。

代码片段和文件信息
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 System.Threading;
using System.IO;
namespace tcp客户端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
}
Thread threadClient = null; // 创建用于接收服务端消息的 线程;
Socket sockClient = null;
private void btnConnect_Click(object sender EventArgs e)
{
IPAddress ip = IPAddress.Parse(txtIp.Text.Trim());
IPEndPoint endPoint = new IPEndPoint(ip int.Parse(txtPort.Text.Trim()));
sockClient = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
try
{
ShowMsg(“与服务器连接中……“);
sockClient.Connect(endPoint);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
return;
//this.Close();
}
ShowMsg(“与服务器连接成功!!!“);
threadClient = new Thread(RecMsg);
threadClient.IsBackground = true;
threadClient.Start();
}
public delegate void DeleUpdateTextbox(string dataRe);
void RecMsg()
{
DeleUpdateTextbox deleupdatetextbox = new DeleUpdateTextbox(UpdateTextbox);
byte[] arrMsgRec = new byte[1024 * 1024 * 512];
while (true)
{
// 定义一个2M的缓存区;
// 将接受到的数据存入到输入 arrMsgRec中;
int length = -1;
try
{
length = sockClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
arrMsgRec[length] = 0;
}
catch (SocketException se)
{
ShowMsg(“异常;“ + se.Message);
return;
}
catch (Exception e)
{
ShowMsg(“异常:“ + e.Message);
return;
}
if (arrMsgRec[0] == 0x30) // 表示接收到的是消息数据;
{
string strMsg; //System.Text.Encoding.Default.GetString(arrMsgRec 1 length - 1);// 将接受到的字节数据转化成字符串;
//ShowMsg(strMsg);
strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec 1 length - 1);
strMsg = “他说:\r\n“ + strMsg;
ShowMsg(strMsg);
}
if (arrMsgRec[0] == 0x31) // 表示接收到的是文件数据;
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-04-20 20:24 tcp通信\
目录 0 2018-05-01 20:31 tcp通信\tcp服务器\
文件 11206 2018-04-27 22:09 tcp通信\tcp服务器\Form1.cs
文件 8731 2018-04-27 22:09 tcp通信\tcp服务器\Form1.Designer.cs
文件 5817 2018-04-27 22:09 tcp通信\tcp服务器\Form1.resx
文件 484 2018-04-17 22:27 tcp通信\tcp服务器\Program.cs
文件 14336 2018-04-27 22:09 tcp通信\tcp服务器\tcp.exe
文件 851 2018-04-20 20:22 tcp通信\tcp服务器\tcp.sln
文件 24576 2018-04-27 22:09 tcp通信\tcp服务器\tcp.suo
目录 0 2018-05-01 20:31 tcp通信\tcp客户端\
文件 7897 2018-04-20 20:21 tcp通信\tcp客户端\Form1.cs
文件 8829 2018-04-20 20:21 tcp通信\tcp客户端\Form1.Designer.cs
文件 5817 2018-04-20 20:21 tcp通信\tcp客户端\Form1.resx
文件 493 2018-04-17 22:40 tcp通信\tcp客户端\Program.cs
文件 13824 2018-04-27 22:04 tcp通信\tcp客户端\tcp客户端.exe
文件 878 2018-04-20 20:22 tcp通信\tcp客户端\tcp客户端.sln
文件 24576 2018-04-27 22:09 tcp通信\tcp客户端\tcp客户端.suo
- 上一篇:快速设计一个简单的WPF串口上位机
- 下一篇:大地测量中国地质大学七参数求解
相关资源
- 考试管理系统 - C#源码
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
- C# 使用ListView控件实现图片浏览器(源
评论
共有 条评论