Simon Njuguna
Simon Njuguna

Reputation: 47

Server, Client socket implementation

Am writing a Server, client chat program using Java Socket. Here is my code for the Server socket class.

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

public class Main {

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

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8085);
        } catch (IOException ex) {
            System.out.println("IO Error, " + ex);
            System.exit(1);
        }
        Socket clientSocket = null;
        System.out.println("Listening for incoming connections");
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException ex) {
            System.out.println("Failed to accept connection " + ex);
            System.exit(1);
        }
        System.out.println("Connection Successful");
        System.out.println("Listening to get input");
        PrintStream output = new PrintStream(clientSocket.getOutputStream(), true);
        BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine;
        while ((inputLine = input.readLine()) != null) {
            System.out.println(inputLine);
            System.out.println("Server: ");
            inputLine = input.readLine();
            output.println(inputLine);
            if (!inputLine.equals("exit")) {

            } else {
                break;
            }
        }
        output.close();
        input.close();
        clientSocket.close();
        serverSocket.close();
    }
}

The client is able to make a connection and send a message to the server. The server can also receive the messages sent by the client. The problem is that when the message is sent from the server, the client does not receive the message. Here is my client socket code.

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


public class Client {
public static void main(String [] args) throws Exception
{
    BufferedReader input;
    PrintStream output;
    BufferedReader clientInput;
    try (Socket client = new Socket("127.0.0.1", 8085)) {
        input = new BufferedReader(new InputStreamReader(client.getInputStream()));
        output = new PrintStream(client.getOutputStream());
        clientInput = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while(true)
        {
            System.out.println("Client: ");
            line = clientInput.readLine();
            output.println("Server: " + line );
            if(line.equals("quit"))
            {
                break;
            }
        }
    }
    input.close();
    clientInput.close();
    output.close();
}
}

Upvotes: 2

Views: 200

Answers (3)

Ernestas Gruodis
Ernestas Gruodis

Reputation: 8787

Server side:

public class Server {

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

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8085);
        } catch (IOException ex) {
            System.out.println("IO Error, " + ex);
            System.exit(1);
        }
        Socket clientSocket = null;
        System.out.println("Listening for incoming connections");
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException ex) {
            System.out.println("Failed to accept connection " + ex);
            System.exit(1);
        }
        System.out.println("Connection Successful");
        System.out.println("Listening to get input");
        PrintStream output = new PrintStream(clientSocket.getOutputStream(), true);
        BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine;
        while ((inputLine = input.readLine()) != null) {

            System.out.println("Client request: " + inputLine);

            String resp = "some response as you need";
            output.println(resp);
            System.out.println("Server response: " + resp);

            if (!inputLine.equals("exit")) {

            } else {
                break;
            }
        }
        output.close();
        input.close();
        clientSocket.close();
        serverSocket.close();
    }
}

Client side:

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

public class Client {

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

        BufferedReader input;
        PrintStream output;
        BufferedReader clientInput;
        try (Socket client = new Socket("127.0.0.1", 8085)) {
            input = new BufferedReader(new InputStreamReader(client.getInputStream()));
            output = new PrintStream(client.getOutputStream());
            clientInput = new BufferedReader(new InputStreamReader(System.in));

            while (true) {

                String inputStr = clientInput.readLine();

                output.println(inputStr);
                System.out.println("Client: " + inputStr);

                if (inputStr.equals("quit")) {
                    break;
                }

                String serverResp = input.readLine();
                output.println("Server: " + serverResp);
            }
        }
    }
}

It is tested.

Upvotes: 2

Alain O'Dea
Alain O'Dea

Reputation: 21686

The server is expecting an extra line from the client input here:

while ((inputLine = input.readLine()) != null) {
    System.out.println(inputLine);
    System.out.println("Server: ");
    inputLine = input.readLine(); // <--- here

The client is not reading from the InputStream called input it gets when it connects to the server. It is only reading the local console input from clientInput.

In the while loop in Client.java you need something like this after the quit block to get the server's response:

System.out.println("Server: " + input.readLine());

Upvotes: 0

Mike Rotch
Mike Rotch

Reputation: 19

It's always a good idea to flush your output streams when you are done with them, the info you are sending may have buffered.

Upvotes: 0

Related Questions