Search code examples
javasocketsserverclientobjectinputstream

cannot receive data on client side which is running on different machine than server (Java socket programming)


Okay so I have made a project called Uno Game server. The server and client program both run fine when executed on same machine. But when they are executed on different machines, I cannot receive data on the client side. The program gets stuck where I have written readObject. Note: I have used Objectinputstream and Objectoutputstream

Here is client code:

class UnoPlayer
{
public static void main(String args[])throws 
IOException,ClassNotFoundException
{
    Socket s=new Socket("localhost",1234);
    Hand h=new Hand();
    String name,tempCardValue,tempCardColor,tempName,currentColor;
    int ncards,choice,drawFactor,color,i,menu=0;
    Card tempCard,playedCard;
    boolean finished=false;
    ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
    System.out.println("Enter name: ");
    Scanner sc=new Scanner(System.in);
    name=sc.nextLine();
    oos.writeObject(name);
    System.out.println("Waiting for Players to Connect...");
    ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
    System.out.println(ois.readObject());
    ncards=(int)ois.readObject();

The program is stuck at ObjectInputStream ois=new ObjectInputStream(s.getInputStream()) I guess it cannot get InputStream

Here is the server code:

    ServerSocket ss=new ServerSocket(1234);
    Scanner sc=new Scanner(System.in);
    String name,tempCardValue,color;
    Player tempPlayer,currentPlayer;
    Card tempCard,upperCard;
    int i,j,nplayers,ncards,turn=0,dir=0,drawFactor=1;
    Deck d=new Deck();
    Pile p=new Pile();
    boolean finished=false;
    System.out.println("Enter The Number of Players (Max 4)");
    nplayers=sc.nextInt();
    System.out.println("Waiting for the Game Server to be ready...");
    for(i=0;i<nplayers;i++)
    {
        players.add(new Player(ss));
        name=(String)players.get(i).getInputStream().readObject();
        players.get(i).getOutputStream().flush();
        tempPlayer=players.get(i);
        tempPlayer.setName(name);
        System.out.println(tempPlayer.getName()+" Connected.");
    }
    for(i=0;i<nplayers;i++)
    {
        players.get(i).getOutputStream().writeObject("All the players are 
       ready!");
        players.get(i).getOutputStream().flush();
    }
    System.out.println("Players are ready!");
    d.createDeck();
    d.shuffle(5);
    System.out.println("Enter number of cards (Max 8)");
    ncards=sc.nextInt();

And this is the player class

class Player
{
String name;
Socket s;
ObjectInputStream ois;
ObjectOutputStream oos;
public Player(ServerSocket ss) throws IOException
{
    s=ss.accept();
    oos=new ObjectOutputStream(s.getOutputStream());
    //oos.flush();
    ois=new ObjectInputStream(s.getInputStream());

}
public void setName(String name)
{
    this.name=name;
}
public String getName()
{
    return name;
}
public Socket getSocket()
{
    return s;
}
public ObjectInputStream getInputStream()
{
    return ois;
}
public ObjectOutputStream getOutputStream()
{
    return oos;
}
}

Also instead of localhost i am using ip address of machine. Note: the server and client are running on different machines.


Solution

  • As it turns out I had problem with my firewall. Thanks anyway for help. I found out while removing my error:

    Always create objectoutputstream first and flush it on creation. Then create objectinputstream. The objectinputstream always blocks until it can get the sender's objectoutputstream.