Carl S
Carl S

Reputation: 33

Keeping data in sync with a client and server?

I am creating a small program that sends data in between a client and server (to practice network programming). All it does is have the client send the data (an integer) to the server, then the server will add one and send it back to the client, which in turn will add one and send it the server to repeat the cycle up to 100.

The code for the server class:

package simpleslickgame;

import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class NewServer{

static ArrayList<Socket> sockets = new ArrayList<Socket>(20);
static ArrayList<String> msgHistory = new ArrayList<String>(10000);
public static void main(String args[]){

    int port = 60606;

    Socket socky;
    ServerSocket ss;

    try{
        System.out.println("Welcome to the data sender!");
        System.out.println("Listening on port " + port);

        ss = new ServerSocket(port);

        while (true){
            socky = ss.accept();

            sockets.add(socky);

            System.out.println(socky.toString());
            //pw = new PrintWriter(socky.getOutputStream(), true);
            System.out.println("Connection established!");
            Thread t = new Thread(
                    new DataThread(socky));
            t.start();

        }

    }catch(Exception e){
        e.printStackTrace();
    }
}
}
class DataThread extends NewServer implements Runnable{
private Socket s;
private int data;

public DataThread(Socket sock){
    this.s = sock;
}

public void run(){
    String client =  s.getInetAddress().toString();
    System.out.println("Connected to " + client);

    try{
        Scanner in  = new Scanner(s.getInputStream());

        PrintWriter pw = new PrintWriter(s.getOutputStream(), true);

        while(true){
        //pw.println("Testing...");
        this.data = in.nextInt();
        if(this.data > 99)
            break;
        System.out.println("Server: " + this.data);
        pw.println(this.data+1);
        }
        s.close();
        in.close();

    }catch(Exception e){
        e.printStackTrace();
    }
}
}

and then the Client class

package simpleslickgame;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class NewClient {

public static void main(String[] args) {
    int port = 60606;
    System.out.println("Welcome to the message client!");
    Socket s = getSocket(port);
    String message;

    String user;
    try{
        System.out.println("Connected on port " + port);


        while(true){
            Thread t = new Thread(
                    new GameThread(s));
            t.start();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}
private static Socket getSocket(int port){
    Socket s;
    String host;
    InetAddress ip;

    Scanner input = new Scanner(System.in);
    while(true){
        System.out.print("What server do you want to connect to?");
        host = input.nextLine();
        try{
            ip = InetAddress.getByName(host);
            s = new Socket(ip,port);
            return s;
        }catch(UnknownHostException e){
            System.out.println("The host is unknown.");
        }catch(IOException e){
            System.out.println("Network error.");
        }
    }
}

}
class GameThread extends NewClient implements Runnable{
private Socket s;
private int data;
public GameThread(Socket sock){
    this.s = sock;
}
public void run(){
    try{
        Scanner in = new Scanner(s.getInputStream());
        PrintWriter out = new PrintWriter(s.getOutputStream(), true);
        this.data = -1;
        while(true){

            out.println(this.data+1);
            this.data = in.nextInt();
            System.out.println("Client: " + this.data);
            if(this.data > 99){
                break;
            }
        }
        this.s.close();


    }catch(Exception e){
        e.printStackTrace();
    }
}



}

When I run these two however, I just get a massive amount of output that does not seem to follow the code, instead just what appears to be random amounts(obviously they are not, but I would like some help in figuring out why it is not incremental).

example output:

Server: 4 Server: 10 Server: 12 Server: 2 Server: 16 Server: 14 Server: 6 Server: 4 Server: 6 Server: 12 Server: 12 Server: 6 Server: 10 Server: 16 Server: 10 Server: 12

Upvotes: 0

Views: 357

Answers (1)

in NewClient class:

     while(true){
        Thread t = new Thread(
                new GameThread(s));
        t.start();
    }

Generates several threads using the same socket. Hence the server gets seemingly random int's, since there are several using the same communication channel.

Upvotes: 1

Related Questions