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", 9000); 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,0,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"), 9000); 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 != -1) { bits = byte.Parse(read.ToString("x"), System.Globalization.NumberStyles.AllowHexSpecifier); data.Add(bits); } } while (read != -1); } if (data[0] == 0x7e) { Data.Clear(); Data.AddRange(data); } else { Data.AddRange(data); } if (data[data.Count-1] == 0x0d) { for (int i = 0; i < Data.Count;i++ ) { string value = Data[i].ToString("X"); if (value.Length==1) { value = "0" + 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, 9000); byte[] bytes= {0x1A,0x14,12,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, 9000); static void Main(string[] args) { Thread ath = new Thread(new ThreadStart(ReceiveData)); ath.Start(); } static void ReceiveData() { UdpClient sv = new UdpClient( 9000); while (true) { try { byte[] recData = sv.Receive(ref receivePoint); //receivePoint,获取发数据的地址。之后可以利用此地址回话。 if (recData.Length != 0) { 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()); } } }