0

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.

1
  • Is the server trying to read everything the client might send before writing anything back? It's hard to tell with the way you've formatted the code. Commented Aug 8, 2014 at 14:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The the server should give a response to the client, so the client can not easly disconnect. Is there any other possibility to tell the server, that the client send all the data he wants? There could be a kind of escape bytes or so. But is there any other solution? Your side note: Thanks a lot, I'm new at Java :/
You have to define a protocol. You could indeed rely on a specific sequence of bytes, or the client could send the length of the message followed by the message itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.