服务器和客户端正在发送多个对象[重复]
Posted
技术标签:
【中文标题】服务器和客户端正在发送多个对象[重复]【英文标题】:Server and Client are sending multiple objects [duplicate] 【发布时间】:2014-07-06 04:09:16 【问题描述】:我希望服务器向客户端发送一个对象,然后客户端用另一条消息回复服务器,依此类推。
我有以下代码,但收到异常消息。我想我不明白通过套接字发送和接收对象的逻辑。
服务器代码:
public class Server implements Runnable
int MAX_NUM_CLIENT = 5;
int clientNum = 0;
boolean isStart = false; // if ranking algorithm starts it is true
Socket csocket;
Server(Socket csocket)
this.csocket = csocket;
BufferedReader reader = new BufferedReader(new InputStreamReader
(getClass().getClassLoader().getResourceAsStream("questions.txt")));
ArrayList<String> questions = new ArrayList<String>();
public static void main(String args[])
throws Exception
ServerSocket ssock = new ServerSocket(9654);
System.out.println("Listening");
//wait connection
while (true)
Socket sock = ssock.accept();
System.out.println("Connected");
new Thread(new Server(sock)).start();
public void run()
try
if(questions.isEmpty())
String line = null;
while((line = reader.readLine()) != null)
questions.add(line);
ObjectOutputStream objectOutput = new ObjectOutputStream(csocket.getOutputStream());
ObjectInputStream objectInput = new ObjectInputStream(csocket.getInputStream());
//InetAddress host = InetAddress.getLocalHost();
objectOutput.writeObject(questions);
P0Computation comp = new P0Computation(); //some calculations
P0Sending send1 = comp.P1Calculation1(); //some calculations
objectOutput.writeObject(send1);
int [] pjans = new int[2];
pjans[0] = objectInput.readInt(); // read client's answer
pjans[1] = objectInput.readInt(); // read client's answer
objectOutput.writeInt(comp.P0Calculation2(pjans));
csocket.close();
catch (IOException e)
System.err.println("Client left...");;
客户端代码:
public class Client
public static void main(String args[]) throws UnknownHostException, IOException, ClassNotFoundException
Socket ssocket = null;
ssocket = new Socket("192.7.1.6", 9654);
ObjectOutputStream objectOutput = new ObjectOutputStream(ssocket.getOutputStream());
ObjectInputStream objectInput = new ObjectInputStream(ssocket.getInputStream());
ArrayList<String> questions = (ArrayList<String>) objectInput.readObject();
P0Sending p0ans = (P0Sending) objectInput.readObject();
PjComputation comp = new PjComputation();
objectOutput.writeInt(comp.PjComputation1(p0ans.QX, p0ans.c, p0ans.g)[0]);
objectOutput.writeInt(comp.PjComputation1(p0ans.QX, p0ans.c, p0ans.g)[1]);
int beta = objectInput.readInt();
这里是异常信息:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Client.main(Client.java:16)
【问题讨论】:
这意味着匹配的套接字已关闭。 我同时运行server类,这种情况下socket是怎么关闭的? 我遇到过类似的问题,当需要 127.0.0.1:9654 时,服务器绑定到 localhost:9654。启动服务器后运行 netstat 以查看其监听的确切位置(或是否)Client.main(Client.java:16)
是什么
【参考方案1】:
此问题通常是由于写入已被对等方关闭的连接。
【讨论】:
这个问题是关于“连接重置”异常,而不是“套接字关闭”异常,这是由 您 关闭您自己的 引起的socket 然后继续使用它。如果您有自己的问题,请将其作为问题提出。无论如何,请停止在此处发布不相关的答案和 cmets。以上是关于服务器和客户端正在发送多个对象[重复]的主要内容,如果未能解决你的问题,请参考以下文章