2 exit door eXit_door 于 2013.04.11 11:14 提问

通过udp发送hello world 信息

想要通过udp发送一个简单的信息,服务器部分在C#中执行:

c#中的服务器代码:

var server = new UdpClient(8585);
 var groupEP = new IPEndPoint(IPAddress.Parse("192.168.0.120"),8585);
 byte[] bytes = server.Receive(ref groupEP);

客户端的c# 部分:

System.Net.Sockets.UdpClient client;     
client = new System.Net.Sockets.UdpClient("192.168.0.120",8585);    
client.Send(new byte[]{1,2,3,4},4);

如何在objective中实现相同的客服端?我在网上找了一些教程,包括我将其中一个数据库导入到工程中,不知道应该怎么实例化新对象,我试过这样:

[[GCDAsyncUdpSocket alloc] initWithSocketQueue:... ??? I don't know how to initialize it.

非常感谢大家给我提供的帮助。谢谢

2个回答

xinyoulingxi1_tong
xinyoulingxi1_tong   2013.04.11 14:09
已采纳

下载GCDAsyncUdpSocket,然后你可以这样发送数据包

 GCDAsyncUdpSocket *udpSocket ; // create this first part as a global variable
 udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

 NSData *data = [ 
                   [NSString stringWithFormat:@"%Hello Wold" 
                    dataUsingEncoding:NSUTF8StringEncoding
                ];

[udpSocket sendData:data toHost:@"192.168.10.111" port:550 withTimeout:-1 tag:1];
vikeyToy
vikeyToy   2013.04.11 11:35

我试了用monotouch解决这个,

System.Net.Sockets.UdpClient client;
client = new System.Net.Sockets.UdpClient("192.168.0.120",550);
client.Send(new byte[]{1,2,3,4},4);
Csdn user default icon
上传中...
上传图片
插入图片