简体   繁体   中英

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

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.

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

Simply add a

toClient.flush()

call on the server side, too!

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