简体   繁体   中英

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();
}
}

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.

处理完输出流后刷新它们总是一个好主意,您发送的信息可能已缓冲。

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());

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