客户端 - 服务器ObjectInputStream错误 [英] Client - Server ObjectInputStream Error
问题描述
我的代码是这个
try
{
//Connect to the login server
socket = new Socket(hostName , 15000);
out = new ObjectOutputStream(socket.getOutputStream());
input = new ObjectInputStream(socket.getInputStream());
.. //perform the login , if the login succeed make answ=1;
}
catch (UnknownHostException e) {
System.err.println("Unknown host: " + hostName);
StatusLabel.setText("Unknown host");
}
catch (ConnectException e) {
System.err.println("Connection refused by host: " + hostName);
StatusLabel.setText("Connection refused by host, server is down.");
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try
{
//Finaly closing the connection
socket.close();
System.out.flush();
System.out.println("Socket Closed");
//closing the output and input stream
out.close();
input.close();
setVisible(false); //closing the window
}
catch (IOException e ) {
System.out.println("Couldn't close socket");
}
}
if(answ==1) //if the login suceed start the class for the game
{
Gumoku_Graphics g = new Gumoku_Graphics();
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.Play();
}
inside Gumoku_Graphics()我调用这个类执行与游戏服务器的连接
inside Gumoku_Graphics() i call this class which performs the connection with the game server
Game n = new Game();
status=n.Connection();
这是
public int Connection()
{
try {
socket = new Socket(hostName, 15432);
System.out.println("socket ok\n");
out = new ObjectOutputStream(socket.getOutputStream());
System.out.println("out ok\n");
input = new ObjectInputStream(socket.getInputStream());
System.out.println("socket,out,input ok\n");
return 1;
}
catch (UnknownHostException e) {
System.err.println("Unknown host: " + hostName);
return 0;
}
catch (ConnectException e) {
System.err.println("Connection refused by host: " + hostName);
return 0;
}
catch (IOException e) {
e.printStackTrace();
return 0;
}
}
并且每次停在input = new ObjectInputStream socket.getInputStream());
它不能这样做,它卡在那里。
它打印socket ok,out ok,它卡在那里。
什么问题?
and everytime it stops in "input = new ObjectInputStream(socket.getInputStream());" it can't do this and it stuck there. It prints "socket ok", "out ok" and it stuck there. Whats the problem? What am i doing wrong?
推荐答案
从 ObjectInputStream documentation :
创建从指定的InputStream读取的ObjectInputStream。从流中读取串行化流头并进行验证。这个构造函数将阻塞,直到相应的ObjectOutputStream写入和刷新头。
Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
如果引用,调用将阻塞,直到标题可以读取。您需要有一些数据从套接字到达,才能继续此呼叫。
As quoted the call will block until the header can be read. You need to have some data to arrive from the socket before this call can continue.
这篇关于客户端 - 服务器ObjectInputStream错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!