3168|2

75

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

udp通信接收问题(附源码) [复制链接]


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;

  4. using System.Net;
  5. using System.Net.Sockets;

  6. namespace cejie
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //Creates a UdpClient for reading incoming data.
  13.             UdpClient receivingUdpClient = new UdpClient(2950);

  14.             //Creates an IPEndPoint to record the IP Address and port number of the sender.
  15.             // The IPEndPoint will allow you to read datagrams sent from any source.
  16.             //IPAddress ip = new IPAddress("192.168.137.115");

  17.             IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  18.             while (true)
  19.             {
  20.                 // Blocks until a message returns on this socket from a remote host.
  21.                 Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

  22.                 //string returnData = Encoding.ASCII.GetString(receiveBytes);

  23.                 //Console.WriteLine("This is the message you received " +
  24.                 //                             returnData.ToString());
  25.                 Console.WriteLine("This message was sent from " +
  26.                                             RemoteIpEndPoint.Address.ToString() +
  27.                                             " on their port number " +
  28.                                             RemoteIpEndPoint.Port.ToString());
  29.             }
  30.         }
  31.     }
  32. }
  33. //在pc机上运行没有什么问题。能接受到发送的数据。但是放到wince上边怎么出错啊?说什么少什么程序集。想知道到底是程序有问题还是装的wince有问题??

  34. using System;
  35. using System.Collections.Generic;
  36. using System.Text;

  37. using System.Net;
  38. using System.Net.Sockets;
  39. using System.Threading;

  40. namespace jieshou
  41. {
  42.     class Program
  43.     {
  44.         static void Main(string[] args)
  45.         {
  46.                 Jie();
  47.         }

  48.         static void Jie()
  49.         {
  50.             int i = 0;

  51.              Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//初始化一个Scoket协议

  52.             IPEndPoint iep = new IPEndPoint(IPAddress.Any, 3000);//初始化一个侦听局域网内部所有IP和指定端口

  53.             EndPoint ep = (EndPoint)iep;

  54.             socket.Bind(iep);//绑定这个实例

  55.             while (true)
  56.             {
  57.                 byte[] buffer = new byte[1024];//设置缓冲数据流

  58.                 socket.ReceiveFrom(buffer, ref ep);//接收数据,并确把数据设置到缓冲流里面

  59.                 Console.WriteLine(Encoding.Unicode.GetString(buffer).TrimEnd('\u0000') + " " + i.ToString());

  60.                 i++;
  61.             }
  62.         }
  63.     }
  64. }
  65. //又写了一个socket的接收程序。在pc上边运行没问题。但放到wince上边就没法跑。程序运行的时候出错。说是缺少程序集。socket.net.bind();不知道问题出在哪??而且发生了奇怪的事情,前边这两个程序自己运行过。能成功。但是不正常,不知道那的原因??
复制代码

最新回复

你试一下自动设定端口号  详情 回复 发表于 2008-4-28 10:34
点赞 关注

回复
举报

79

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
没用过C#写的,不过看你的问题,应该是该包的头文件或库没有连进去
 
 

回复

53

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
你试一下自动设定端口号
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表