MisterCavespider
MisterCavespider

Reputation: 96

Java TCP - server & client working, but can't get answer

I'm trying to make a small TCP server/client thingy. I have a client and a server, and after getting some basic Exceptions solved, nothing works. The client is supposed to send (user) data to the server (at port 6789, localhost), who is then supposed to write it in uppercase and end it back. Everything seems to work, except for sending back the uppercase. Here is the code:

TCPServer:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {

    public static void main(String[] args) throws Exception {

        System.out.println("SERVER");

        String clientSentence;
        String capitalizedSentence;
        ServerSocket server = new ServerSocket(6789);


        TCPClient.main(null);


        while (true) {
            Socket connection = server.accept();
            System.out.println("<S> Connection!");
            BufferedReader fromClient = new BufferedReader(
                new InputStreamReader(connection.getInputStream()) );
            DataOutputStream toClient = new DataOutputStream(connection.getOutputStream());

            clientSentence = fromClient.readLine();
            System.out.println("<S> Recieved: " + clientSentence);

            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            System.out.print("About to send: " + capitalizedSentence);
            toClient.writeBytes(capitalizedSentence);
        }
    }

}

TCPClient:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class TCPClient {

    public static void main(String[] args) throws Exception {

        System.out.println("CLIENT");

        String sentence;
        String modSentence;
        BufferedReader inUser = new BufferedReader(new InputStreamReader(System.in));

        Socket clientToServer = new Socket("localhost", 6789);
        DataOutputStream outServer = new DataOutputStream(clientToServer.getOutputStream());
        BufferedReader inServer = new BufferedReader(
            new InputStreamReader(clientToServer.getInputStream()) );

        System.out.println("<C> Type now: ");
        sentence = inUser.readLine();
        System.out.println("<C> Got it!");

        System.out.println("<C> Sending " + sentence + " to server...");
        outServer.writeBytes(sentence + '\n');
        outServer.flush();

        System.out.println("<C> Server respodse:");
        modSentence = inServer.readLine();

        System.out.println("<C> From server: " + modSentence);
        clientToServer.close();
    }
}

Console:

SERVER
CLIENT
<C> Type now: 
efd
<C> Got it!
<C> Sending efd to server...
<C> Server respodse:
asd

and...

Nothing

Upvotes: 1

Views: 287

Answers (2)

MisterCavespider
MisterCavespider

Reputation: 96

The answer is: I'm stupid.

I accidentally let both the client and the server run on the same thread. I now made TCPClient a Runnable and everything works as planned.

NOTE: the TCPClient.main(null) does not start a new program/thread, just the same thread, another static function.

Upvotes: 1

GhostCat
GhostCat

Reputation: 140613

You are flushing on the client side - but not on the server side!

Simply add a

toClient.flush()

call on the server side, too!

Upvotes: 0

Related Questions