为什么会把我的拼音也显示到控制台?
package cn.sxt.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class Test2 {
/**
* 一发
* 一收
* @throws IOException
* */
public static void main(String[] args) throws IOException{
System.out.println("咨询者");
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket(8888);
while(true){
//创建DatagramSocket对象
//准备要发送的数据
String s =sc.nextLine();
byte[] buf =s.getBytes();
//创建数据包对象 //发送的数据,发送多少,发到哪台主机,主机程序端口
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("localhost"),9999);
//发送数据
ds.send(dp);
if(s.equals("bye")){
break;
}
/**接收数据*/
byte [] buf2 = new byte[1024];
DatagramPacket dp2 = new DatagramPacket(buf2,buf2.length);
//获取数据
ds.receive(dp2);
//借助String的构造方法查看
String str = new String (dp2.getData(),0,dp2.getLength());
System.out.println("客服说:"+str);
ds.close();
}
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;
public class Test2 {
/**
* 一发
* 一收
* @throws IOException
* */
public static void main(String[] args) throws IOException{
System.out.println("客服");
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket(9999);
while(true){
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
System.out.println("客户说:"+str);
/**
* 发送数据
* */
String s = sc.nextLine();
byte[] buf2 = s.getBytes();
DatagramPacket dp2 = new DatagramPacket(buf2,buf2.length,dp.getAddress(),dp.getPort());
ds.send(dp2);
if(s.equals("bye")){
break;
}
ds.close();
}
}
}
