c++网络通信(有待整理)

链接:http://pan.baidu.com/s/1i3nMLKT 密码:ksi8

c#网络通信(tcp/udp两部分)

TCP发送端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO; namespace tcpTest
{
class Program
{
static void Main(string[] args)
{
TcpClient client = new TcpClient("127.0.0.1", );
NetworkStream stream = client.GetStream();
//Byte[] btt = { 126, 12, 21, 32, 13 }; //以十进制的方式给Byte赋值后发送
//Byte[] btt = { 0x7e,0x1c,0x15,0x20,0xd }; //以十六进制的方式给Byte赋值后发送
string data = " 7e 1c 15 20 0d "; //将字符串转换后赋值给Byte再发送
string[] numbers = data.Trim().Split(' ');
List<byte> buffer = new List<byte>();
foreach (string number in numbers)
{
Byte aByte;
aByte = byte.Parse(number,System.Globalization.NumberStyles.AllowHexSpecifier);
buffer.Add(aByte);
}
Byte[] btt = buffer.ToArray();
stream.Write(btt,,btt.Length);
foreach(Byte bt in btt)
Debug.WriteLine(bt.ToString("x"));
client.Close();
stream.Close();
}
}
}

TCP接收端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Net; namespace tcpTest
{
class Program
{
static TcpListener listener;
static Thread thread;
static bool isLoop;
static List<byte> Data = new List<byte>();
static void Main(string[] args)
{
listener = new TcpListener(IPAddress.Parse("127.0.0.1"), );
listener.Start(); //定义、开始监听
isLoop = true;
thread = new Thread(new ThreadStart(BeginListen));
thread.Start();
}
static private void BeginListen()
{
NetworkStream stream; //重点1. 定义 流
while (isLoop)
{
List<byte> data = new List<byte>();
using (TcpClient client = listener.AcceptTcpClient())
{
byte bits;
int read;
stream = client.GetStream();
do
{
read = stream.ReadByte(); //重点2.获取数据字节
if (read != -)
{
bits = byte.Parse(read.ToString("x"), System.Globalization.NumberStyles.AllowHexSpecifier);
data.Add(bits);
}
} while (read != -);
}
if (data[] == 0x7e)
{
Data.Clear();
Data.AddRange(data);
}
else
{
Data.AddRange(data);
}
if (data[data.Count-] == 0x0d)
{
for (int i = ; i < Data.Count;i++ )
{
string value = Data[i].ToString("X");
if (value.Length==)
{
value = "" + value;
}
Console.Write(value);
}
Console.WriteLine("+");
}
}
}
}
}

结果图:

工程文件地址:http://pan.baidu.com/s/1ntIhjGP 密码:7h0z

UDP发送端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets; namespace client
{
class Program
{
static void Main(string[] args)
{
IPAddress HostIP = IPAddress.Parse("127.0.0.1");
IPEndPoint host = new IPEndPoint(HostIP, );
byte[] bytes= {0x1A,0x14,,0x12}; //12转换为16进制就是c
UdpClient uc = new UdpClient();
int i = uc.Send(bytes,bytes.Length, host);
Console.WriteLine(i);
}
}
}

UDP接收端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Threading; namespace server
{
class Program
{
static IPAddress ip = IPAddress.Parse("127.0.0.3");
static IPEndPoint receivePoint = new IPEndPoint(ip, );
static void Main(string[] args)
{
Thread ath = new Thread(new ThreadStart(ReceiveData));
ath.Start();
}
static void ReceiveData()
{
UdpClient sv = new UdpClient( );
while (true)
{
try
{
byte[] recData = sv.Receive(ref receivePoint); //receivePoint,获取发数据的地址。之后可以利用此地址回话。
if (recData.Length != )
{
foreach (byte b in recData)
Console.Write(b.ToString("x")+" "); //以16进制的形式展示,没有"x"则显示的是10进制的显示
Console.WriteLine("数据来自于IP:"+receivePoint.Address.ToString()+" 端口:"+receivePoint.Port.ToString()); //不知道端口为什么会变?
}
}
finally
{ }
}
}
}
}

结果截图:

相关:http://my.oschina.net/Tsybius2014/blog/351974

工程文件:链接:http://pan.baidu.com/s/1dDdLTV7 密码:3e8z

一个关于字节的处理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO; namespace tcpTest
{
class Program
{
static void Main(string[] args)
{ byte[] bytes = File.ReadAllBytes("C:\\Users\\dell\\Desktop\\1.rar"); //文件到字节
string stringByte = ""; //字节到字符串
foreach (byte by in bytes)
{
stringByte += by.ToString("X")+" " ;
}
//Debug.WriteLine(stringByte); //在此处可以将stringByte存储到数据库中,后续使用的时候在调出
string[] temStr = stringByte.Trim().Split(' '); //字符串到字节
List<byte> listByte = new List<byte>();
foreach (string tem in temStr)
{
Byte aByte;
aByte = byte.Parse(tem, System.Globalization.NumberStyles.AllowHexSpecifier);
listByte.Add(aByte);
}
byte[] backToBytes = listByte.ToArray();
// string stt = System.Text.Encoding.Default.GetString(backToBytes); //字节到对应的编码字符串,注意和上面的区分
// Debug.WriteLine(stt);
// backToBytes = System.Text.Encoding.Default.GetBytes(stt); //字符串到对应的编码字节
File.WriteAllBytes("C:\\Users\\dell\\Desktop\\2.rar", listByte.ToArray());
}
}
}

