我正在建立一个简单的客户/服务器程序到学校。 服务器从客户端收到一条消息并在控制台中显示它。
它使用Java和UDP套接字。 客户端和服务器都在工作。
我的问题出在服务器上。 在控制台中显示消息后,它会用垃圾填充剩余的行(小方块更精确)。
客户来源:
import java.io.*;
import java.net.*;
public class Main {
public Main() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
int argc = args.length;
if (argc!=2){
System.out.println("Syntax:");
System.out.println("java javaUDPclient ip/hostname port");
return;
}
String hostname = args[0];
int port = Integer.parseInt(args[1]);
//create
try{
System.out.println ("Binding to a local port");
// CREATE A DATAGRAM SOCKET, BOUND TO ANY AVAILABLE LOCAL PORT
DatagramSocket socket = new DatagramSocket();
System.out.println ("Bound to local port " + socket.getLocalPort());
// CREATE A MESSAGE TO SEND USING A UDP PACKET
String message = new String("Time");
// GET THE CONTENTS OF OUR MESSAGE AS AN ARRAY OF BYTES
byte[] barray = message.getBytes();
// CREATE A DATAGRAM PACKET, CONTAINING OUR BYTE ARRAY
DatagramPacket packet = new DatagramPacket( barray, barray.length );
System.out.println ("Looking up hostname " + hostname );
// LOOKUP THE SPECIFIED HOSTNAME, AND GET AN INETADDRESS
InetAddress addr = InetAddress.getByName(hostname);
System.out.println ("Hostname resolved as "+addr.getHostAddress());
// ADDRESS PACKET TO SENDER
packet.setAddress(addr);
// SET PORT NUMBER TO 2000
packet.setPort(port);
// SEND THE PACKET - REMEMBER NO GUARANTEE OF DELIVERY socket.send(packet);
socket.send(packet);
System.out.println ("Packet sent!");
}catch (UnknownHostException e){
System.err.println ("Can't find host " + hostname);
}catch (IOException e){
System.err.println ("Error - " + e);
}
}
}
服务器来源:
package progd.java.udp.time.server;
import java.net.*;
import java.io.*;
public class UDPserver {
public static void main(String[] args) {
try{
System.out.println ("Binding to local port 6001");
// CREATE A DATAGRAM SOCKET, BOUND TO THE SPECIFIC PORT 6001
DatagramSocket socket = new DatagramSocket(6001);
// CREATE A DATAGRAM PACKET WITH A MAXIMUM BUFFER OF 256 BYTES
DatagramPacket packet = new DatagramPacket(new byte[256], 256);
// RECEIVE A PACKET (BY DEFAULT, THIS IS A BLOCKING OPERATION)
socket.receive(packet);
String message = new String(packet.getData());
// DISPLAY PACKET INFORMATION
InetAddress remote_addr = packet.getAddress();
System.out.println("Sent by: " + remote_addr.getHostAddress());
System.out.println ("Sent from port: " + packet.getPort());
System.out.println("Message:\n"+ message);
socket.close();
}
catch (IOException e){
System.err.println ("Error - " + e);
}
}
}
客户端控制台输出:
Binding to a local port
Bound to local port 58534
Looking up hostname localhost
Hostname resolved as 127.0.0.1
Packet sent!
服务器控制台输出: (如果我从控制台复制粘贴,则在文本编辑器中无法识别正方形,因此,我将制作printcrean)
我错过了" string-endig"字符?
由于
答案 0 :(得分:7)
正如DatagramPacket.getData()
的JavaDoc所说:
收到的数据或要发送的数据从缓冲区中的
offset
开始,并且运行时间为length
。
这意味着您获得的整个byte[]
不一定有效 - 您必须提取相关的部分内容。在客户端试试这个:
new String(packet.getData(), packet.getOffset(), packet.getLength());
我们正在使用String(byte[] bytes, int offset, int length)
构造函数。