首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

套接字通信有关问题!救命用

2012-07-08 
套接字通信问题!救命用我用的是UDP连接,在本地或者局域网都没有问题,可是放到服务器就不行了,怎么回事?另

套接字通信问题!救命用
我用的是UDP连接,在本地或者局域网都没有问题,可是放到服务器就不行了,怎么回事?

另外还有一个诡异的现象,另一个朋友编译的文件放在服务器当接收的,我自己这边发送就能收到
他的源码我编译一下,送到服务器上,就接收不到,各位帮我看看,是不是我写错了??

服务端:

C# code
using System;using System.Net;using System.Net.Sockets;using System.Text;namespace Test2{    class Program    {        static void Main(string[] args)        {            int recv;            byte[] data = new byte[1024];            //构建TCP 服务器            //得到本机IP,设置TCP端口号                     IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 63338);            using (Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))            {                newsock.Bind(ipep);                Console.WriteLine("绑定网络地址 {0}", Dns.GetHostName());                Console.WriteLine("等待客户机连接");                IPEndPoint aaa = new IPEndPoint(IPAddress.Any, 0);                EndPoint Remote = (EndPoint)aaa;                data = new byte[1024];                recv = newsock.ReceiveFrom(data, ref Remote);                Console.WriteLine("收到:" + Encoding.ASCII.GetString(data).Trim('\0'));                string welcome = "Welcome ! ";                data = Encoding.ASCII.GetBytes(welcome);                newsock.SendTo(data, data.Length, SocketFlags.None, Remote);                System.Threading.Thread.Sleep(200);            }        }    }}




客户端:
C# code
using System;using System.Net;using System.Net.Sockets;using System.Text; namespace Test{    class Class1    {        static void Main(string[] args)        {            byte[] data;            string input = "hee";            string stringData;            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("223.4.21.***"), 63338);                        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);            while (true)            {                Console.WriteLine("请输入!");               input = Console.ReadLine();                data = Encoding.ASCII.GetBytes(input);                server.SendTo(data, data.Length, SocketFlags.None, ipep);                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);                EndPoint Remote = (EndPoint)sender;                                data = new byte[1024];                int recv = server.ReceiveFrom(data, ref Remote);                stringData = Encoding.ASCII.GetString(data, 0, recv);                Console.WriteLine("收到:" + stringData);            }        }    }}


[解决办法]
测了下你的客户端,没问题,服务器端改下
C# code
 public static void Main()  {   int recv;   byte[] data = new byte[1024];   IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);   Socket newsock = new Socket(AddressFamily.InterNetwork,           SocketType.Dgram, ProtocolType.Udp);   newsock.Bind(ipep);   Console.WriteLine("Waiting for a client...");   IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);   EndPoint Remote = (EndPoint)(sender);   recv = newsock.ReceiveFrom(data, ref Remote);   Console.WriteLine("Message received from {0}:", Remote.ToString());   Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));   string welcome = "Welcome to my test server";   data = Encoding.ASCII.GetBytes(welcome);   newsock.SendTo(data, data.Length, SocketFlags.None, Remote);   while(true)   {     data = new byte[1024];     recv = newsock.ReceiveFrom(data, ref Remote);        Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));     newsock.SendTo(data, recv, SocketFlags.None, Remote);   }  } 


[解决办法]
别忘了用Releas版本,如果服务器上没有装开发环境,打的断点,可能会抛异常。

热点排行