无法在与服务器不同的机器上 运行 的客户端接收数据(Java 套接字编程)
cannot receive data on client side which is running on different machine than server (Java socket programming)
好的,所以我做了一个名为 Uno Game 服务器的项目。服务器和客户端程序都 运行 在同一台机器上执行时都很好。但是当它们在不同的机器上执行时,我无法在客户端接收数据。程序卡在我写 readObject 的地方。注意:我使用了 Objectinputstream 和 Objectoutputstream
这是客户端代码:
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();
程序卡在
ObjectInputStream ois=new ObjectInputStream(s.getInputStream())
我猜它无法获取 InputStream
这是服务器代码:
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();
这就是玩家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;
}
}
另外,我使用的不是本地主机,而是机器的 IP 地址。
注意:服务器和客户端 运行ning 在不同的机器上。
您的服务器尚未在接受的套接字上构建 ObiectOutputStream
,因此客户端卡在 new ObjectInputStream()
试图读取 object 流 header。请参阅 Javadoc。
事实证明我的防火墙有问题。无论如何感谢您的帮助。
我在删除错误时发现:
始终先创建对象输出流并在创建时刷新它。
然后创建对象输入流。
objectinputstream 始终阻塞,直到它可以获取发送方的 objectoutputstream。
好的,所以我做了一个名为 Uno Game 服务器的项目。服务器和客户端程序都 运行 在同一台机器上执行时都很好。但是当它们在不同的机器上执行时,我无法在客户端接收数据。程序卡在我写 readObject 的地方。注意:我使用了 Objectinputstream 和 Objectoutputstream
这是客户端代码:
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();
程序卡在 ObjectInputStream ois=new ObjectInputStream(s.getInputStream()) 我猜它无法获取 InputStream
这是服务器代码:
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();
这就是玩家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;
}
}
另外,我使用的不是本地主机,而是机器的 IP 地址。 注意:服务器和客户端 运行ning 在不同的机器上。
您的服务器尚未在接受的套接字上构建 ObiectOutputStream
,因此客户端卡在 new ObjectInputStream()
试图读取 object 流 header。请参阅 Javadoc。
事实证明我的防火墙有问题。无论如何感谢您的帮助。 我在删除错误时发现:
始终先创建对象输出流并在创建时刷新它。 然后创建对象输入流。 objectinputstream 始终阻塞,直到它可以获取发送方的 objectoutputstream。