简体   繁体   中英

Java: Send and receive byte array

Well, I want to write a simple java client-server-programme, which exchanges byte arrays over tcp-sockets.

/* 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;
}
}

The problem is, that the client send something to the server but the server doesn't receive anything, so he hangs at the readMessage function. On the other hand, the client receive some weird stuff, but not the response from the server.

The server receives bytes, but it never leaves the while loop because read() never returns -1. read() returns -1 when the end of the stream is reached. And that happens only when the client closes the socket output stream. Since the client never closes the output stream, the server keeps waiting for the more bytes to come.

Side note: your code is hard to read because you don't respect the standard Java naming conventions: variables start with a lowercase letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM