4

我正在为学校构建一个简单的客户端/服务器程序。服务器从客户端接收消息并将其显示在控制台中。

它使用 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!

服务器控制台输出:(如果我从控制台复制粘贴,文本编辑器无法识别方块,所以我将制作一个打印屏幕)

在此处输入图像描述

我错过了“string-endig”字符吗?

谢谢

4

1 回答 1

7

正如 JavaDocDatagramPacket.getData()所说:

接收的数据或要发送的数据从offset缓冲区中的 开始,并运行length很长时间。

这意味着byte[]您获得的整体不一定有效 - 您必须提取相关的部分。在客户端试试这个:

new String(packet.getData(), packet.getOffset(), packet.getLength());

我们正在使用String(byte[] bytes, int offset, int length)构造函数。

于 2012-10-06T16:13:30.693 回答