Esha
Esha

Reputation: 465

Server doesn't receive message in client-server application

In my client-server application, the client sends message to Server and the Server should display the message. But in my case, the client is only able to send but the server can't achieve it.

I have tried with different port numbers (i.e. 8080, 8000, 4444 etc). It seems that the socket can set up the connection, but I really don't know why the server can't read the input from client.

This is my complete project (I have ignored the main classes for both application here, because I have nothing more than just calling the methods):

EchoServer.java:

    package client.server;

import java.io.*;
import java.net.*;

public class EchoServer {

    public EchoServer() {
    }

    public void establish() {
        ServerSocket serverSocket = null;
        try {

            serverSocket = new ServerSocket(8080);
        } catch (IOException e) {
            System.out.println("Could not listen on port: 1234");
            System.exit(-1);
        }
        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("Accept failed: 1234");
            System.exit(-1);
        }
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            out = new PrintWriter(
                    clientSocket.getOutputStream(), true);
            in = new BufferedReader(
                    new InputStreamReader(
                            clientSocket.getInputStream()));
        } catch (IOException ioe) {
            System.out.println("Failed in creating streams");
            System.exit(-1);
        }
        String inputLine, outputLine;
        try {
            while ((inputLine = in.readLine()) != null) {
                out.println(inputLine);
                if (inputLine.equals("Bye.")) {
                    break;
                }
            }
        } catch (IOException ioe) {
            System.out.println("Failed in reading, writing");
            System.exit(-1);
        }
        try {
            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            System.out.println("Could not close");
            System.exit(-1);
        }
    }
}

EchoClient.java:

    package server.client;

import java.io.*;
import java.net.*;

public class EchoClient {

    public EchoClient() {
    }

    public void establish() {
        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            //echoSocket = new Socket(InetAddress.getLocalHost(), 1234);
            echoSocket = new Socket(InetAddress.getLocalHost(), 8080);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O");
            System.exit(1);
        }
        BufferedReader stdIn = new BufferedReader(
                new InputStreamReader(System.in));
        String userInput;
        try {
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                if (userInput.equals("Bye.")) {
                    break;
                }
                System.out.println("echo: " + in.readLine());
            }
            out.close();
            in.close();
            stdIn.close();
            echoSocket.close();
        } catch (IOException ioe) {
            System.out.println("Failed");
            System.exit(
                    -1);

        }

    }
}

Upvotes: 0

Views: 1889

Answers (3)

Ravindra babu
Ravindra babu

Reputation: 38950

Code seems to be fine. If you write some thing on PrintWriter at both server & client ends, you will get output.

Add below code in server: ( After creating Socket with accept() API)

out.println("Hello from Server");

Add below code in client ( After creating Socket)

out.println("Hello from client");

Other suggestions:

1) Create a thread once you accept a Socket Connection at server and that thread should handle all IO processing

2) You can create thread at client end too after creating Socket with Server. The new thread should handle all IO processing

Upvotes: 0

Shriram
Shriram

Reputation: 4421

In your server you are listening with readLine() in whileloop

while ((inputLine = in.readLine()) != null) {
                System.out.println("inputLine "+inputLine);

which is nothing but the inputstream from client. The above SOP will print the message from client. Try to flush your streams because it won't print until your buffer is full.

Upvotes: 0

Niklas P
Niklas P

Reputation: 3507

Your server doesn't write the incoming text to the console but only back to the client which doesn't handle incoming text from the server yet.

(out isn't System.out but Socket.out!)

Upvotes: 1

Related Questions