java对象流
我正在尝试用 java 创建一个服务器,它将同时维护最多 4 个连接。我认为将相关信息保存在数组中可以满足我的目的,但是我遇到了一些麻烦。
这是我创建的类:
import java.net.*;
import java.io.*;
public class tcpConnects{
private ObjectInputStream input;
private ObjectOutputStream output;
private int player;
public tcpConnects(int playerNumber, Socket connect) {
// TODO Auto-generated method stub
try{
System.out.println("create InputStream");
input= new ObjectInputStream(connect.getInputStream());
System.out.println("create OutputStream");
output= new ObjectOutputStream(connect.getOutputStream());
System.out.println("streams created");
//sendData("Welcome!");
player=playerNumber;
}catch (IOException ioException){
ioException.printStackTrace();
}
}
public ObjectInputStream getInput(){
return input;
}
public void setInput(ObjectInputStream in){
input=in;
}
public ObjectOutputStream getOutput(){
return output;
}
public void setOutput(ObjectOutputStream out){
output=out;
}
public int getPlayer(){
return player;
}
public void sendData(String data){
try{
output.writeObject(data);
output.flush();
}catch (IOException ioException){
ioException.printStackTrace();
}
}
}
无论如何,当我将套接字发送到类中时,它会进入创建输入流的阶段,但实际上从未创建该流。语法似乎是正确的,所以我只能假设存在某种我不知道的逻辑错误。任何有助于解释为什么此类不会创建输入或输出流的帮助将不胜感激。
谢谢,
I'm trying to create a server in java which will maintain up to 4 connections simultaneously. I thought that holding the relevant information in an array would serve my purpose, well, but I'm having some trouble.
Here is the class I've created:
import java.net.*;
import java.io.*;
public class tcpConnects{
private ObjectInputStream input;
private ObjectOutputStream output;
private int player;
public tcpConnects(int playerNumber, Socket connect) {
// TODO Auto-generated method stub
try{
System.out.println("create InputStream");
input= new ObjectInputStream(connect.getInputStream());
System.out.println("create OutputStream");
output= new ObjectOutputStream(connect.getOutputStream());
System.out.println("streams created");
//sendData("Welcome!");
player=playerNumber;
}catch (IOException ioException){
ioException.printStackTrace();
}
}
public ObjectInputStream getInput(){
return input;
}
public void setInput(ObjectInputStream in){
input=in;
}
public ObjectOutputStream getOutput(){
return output;
}
public void setOutput(ObjectOutputStream out){
output=out;
}
public int getPlayer(){
return player;
}
public void sendData(String data){
try{
output.writeObject(data);
output.flush();
}catch (IOException ioException){
ioException.printStackTrace();
}
}
}
anyway, when I send a socket into the class, it gets to the stage of creating the input stream, but then the stream is never actually created. The syntax appears to be correct, so I can only assume that there is some form of logic error which I am not aware of. Any help in deciphering why this class will not create an input or output stream would be greatly appreciated.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
ObjectInputStream
之前在两端创建ObjectOutputStream
。ObjectOutputStream
的构造函数将标头写入到ObjectInputStream
的构造函数读取的流中,因此,如果您先创建ObjectInputStreams
,则会出现死锁。Create the
ObjectOutputStream
before theObjectInputStream
, at both ends. The constructor ofObjectOutputStream
writes a header to the stream that the constructor ofObjectInputStream
reads, so if you create theObjectInputStreams
first you get a deadlock.