编写简单的UDP应用
編寫簡單的UDP應用
首先,編寫發送UDP數據報的示例。這需要有個服務器端能接收到內容。用Java寫了個簡單的接收端:
public static void main(String[] args) throws IOException {
??? InetSocketAddress address = new InetSocketAddress("0.0.0.0", 5555);
??? DatagramSocket datagramSocket=new DatagramSocket(address);
????
??? System.out.println("start udp server");
????
??? byte[] buffer=new byte[1024];
????
??? for(;;){
??????? DatagramPacket datagramPacket=new DatagramPacket(buffer, buffer.length);
??????? datagramSocket.receive(datagramPacket);
??????? System.out.println("receive data:"+new String(datagramPacket.getData(),0,datagramPacket.getLength()));
??? }
}
下面寫發送的代碼:
AsyncUdpSocket *socket=[[AsyncUdpSocket alloc]initWithDelegate:self];
NSData *data=[@"Hello from iPhone" dataUsingEncoding:NSUTF8StringEncoding];
[socket sendData:data toHost:@"192.168.0.165" port:5555 withTimeout:-1 tag:1];
NSLog(@"send upd complete.");
執行后,在接收端成功輸出如下內容:
下面,寫個接收端的代碼:
AsyncUdpSocket *socket=[[AsyncUdpSocket alloc] initWithDelegate:self];
NSError *error = nil;
[socket bindToPort:5555 error:&error];
if (error) {
??? NSLog(@"error: %@",error);
}
[socket receiveWithTimeout:-1 tag:1];
NSLog(@"start udp server");
另外,至少寫這個delegate方法:
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock
???? didReceiveData:(NSData *)data
??????????? withTag:(long)tag
?????????? fromHost:(NSString *)host
?????????????? port:(UInt16)port{
??? NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
??? return YES;
}
發送端,還是用java寫個測試代碼:
public static void main(String[] args) throws IOException {
??? DatagramSocket datagramSocket = new DatagramSocket();
??? byte[] buffer = "Hello!".getBytes();
??? DatagramPacket datagramPacket = new DatagramPacket(buffer,
??????????? buffer.length, new InetSocketAddress("192.168.0.144", 5555));
??? datagramSocket.send(datagramPacket);
}
在iPhone日志中:
2011-07-20 15:23:33.571 SocketDemos[795:707] start udp server
2011-07-20 15:23:47.395 SocketDemos[795:707] received data: Hello!
收到了數據報。
使用UDP發送和接收組播
這里主要關注的是接收,一方面是需求上要求,另一方面,碰到過Android Wifi獲取組播問題,擔心iOS也有類似的機制。后來測試發現沒有那么麻煩(打開組播鎖)。
為了測試,還是用java編寫了個發送UDP廣播的簡單代碼:
public static void main(String[] args) throws IOException {
??? int port=3333;
??? MulticastSocket socket=new MulticastSocket(port);
??? InetAddress address=InetAddress.getByName("239.0.0.1");
??? socket.joinGroup(address);
??? byte[] data="Hello everyone.".getBytes();
??? DatagramPacket datagramPacket=new DatagramPacket(data,data.length,address,port);
??? socket.send(datagramPacket);
??? System.out.println("send ok.");
編寫的iOS代碼:
AsyncUdpSocket *socket=[[AsyncUdpSocket alloc] initWithDelegate:self];
NSError *error = nil;
[socket bindToPort:3333 error:&error];
[socket enableBroadcast:YES error:&error];
[socket joinMulticastGroup:@"239.0.0.1" error:&error];
if (error) {
??? NSLog(@"error: %@",error);
}
[socket receiveWithTimeout:-1 tag:1];
NSLog(@"start udp server");
delegate和上面接收普通UDP一模一樣:
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock
???? didReceiveData:(NSData *)data
??????????? withTag:(long)tag
?????????? fromHost:(NSString *)host
?????????????? port:(UInt16)port{
??? NSLog(@"received data: %@",[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
??? return YES;
}
測試得到的日志:
2011-07-20 16:14:30.338 SocketDemos[860:707] start udp server
2011-07-20 16:14:42.829 SocketDemos[860:707] received data: Hello everyone.
說明是收到了。
發送組播和前面的UDP發送類似,只是多了要做join group的操作。這里就不多說了。
?
?
?
社區原帖:http://www.cocoachina.com/bbs/read.php?tid=125575&fpage=3
轉載于:https://www.cnblogs.com/zhuchangsheng/p/5681974.html
總結
以上是生活随笔為你收集整理的编写简单的UDP应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云中间件团队首次解密企业级分布式应用
- 下一篇: CSS :hover 选择器