> 文档中心 > c# Socket Udp通讯示例源码

c# Socket Udp通讯示例源码

读卡器介绍: https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.15.55615b43otkNYI&id=22173428704https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.15.55615b43otkNYI&id=22173428704 

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Collections;using System.Collections.Specialized;namespace ConsoleApp{    class Program    { private static Socket newsock; private static EndPoint Remote; private static string localIp; static void Main(string[] args) {     ArrayList AllIp = new ArrayList();     string name = Dns.GetHostName();   IPAddress[] ipadrlist = Dns.GetHostAddresses(name);     foreach (IPAddress ipa in ipadrlist)     {  if (ipa.AddressFamily == AddressFamily.InterNetwork)      localIp = ipa.ToString();      if (checkip(localIp))      {   AllIp.Add(localIp);   Console.WriteLine("IPv4地址:"+localIp);      }      }     localIp = AllIp[0].ToString();  //如有多个网卡可选任意一个   ReceviceData(); } private static bool checkip(string ipstr)   //判断IP是否合法 {  IPAddress ip;     if (IPAddress.TryParse(ipstr, out ip))     { return true; }     else { return false; } } static void ReceviceData() {   IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(localIp), 39169);     newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);   newsock.Bind(ipep);     Console.WriteLine("绑定地址:" + localIp+":39169\n");     //发送信息     byte[] sendbuf = new byte[1];     sendbuf[0] = 0xa6; //搜寻所有连线的读卡器命令 IPEndPoint ipep1 = new IPEndPoint(IPAddress.Broadcast, 39169);     Remote = (EndPoint)(ipep1);     newsock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);//设为广播式发送     newsock.SendTo(sendbuf, 1, SocketFlags.None, Remote);     Console.WriteLine("SendTo:"+ (Convert.ToString(Remote)+" ").Substring(0,21)  + " Data:A6");     Thread thread = new Thread(new ThreadStart(GetData));     thread.Start(); } static void GetData() {     try     {  byte[] buf = new byte[1024];  byte[] sendbuf = new byte[255];  byte[] revbufheadbak = new byte[20];  while (true)  {      if (newsock == null || newsock.Available < 1)      {   Thread.Sleep(20);   continue;      }      //接受UDP数据包      int recv = newsock.ReceiveFrom(buf, ref Remote);      string RemoteIP = Convert.ToString(Remote).Split(':')[0];   //sock来源IP      int RemotePort = Convert.ToInt32(Convert.ToString(Remote).Split(':')[1]); //Sock来源端口      string recestr = "FromIP:" + (Convert.ToString(Remote) + " ").Substring(0, 21) + " Data:";//将接收到的数据显示      for (int i = 0; i 8){   if (buf[0] == revbufheadbak[0] && buf[1] == revbufheadbak[1] && buf[2] == revbufheadbak[2] && buf[3] == revbufheadbak[3] && buf[4] == revbufheadbak[4] && buf[5] == revbufheadbak[5] && buf[6] == revbufheadbak[6] && buf[7] == revbufheadbak[7] && buf[8] == revbufheadbak[8])   {DisableSendAge(buf, RemoteIP, RemotePort);  //是重复接收的数据包,回应确认接收,不做处理return;   } else {for (int i=0;i 38) {     string SerialNum = "";     for (int i = 38; i  14) {     string SerialNum = "";     for (int i = 14; i  14) {     string SerialNum = "";     for (int i = 14; i < recv; i++)     {  SerialNum = SerialNum + buf[i].ToString("X2");     }     Console.WriteLine("唯一设备标识:" + SerialNum+"\n"); } //此处可加入业务对数据库的查、增、删、改 SendDispBeep(cardnum, RemoteIP, RemotePort); break;   default:break;      } }     }     catch (Exception ex)     {  Console.WriteLine(ex.Message);     } }  static void DisableSendAge(byte[] buf, string RemortIpStr, int RemortPortInt) {   //回应已接收到数据包,否则读卡器会连发三次     buf[0] = 0x69;     IPEndPoint client = new IPEndPoint(IPAddress.Parse(RemortIpStr), RemortPortInt);     newsock.SendTo(buf, 9, SocketFlags.None, client);     string SendInf = "SendTo:" + (Convert.ToString(client) + " ").Substring(0, 21) + " Data:";     for (int i = 0; i < 9; i++)     {  SendInf = SendInf + buf[i].ToString("X2")+" ";     }     Console.WriteLine(SendInf); } static void SendDispBeep(string cardnum, string RemortIpStr, int RemortPortInt) {   //向读卡器发送显示文字及蜂鸣响声     string DispInf = "卡号:" + cardnum + "  " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")+"     ";//发送的显示文字     byte[] strlsansi = System.Text.Encoding.GetEncoding(936).GetBytes(DispInf);//显示文字转换为Ansi     byte[] Sendbyte=new byte[39];     Sendbyte[0] = 0x5a;  //指令码     Sendbyte[1] = 0x00;  //机号低位     Sendbyte[2] = 0x00;  //机号高位,如果高低位都为0表示任意机号     Sendbyte[3] = 0x02;  //蜂鸣响声代码,共12种不代响声代码     Sendbyte[4] = 0x14;  //显示时长     for (int i = 0; i < 34; i++)     {  Sendbyte[i+5]=strlsansi[i];     }     IPEndPoint client = new IPEndPoint(IPAddress.Parse(RemortIpStr), RemortPortInt);     newsock.SendTo(Sendbyte, 39, SocketFlags.None, client);     string SendInf = "SendTo:" + (Convert.ToString(client) + " ").Substring(0, 21) + " Data:";     for (int i = 0; i < 39; i++)     {  SendInf = SendInf + Sendbyte[i].ToString("X2") + " ";     }     Console.WriteLine(SendInf+"\n"); }    }}