想要通過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.
非常感謝大家給我提供的幫助。謝謝
下載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];