资源简介
c# TCP与UDP 的服务器与客户端通讯的实例,代码中有客户端与服务器端,并且还扩展了服务器与客户端之间的通讯加密算法。

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Security.Cryptography;
using Client;
namespace Myclient
{
public partial class Client : Form
{
Socket socketsend;//发送消息的socket
IPAddress ip;//ip
int port;//端口
static Socket client;//UDP
private RSACryptoServiceProvider rsa;
private byte[] SAIV;//密钥
private string SAKey;//密钥
public Client()
{
InitializeComponent();
tcpradioButton.Checked = true;
}
private void Form1_Load(object sender EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;//取消跨线程检查,避免界面假死
}
private void okbutton_Click(object sender EventArgs e)
{
//连接服务器
//TCP通讯
if (tcpradioButton.Checked)
{
try
{
ip = IPAddress.Parse(iptextBox.Text.Trim());
port = Convert.ToInt32(porttextBox.Text);
socketsend = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(ip port);
socketsend.Connect(point);
ShowMsg(“连接成功!“);
sendPublicKey();//发送客户端的加密密钥
getSymmetricKey();//获取服务器的加密密钥
Thread th = new Thread(AcceptMessage);
th.IsBackground = true;
th.Start();
}
catch
{ }
}
//UDP通讯
else
{
client = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);
client.Bind(new IPEndPoint(IPAddress.Parse(“127.0.0.1“) 6000));
Thread t2 = new Thread(ReciveMsg);
t2.Start();
ShowMsg(“连接成功!“);
}
}
///
/// 向特定ip的主机的端口发送数据报
///
void sendMsg()
{
EndPoint point = new IPEndPoint(IPAddress.Parse(“127.0.0.1“) 6001);
string msg =textBox2.Text.Trim();
client.SendTo(Encoding.UTF8.GetBytes(msg) point);
}
///
/// 接收发送给本机ip对应端口号的数据报
///
void ReciveMsg()
{
while (true)
{
EndPoint point = new IPEndPoint(IPAddress.Any 0);//用来保存发送方的ip和端口号
byte[] buffer = new byte[1024];
int length = client.ReceiveFrom(buffer ref point);//接收数据报
string m
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-10-26 20:34 MyServer and Myclient\
目录 0 2018-01-08 14:36 MyServer and Myclient\Myclient\
文件 187 2016-10-22 17:29 MyServer and Myclient\Myclient\App.config
目录 0 2016-10-24 16:51 MyServer and Myclient\Myclient\bin\
目录 0 2018-01-08 15:01 MyServer and Myclient\Myclient\bin\Debug\
文件 15360 2018-01-08 16:22 MyServer and Myclient\Myclient\bin\Debug\Myclient.exe
文件 187 2016-10-22 17:29 MyServer and Myclient\Myclient\bin\Debug\Myclient.exe.config
文件 38400 2018-01-08 16:22 MyServer and Myclient\Myclient\bin\Debug\Myclient.pdb
文件 22472 2018-01-08 17:29 MyServer and Myclient\Myclient\bin\Debug\Myclient.vshost.exe
文件 187 2016-10-22 17:29 MyServer and Myclient\Myclient\bin\Debug\Myclient.vshost.exe.config
目录 0 2016-10-24 16:51 MyServer and Myclient\Myclient\bin\Release\
文件 7985 2018-01-08 16:22 MyServer and Myclient\Myclient\Client.cs
文件 8842 2018-01-08 15:17 MyServer and Myclient\Myclient\Client.Designer.cs
文件 5817 2018-01-08 15:29 MyServer and Myclient\Myclient\Client.resx
文件 5236 2018-01-08 14:43 MyServer and Myclient\Myclient\Myclient.csproj
文件 452 2016-10-24 16:42 MyServer and Myclient\Myclient\Myclient.csproj.user
目录 0 2016-10-22 17:29 MyServer and Myclient\Myclient\obj\
目录 0 2018-01-08 16:22 MyServer and Myclient\Myclient\obj\Debug\
文件 1451 2018-01-08 14:15 MyServer and Myclient\Myclient\obj\Debug\DesignTimeResolveAssemblyReferences.cache
文件 7028 2018-01-08 14:36 MyServer and Myclient\Myclient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 180 2018-01-08 15:42 MyServer and Myclient\Myclient\obj\Debug\Myclient.Client.resources
文件 1674 2018-01-08 17:29 MyServer and Myclient\Myclient\obj\Debug\Myclient.csproj.FileListAbsolute.txt
文件 976 2018-01-08 15:42 MyServer and Myclient\Myclient\obj\Debug\Myclient.csproj.GenerateResource.Cache
文件 2209 2018-01-08 15:01 MyServer and Myclient\Myclient\obj\Debug\Myclient.csprojResolveAssemblyReference.cache
文件 15360 2018-01-08 16:22 MyServer and Myclient\Myclient\obj\Debug\Myclient.exe
文件 38400 2018-01-08 16:22 MyServer and Myclient\Myclient\obj\Debug\Myclient.pdb
文件 180 2018-01-08 15:01 MyServer and Myclient\Myclient\obj\Debug\Myclient.Properties.Resources.resources
文件 0 2016-10-22 17:29 MyServer and Myclient\Myclient\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2016-10-22 17:29 MyServer and Myclient\Myclient\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2016-10-22 17:29 MyServer and Myclient\Myclient\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
目录 0 2016-10-22 17:29 MyServer and Myclient\Myclient\obj\Debug\TempPE\
............此处省略48个文件信息
- 上一篇:串口数据采集开发C#
- 下一篇:VS2008开发的C#酒店管理系统源码SQL2005
相关资源
- C#TCP通信协议
- C#TCP 通信(TCP发送16进制)
- C#有界面局域网DES UDP加密通讯程序
- C# Socket Tcp文件传输和文字聊天系统
- C# TCP通讯组件源代码
- C#编写UDP协议的一个局域网简单聊天工
- C# TcpListener 例程(源码)
- C# Socket TCP通信
- c#实现Modbus tcp客户端
- c#与西门子s7-200 smart 通讯 S7TCPDLL.dll
- C# Modbus TCP通讯(S7200PLC)
- UDP实现简单的聊天室
- 使用Socket发送数据
- C#UDP连接
- c# udp通信例程(上位控制程序)
- 服务器和客户端(TCP发送消息、发送
- TCP通信(支持发送文本和图片文件)
- modbus tcp 调试
- TcpClient C#2010读取PLC
- tcp和udp(Mina.net)
- Tcp/ip socket 服务器/客户端
- TCP/IP通讯 (附服务端以及客户端)
- udp通讯(需修改客户端链接服务端时
- 简单tcp 通讯网口工具客户端
- MODBUS通讯(TCP/Rtu)
- C# 使用 TcpClient,Socket 聊天
- tcp+udp完全c#通信封装
- Unity通过TCP接收服务端数据
- Unity和C#实现TCP网络聊天
- C#实现TCP服务端
评论
共有 条评论