简体   繁体   中英

Communicate with server and client websockets in java properly

I have developed a simply video game and I want to update it with the multiplayer functionality using websockets. I want to have two versions of the game. The first one to work as server and the other version as a client. I want to initialize the game from the server and to wait for the reaction of the client. My first question: is it possible to run server and client in the same machine (give as an input the same ip)? Secondly I am using the following code in order to create the sockets from the server side:

    try {
        serverSocket = new ServerSocket(10007);
    }
    catch (IOException e)
    {
        System.err.println("Could not listen on port: 10007.");
        System.exit(1);
    }

    System.out.println ("Waiting for connection.....");

    try {
        clientSocket = serverSocket.accept();
    }
    catch (IOException e)
    {
        System.err.println("Accept failed.");
        System.exit(1);
    }
    System.out.println ("Connection successful");

When I am trying to connect from the client it seems that the whole thing its not working as i am receiving the message waiting for connection... . My code for connecting the client is the following:

 String serverHostname = new String("ip");

    if (args.length > 0)
        serverHostname = args[0];
    System.out.println("Attemping to connect to host " +
            serverHostname + " on port 10007.");

    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        echoSocket = new Socket(serverHostname, 10007);
        out = new PrintWriter(echoSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                echoSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + serverHostname);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for "
                + "the connection to: " + serverHostname);
        System.exit(1);
    }

What can it be the issue here?

Your Server Side Code is fine. just a little note here, the ServerSocket.accept(); method is ablocking call, meaning that program execution will halt there until a client connects to it.

Secondly i can see an issue from the first line of your client code below

if (args.length > 0)
    serverHostname = args[0];

args[0] may not be an ip address, i'm not too sure about how java command line applications behave but in c++ for instance, args[0] in the right context is always the absolute path of the program executable. this may be the case in java too. So you may be passing an ip address but will actually be passed in as args[1] .

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