Java:发送和接收字节数组

时间:2014-08-07 15:52:34

标签: java sockets tcp send

好吧,我想编写一个简单的java客户端服务器程序,它通过tcp-socket交换字节数组。

/* Server */
public class Server {

private ServerSocket Server = null;
private Socket Client = null;

public static void main(String[] args) {
    Server A = new Server();
    A.runServer();
    A.listenServer();
}


public void runServer() {
    try {
        Server = new ServerSocket(1234);
    }
    catch (Exception e) {
        System.out.println("Server fault: "+ e.getMessage());
        System.exit(-1);
    }       
}

public void listenServer() {
    try {
        while (true) {
            System.out.println("Waiting...");
            Client = Server.accept();
            System.out.println("Got something new");
            readMessage(Client);
        }
    }
    catch (Exception e) {
        System.out.println("Server fault: "+ e.getMessage());
    }
}

public byte [] readMessage (Socket socket) {

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1];
        int len = -1;
        while((len = socket.getInputStream().read(buf))!=-1){
            baos.write(buf, 0, len);
        }
        for (int i=0; i<baos.toByteArray().length; i++) {
            System.out.println(baos.toByteArray()[i]);
        }

        return baos.toByteArray();
    }
    catch (Exception e) {
        System.out.println("Server fault: "+ e.getMessage());
    }

    return null;
}

public void writeMessage (Socket socket, String Message) {
    try {
        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        printWriter.print(Message);
        printWriter.flush();
    }
    catch (Exception e) {
        System.out.println("Server fault: "+ e.getMessage());
    }
}
}

/* Client */
public class Client {

public static void main(String[] args) {

    Client B = new Client();
    B.runClient();

}

public void runClient () {
    Socket socket = null;
    try {
        socket = new Socket("127.0.0.1", 1234);
    }
    catch (Exception e) {
        System.out.println("Client fault: "+e.getMessage());
    }

    byte [] Tmp = new byte[10];
    for (int i=0; i<Tmp.length; i++) {
        Tmp[i] = 1;
    }

    writeMessage(socket, Tmp);

    for (int i=0; i<10; i++) {
        byte []  Message = readMessage(socket);
        System.out.println(Message);
    }
}

public void writeMessage (Socket socket, byte [] myByteMessage) {
    try {
        OutputStream out = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(out);
        dos.write(myByteMessage, 0, myByteMessage.length);
        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        printWriter.print(myByteMessage);
        printWriter.flush();

    }
    catch (Exception e) {
        System.out.println("Could not send data over TCP");
        return;
    }
}

public byte [] readMessage (Socket socket) {

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1];
        int len = -1;
        while((len = socket.getInputStream().read(buf))!=-1){
            baos.write(buf, 0, len);
        }
        for (int i=0; i<baos.toByteArray().length; i++) {
            System.out.println(baos.toByteArray()[i]);
        }

        System.out.println("Test");
        return baos.toByteArray();
    }
    catch (Exception e) {
        System.out.println("Server fault: "+ e.getMessage());
    }

    return null;
}
}

问题是,客户端向服务器发送了一些内容但服务器没有收到任何内容,因此他挂起了readMessage函数。 另一方面,客户端收到一些奇怪的东西,但不是服务器的响应。

1 个答案:

答案 0 :(得分:1)

服务器接收字节,但它永远不会离开while循环,因为read()永远不会返回-1。到达流末尾时,read()返回-1。这只有在客户端关闭套接字输出流时才会发生。由于客户端从不关闭输出流,因此服务器会一直等待更多字节。

旁注:您的代码很难阅读,因为您不遵守标准的Java命名约定:变量以小写字母开头。