大家好,欢迎来到IT知识分享网。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
java代码如下,UDP 客户端 utf-8编码
package xj.ne;
/**
* UDP客户端 UTF-8编码
*/
import java.io.*;
import java.net.*;
public class UDPClient{
public static void main(String[] args)throws IOException{
DatagramSocket client = new DatagramSocket();
System.out.println(“请输入内容”);
BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
String sendStr = wt.readLine();
byte[] sendBuf;
sendBuf = sendStr.getBytes(“UTF-8”);
InetAddress addr = InetAddress.getByName(“127.0.0.1”);
int port = 27015;
DatagramPacket sendPacket
= new DatagramPacket(sendBuf ,sendBuf.length , addr , port);
client.send(sendPacket);
byte[] recvBuf = new byte[100];
DatagramPacket recvPacket
= new DatagramPacket(recvBuf , recvBuf.length);
client.receive(recvPacket);
String recvStr = new String(recvPacket.getData() , 0 ,recvPacket.getLength());
System.out.println(“收到:” + recvStr);
client.close();
}
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/15237.html