资源简介
C#基于TCP的Socket多线程通信(包含服务端和客户端)
代码都有注释,方便新手阅读
![](http://www.nz998.com/pic/41964.jpg)
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace IOTClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender EventArgs e)
{
}
Socket sokClient = null;//负责与服务端通信的套接字
Thread threadClient = null;//负责 监听 服务端发送来的消息的线程
bool isRec = true;//是否循环接收服务端数据
private void btnConnect_Click(object sender EventArgs e)
{
//实例化 套接字
sokClient = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
//创建 ip对象
IPAddress address = IPAddress.Parse(txtIP.Text.Trim());
//创建网络节点对象 包含 ip和port
IPEndPoint endpoint = new IPEndPoint(address int.Parse(txtPort.Text.Trim()));
//连接 服务端监听套接字
sokClient.Connect(endpoint);
//创建负责接收 服务端发送来数据的 线程
threadClient = new Thread(ReceiveMsg);
threadClient.IsBackground = true;
//如果在win7下要通过 某个线程 来调用 文件选择框的代码,就需要设置如下
threadClient.SetApartmentState(ApartmentState.STA);
threadClient.Start();
}
///
/// 接收服务端发送来的消息数据
///
void ReceiveMsg()
{
while (isRec)
{
byte[] msgArr = new byte[1024 * 1024 * 1];//接收到的消息的缓冲区
int length = 0;
//接收服务端发送来的消息数据
length = sokClient.Receive(msgArr);//Receive会阻断线程
if (msgArr[0] == 0)//发送来的是文字
{
string strMsg = System.Text.Encoding.UTF8.GetString(msgArr 1 length - 1);
txtShow.AppendText(strMsg + “\r\n“);
}
else if (msgArr[0] == 1)
{ //发送来的是文件
SaveFileDialog sfd = new SaveFileDialog();
//弹出文件保存选择框
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//创建文件流
using (FileStream fs = new FileStream(sfd.FileName FileMode.OpenOrCreate))
{
fs.Write(msgArr 1 length - 1);
MessageBox.Show(“文件保存成功!“);
}
}
}
else if (msgArr[0] == 2)
{
ShakeWindow();
}
}
}
///
/// 闪屏
///
private void ShakeWindow()
{
Rand
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 20480 2012-04-11 20:39 IOTClient\IOTClient\bin\Debug\IOTClient.exe
文件 22016 2012-04-11 20:39 IOTClient\IOTClient\bin\Debug\IOTClient.pdb
文件 5632 2005-12-08 14:51 IOTClient\IOTClient\bin\Debug\IOTClient.vshost.exe
文件 4194 2012-04-11 20:39 IOTClient\IOTClient\Form1.cs
文件 5578 2012-04-11 20:39 IOTClient\IOTClient\Form1.Designer.cs
文件 5814 2012-04-11 20:39 IOTClient\IOTClient\Form1.resx
文件 3225 2012-04-11 20:22 IOTClient\IOTClient\IOTClient.csproj
文件 842 2012-04-11 20:39 IOTClient\IOTClient\obj\Debug\IOTClient.csproj.GenerateResource.Cache
文件 20480 2012-04-11 20:39 IOTClient\IOTClient\obj\Debug\IOTClient.exe
文件 180 2012-04-11 20:39 IOTClient\IOTClient\obj\Debug\IOTClient.Form1.resources
文件 22016 2012-04-11 20:39 IOTClient\IOTClient\obj\Debug\IOTClient.pdb
文件 180 2012-04-11 20:22 IOTClient\IOTClient\obj\Debug\IOTClient.Properties.Resources.resources
文件 282 2012-04-11 20:50 IOTClient\IOTClient\obj\IOTClient.csproj.FileList.txt
文件 468 2012-04-11 20:17 IOTClient\IOTClient\Program.cs
文件 1190 2012-04-11 20:17 IOTClient\IOTClient\Properties\AssemblyInfo.cs
文件 2872 2012-04-11 20:17 IOTClient\IOTClient\Properties\Resources.Designer.cs
文件 5612 2012-04-11 20:17 IOTClient\IOTClient\Properties\Resources.resx
文件 1092 2012-04-11 20:17 IOTClient\IOTClient\Properties\Settings.Designer.cs
文件 249 2012-04-11 20:17 IOTClient\IOTClient\Properties\Settings.settings
文件 916 2012-04-11 20:17 IOTClient\IOTClient.sln
..A..H. 13312 2012-04-11 20:56 IOTClient\IOTClient.suo
文件 24576 2012-04-11 20:49 IOTServer\IOTServer\bin\Debug\IOTServer.exe
文件 28160 2012-04-11 20:49 IOTServer\IOTServer\bin\Debug\IOTServer.pdb
文件 5632 2005-12-08 14:51 IOTServer\IOTServer\bin\Debug\IOTServer.vshost.exe
文件 4830 2012-04-11 20:12 IOTServer\IOTServer\ConnectionClient.cs
文件 6054 2012-04-11 20:48 IOTServer\IOTServer\IOTServer.cs
文件 3292 2012-04-11 20:00 IOTServer\IOTServer\IOTServer.csproj
文件 8079 2012-04-11 20:49 IOTServer\IOTServer\IOTServer.Designer.cs
文件 5814 2012-04-11 20:48 IOTServer\IOTServer\IOTServer.resx
文件 904 2012-04-11 20:48 IOTServer\IOTServer\obj\Debug\IOTServer.csproj.GenerateResource.Cache
............此处省略34个文件信息
相关资源
- “三打一”扑克牌游戏设计
- C# C/s项目源码
- 基于GMap.ne控件的C/S模式地图开发程序
- C#考试管理系统C/S
- Winform C# C/S 程序 html 富文本编辑器
- C/S 程序设计 三层架构 学生信息管理
- C#.net实现的C/S版酒店管理系统
- 在线考试系统 C# C/S 优秀毕业设计 含
- C#网络对战五子棋c/s源程序
- 基于C/S架构的学生选课系统(C#)
- c/s系统框架
- 用C#做的库存管理系统
- 美萍餐饮管理系统,C#,C/S,WinForm,
- 企业人事管理系统(C#C/S模式,程序,
- 基于C/S框架、利用三层架构、C#开发的
- 数据库课程设计,一个c/s系统一个b
- C/S开发框架源码 winform快速开发框架
- 餐饮管理系统
- [C#]SignalR实现扫码登录(B/SC/S)源码
- c#超市收银和货物管理系统
- asp.net+c#学生信息管理系统C/S程序+源代
- 考试管理系统(C#源码 C/S架构)
- 图书馆管理系统-软件工程课大作业
- C#开发的游戏服务器demoC/S案例,可运
- C# C/S平台软件自动更新程序 SQLServer
- C# 财务管理系统 源码 C/s 构架
- C# remoting 聊天室
- C/S模式的TCP点对点聊天程序C#源码
- 基于c/s模式的简单聊天程序
- 员工信息管理系统C#源码
评论
共有 条评论