问一个Java Socket编程问题

KyKiske22 KyKiske22 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
发送端:
public class UDPSend {
public static void main(String[] args) throws IOException {
DatagramSocket ds =new DatagramSocket();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//输入数据
String line=null;
while((line=br.readLine())!=null)
{
if("886".equals(line))
{
break;
}
byte[] b=line.getBytes();
DatagramPacket dp=new DatagramPacket
(b,b.length,InetAddress.getByName("192.168.252.1"),3303);
ds.send(dp);
}
ds.close();
}
}
接收端:
public class UPDReceive {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(3303);
while(true)
{
byte[] b=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String s=new String(dp.getData(),0,dp.getLength(),"UTF-8");
System.out.println("ip:"+ip+",字符串:"+s);
}
}
}
结果我在发送端输入"你好“,接收端的结果是:ip:192.168.252.1,字符串:浣犲ソ
字符串的内容并没有转码。我这个项目的Text file encoding属性也设置成UTF-8了
为什么结果没有转码呢???求大神解答。。
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
KyKiske22
非技术区 23,404 成员 70,506 内容