TCP和UDP的区别:http://www.cnblogs.com/bizhu/archive/2012/05/12/2497493.html

c++网络通信(与服务器通信聊天)和c#网络通信的更多相关文章

  1. 跨平台网络通信与服务器框架 acl 3.2.0 发布

    acl 3.2.0 版本发布了,acl 是 one advanced C/C++ library 的简称,主要包括网络通信库以及服务器框架库等功能,支持 Linux/Windows/Solaris/F ...

  2. windows phone 8.1开发:socket通信聊天

    本例用WPF程序做服务器端,windows phone程序做客户端.我们使用基于UDP协议的Socket通信.更多关于socket信息请查看:http://msdn.microsoft.com/zh- ...

  3. Android - 传统蓝牙通信聊天

    Android -传统蓝牙通信聊天 技术:java+Android4.4+jdk1.8 运行环境:Android4.4.Android7.0 概述 Android 传统蓝牙的使用,包括开关蓝牙.搜索设 ...

  4. AngularJs 与服务器通信 $http, $q, $resource

    $http服务是AngularJS系统自带的,可以用来进行网络通信.获取远程服务器的数据.要记住的是,$http是对浏览器XMLHttpRequest的封装,也就是说,它其实是Ajax. $http( ...

  5. 跨平台网络通信与服务器框架 acl 3.2.0 发布,acl_cpp 是基于 acl 库的 C++ 库

    acl 3.2.0 版本发布了,acl 是 one advanced C/C++ library 的简称,主要包括网络通信库以及服务器框架库等功能,支持 Linux/Windows/Solaris/F ...

  6. Google Play Store —与google服务器通信时出现问题

    机子:MX4 前几天刷完机后出现登录Google Play Store “与google服务器通信时出现问题”,今天试了好几种方法,来总结一下 1.修改最新Hosts文件 2.SmartHosts   ...

  7. Android操作HTTP实现与服务器通信(转)

    Android操作HTTP实现与服务器通信   本示例以Servlet为例,演示Android与Servlet的通信. 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方 ...

  8. vuejs与服务器通信

    vuejs与服务器通信 与服务器通信 Vue 实例的原始数据 $data 能直接用 JSON.stringify() 序列化.社区贡献了一个插件 vue-resource,提供一种容易的方式与 RES ...

  9. 通信服务器群集——跨服务器通信Demo(源码)

    对于一些基于TCP Socket的大型C/S应用来说,能进行跨服务器通信可能是一个绕不开的功能性需求.出现这种需求的场景类似于下面描述的这种情况. 假设,我们一台TCP应用服务器能同时承载10000人 ...

随机推荐

  1. 网络编程之TCP

    知识补充:源IP地址和目的IP地址以及源端口号和目的端口号的组合称为套接字.其用于标识客户端请求的服务器和服务. TCP编程的实现步骤:服务器端:1.通过ServletSocket创建绑定到指定客户端 ...

  2. 关于DCLP实现的单例模式的一些想法

    关于DCLP实现的单例模式的一些想法 我之前写过单例的文章( http://www.cnblogs.com/mkdym/p/4908644.html ),但是现在又有了一些想法,不想再在原来那篇文章上 ...

  3. Vim复制文件全部内容到系统剪贴板

    参考:http://vim.wikia.com/wiki/%22copy_all_to_clipboard%22_howto vim中有两个buffer为系统的剪贴板,它们为: * (primary ...

  4. (原)配置vs2013使用intel的IPP库

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5473890.html 参考网址: https://software.intel.com/en-us/n ...

  5. python笔记之常用模块用法分析

    python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...

  6. 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法【转自wjr2012的csdn blog】

    点击下载Genymotion-ARM-Translation.zip 将你的虚拟器运行起来,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行.然后重启你的虚拟机.

  7. WPF中获取控件之间的相对位置

    1,获取元素相对于父控件的位置 使用Vector VisualTreeHelper.GetOffset(Visual visual)方法,其会返回visual在其父控件中的偏移量,然后你再将返回值的V ...

  8. Compiler Principles 语法分析

    语法分析的两种思维方式:1:自顶向下分析 :从语法树的根部推下来一直推到需要确认的终结符号串为止:就是为了找到一个符号串的最左推导 自顶向下分析,因为文法有些是以非终结符开头的另外文法中还可能含有右部 ...

  9. Fragment与Activity相互传递数据:

    Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将Bundle数据包传给F ...

  10. android gridview布局,实现长按某一个,所有项都显示删除的图标

    最近一直忙着项目开发,有段时间没有写博文了,今天想跟大家分享的是长按gridview中的某一项显示删除图标,此时点击某项便可删除,再长按取消删除图标. gridview的布局文件如下: <?xm ...