简体   繁体   中英

Java sockets send and receiving data

Hi I am trying to understand how Sockets work can implement a multiplayer side to a monopoly game.I understood how to create the connection, but now it seems I have trouble sending and receiving the data between the client and the server .Here is my code:

Client code:

public class EchoClient
{
  public static void main(String[] args)
  {
    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try
    {
      echoSocket = new Socket("127.0.0.1", 5000);;
      out = new PrintWriter(echoSocket.getOutputStream());
      in = new BufferedReader(
          new InputStreamReader(echoSocket.getInputStream()));

      BufferedReader stdIn = new BufferedReader(
          new InputStreamReader(System.in));

      String userInput;

      while ((userInput = stdIn.readLine()) != null)
      {
        out.println(userInput);

      }

      out.close();
      in.close();
      stdIn.close();
      echoSocket.close();
    }
    catch (UnknownHostException e)
    {
      System.err.println("Don't know about host: taranis");
    }
    catch (IOException e)
    {
      System.err.println("Couldent get I/O for "
          + " the connection to : taranis.");
    }

  }
}

Server code:

public class ServerSide
{

  ServerSocket connect;
  Socket connection;
  PrintWriter out;
  BufferedReader in;

  public void go()
  {

    try
    {
      connect = new ServerSocket(5000);
      connection = connect.accept();
      in = new BufferedReader(
          new InputStreamReader(connection.getInputStream()));

      String userInput;

      while ((userInput = in.readLine()) != null)
      {
        System.out.println("echo: " + in.readLine());
      }

    }
    catch (IOException e)
    {
      System.out.println(e);
    }
  }

  public static void main(String[] args)
  {
    new ServerSide().go();
  }
}

I was trying to create here a simple connection between a client and a server.On the client side when the user inputs data I want it to be sent to the server and then print it it on the server console.It seems that the way I wright the code it isent working what did I do wrong?

First of all you need to declare a console object and then print on it.
Then don't forget to:
After every print on the console at the server side you need to flush the stream so the all the data will be printed.

Your code looks fine to me. Usually with sockets and keyboard input, you run into the case where the reader.readLine() hangs because it is still trying to read input from the other side. Typically, i will put an empty out.println() at the end of my client so the server will terminate the reading while loop. I've tried flush() before as Mike suggested but that seems to not work for me.

On the client, you're missing out.flush; after the out.println .

On the server,

    while ((userInput = in.readLine()) != null) {
        System.out.println("echo: " + userInput); // not in.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