简体   繁体   中英

Client - Server Communication, Not able to get client to send information to server

Here is my server side code:

package server;

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


public class Serverside 
{
public static void main (String [] args) throws Exception 
{
    System.out.println("The server is running.");
    int clientnumber = 1;
    ServerSocket server = new ServerSocket(9090);
    try
    {
        while (true)
        {
            new cserver(server.accept(), clientnumber++).start();

        }

    }finally
    {
        server.close();
    }

}

private static class cserver extends Thread
{
    private Socket socket;
    private int clientnumber;

    public cserver(Socket socket, int clientnumber)
    {
        this.socket = socket;

        this.clientnumber = clientnumber;
        log("New connection with Client: " + clientnumber + " at " + socket);
    }

    private void log (String message)
    {
        System.out.println(message);
    }

    public void run()
    {
        try
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            //Message for Client
            out.println("Hello, Client# " + clientnumber + ".");
            out.println("Input Line\n");

            while(true)
            {   //System.out.println("Reading from Client\n");
                String input = in.readLine();
            //  out.println(input);
                if (input == null)
                    break;

                out.println(input);
            }
        }catch (IOException e)
        {
                log("Error handling in client#" + clientnumber + ": " + e);
        }finally
        {
            try
            {
                socket.close();
            }catch (IOException e)
            {
                log("Could not close socket");
            }
            log("Connection with Client# " + clientnumber + " closed");
        }


    }
}
}

and here is my client side code:

package client;

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



public class clientside
{   public static void main (String [] args) throws IOException
{
    Socket myclient = new Socket("localhost", 9090);
    BufferedReader in = 
            new BufferedReader(new
            InputStreamReader(myclient.getInputStream()));
    PrintWriter out = new PrintWriter(myclient.getOutputStream(), true);
    out.print("Hello server, end session\n");

    for(int i=0;i<=2;i++)
    {
        String input = in.readLine();
        System.out.println(input);
    }

    System.out.println("It is done");

    myclient.close();

}
}

Now when I run the code, the client is able to receive the 2 lines from the server and print them but I am not able to send that one line from client to server and make it print. If anyone can help I would really appreciate it.

It is just a bug. When you change line 65 in Serverside.java to:

System.out.println(input);

and line 16 in clientside.java to:

out.println("Hello server, end session");

it works. So "System.out" instead of "out" in Serverside and "println" instead of "\\n" in client.

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