简体   繁体   English

如何显示使用DatagramSocket.receive作为字符串的udp服务器的响应?

[英]How to show response from udp server using DatagramSocket.receive as string?

I use DatagramSocket in android application. 我在Android应用程序中使用DatagramSocket I am sending request from client to server. 我正在将请求从客户端发送到服务器。 The server responds with a string value either 1 or 0 . 服务器以1或0的字符串值作为响应。 My code sample is as follow: 我的代码示例如下:

// client sends
socket.send(datagrampacket); 

//Server have
socket.receive (datagrampacket);

I am using Logging to display the response coming from the server, but i don't know how to show the server response (the string received 0 or 1). 我正在使用日志记录来显示来自服务器的响应,但是我不知道如何显示服务器响应(字符串收到0或1)。

after server responding .. how can i receive datagrampacket on client side (as string it might be 0 or 1) ? 服务器响应后..我如何在客户端接收datagrampacket(作为字符串,它可能是0或1)?

socket.receive() will block until it received data. socket.receive()将阻塞直到接收到数据。 the received data will be in the datagrampacket byte-array. 接收到的数据将在datagrampacket字节数组中。

to display it, convert the byte-array to a string, eg in java this will look like: 为了显示它,将字节数组转换为字符串,例如在java中,它看起来像:

byte[]data=datagrampacket.getData();
int offset=datagrampacket.getOffset();
int length=datagrampacket.getLenght();
System.out.println( new String(data, offset, length) ) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM