我想使用TCP在客户端/服务器之间发送/接收12个对象。对象是唯一的,但序列始终相同。例如:客户端始终通过发送"对象1"并且服务器总是以"对象2响应。"如何设置队列或堆栈来执行此操作并仅使用java标准库同步对象的发送/接收?我已经处理了下面的代码(1个包中的3个类文件)并且它不起作用(文件结束错误),但它显示了我的目标:
import java.net.*;
import java.io.*;
class SimpleClient {
public static void main(String args[]){
int counter = 0;
try{
Socket s = new Socket("localhost",2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
testobject to = new testobject(1,"object 1","field1","field2","field3","field4");
System.out.println("sending object 1"); //debug
oos.writeObject(to);
//Socket ss = s.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject too = (testobject)ois.readObject();
while (counter != 2) {
while ( too.value != 3 ) {
if (to==null) {
System.out.println("object is null!");
} else if (to.value==1){
System.out.println("receiving object 2"); //debug
System.out.println(to.id);
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
testobject to0 = new testobject(2,"object 3","field1","field2","field3","field4");
oos.writeObject(to0);
System.out.println("object 3 sent!");
} else if (to.value==2){
System.out.println("receiving object 4"); //debug
System.out.println(to.id);
os = s.getOutputStream();
oos = new ObjectOutputStream(os);
testobject to0 = new testobject(3,"object 5","field1","field2","field3","field4");
oos.writeObject(to0);
System.out.println("sending object 5");
}
}
is.close();
s.close();
//System.out.println((String)ois.readObject());
counter = counter + 1;
}
oos.close();
os.close();
s.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
class SimpleServer {
public static void main(String args[]) {
int port = 2002;
int counter = 0;
try {
ServerSocket ss = new ServerSocket(port);
while (counter != 1) {
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to = (testobject)ois.readObject();
while ( to.value != 1 ) {
if (to==null) {
System.out.println("object is null!");
} else if (to.value==1){
System.out.println("receiving object 1"); //debug
System.out.println(to.id);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
testobject to0 = new testobject(2,"object 2","field1","field2","field3","field4");
oos.writeObject(to0);
System.out.println("sending object 2");
} else if (to.value==2){
System.out.println("receiving object 3"); //debug
System.out.println(to.id);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
testobject to0 = new testobject(4,"object 4","field1","field2","field3","field4");
oos.writeObject(to0);
System.out.println("sending object 4");
}
}
is.close();
s.close();
counter = counter + 1;
}
ss.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
class testobject implements Serializable {
int value;
String id;
String field1;
String field2;
String field3;
String field4;
public testobject(int v, String s, String s1, String s2, String s3, String s4) {
this.value=v;
this.id=s;
this.field1 = s1;
this.field2 = s2;
this.field3 = s3;
this.field4 = s4;
}
}
答案 0 :(得分:1)
问题似乎是你的while (to.value != 1)
。服务器接收第一个对象,值为1.它根本不执行while循环,而是立即关闭套接字。因此,当客户端尝试从该套接字的输入流创建ObjectInputStream
时,它会遇到一个关闭的连接,它会期望一个对象流头。因此EOFException
。
实现对话的最简单方法是完全避免循环。毕竟,你进行循环,然后在循环中进行大小写区分,以在每次传递中执行特殊代码。您也可以编写一个线性程序,并在需要时将公共代码考虑到方法调用中,尽管您的示例几乎没有公共代码。
class SimpleClient {
public static void main(String args[]) throws Exception {
// Connection setup
Socket s = new Socket("localhost",2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to;
// Conversation
System.out.println("sending object 1");
to = new testobject(1,"object 1","field1","field2","field3","field4");
oos.writeObject(to);
System.out.println("receiving object 2");
to = (testobject)ois.readObject();
System.out.println(to.id);
System.out.println("sending object 3");
to = new testobject(2,"object 3","field1","field2","field3","field4");
oos.writeObject(to);
System.out.println("receiving object 4");
to = (testobject)ois.readObject();
System.out.println(to.id);
// Connection shutdown
ois.close();
oos.close();
s.close();
}
}
class SimpleServer {
public static void main(String args[]) throws Exception {
// Connection setup
ServerSocket ss = new ServerSocket(2002);
Socket s = ss.accept(); // only handle a single connection
ss.close(); // so we can immediately stop listening for more
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to;
// Conversation
System.out.println("receiving object 1");
to = (testobject)ois.readObject();
System.out.println(to.id);
System.out.println("sending object 2");
to = new testobject(2,"object 2","field1","field2","field3","field4");
oos.writeObject(to);
System.out.println("receiving object 3");
to = (testobject)ois.readObject();
System.out.println(to.id);
System.out.println("sending object 4");
to = new testobject(4,"object 4","field1","field2","field3","field4");
oos.writeObject(to);
// Connection shutdown
ois.close();
oos.close();
s.close();
}
}
如果你真的想从队列中发送东西,你可以简单地迭代它。
import java.net.*;
import java.io.*;
import java.util.*;
class SimpleClient {
public static void main(String args[]) throws Exception {
// Preparing the queues
List<testobject> sendQueue = Arrays.asList(
new testobject(1,"object 1","field1","field2","field3","field4"),
new testobject(2,"object 3","field1","field2","field3","field4"));
List<testobject> receiveQueue = new ArrayList<testobject>();
// Connection setup
Socket s = new Socket("localhost",2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to;
// Conversation, we start by sending
for (testobject toSend: sendQueue) {
System.out.println("Sending " + toSend.id);
oos.writeObject(toSend);
testobject received = (testobject)ois.readObject();
System.out.println("Received " + received.id);
receiveQueue.add(received);
}
// Connection shutdown
ois.close();
oos.close();
s.close();
}
}
class SimpleServer {
public static void main(String args[]) throws Exception {
// Preparing the queues
List<testobject> sendQueue = Arrays.asList(
new testobject(2,"object 2","field1","field2","field3","field4"),
new testobject(4,"object 4","field1","field2","field3","field4"));
List<testobject> receiveQueue = new ArrayList<testobject>();
// Connection setup
ServerSocket ss = new ServerSocket(2002);
Socket s = ss.accept(); // only handle a single connection
ss.close(); // so we can immediately stop listening for more
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
testobject to;
// Conversation, we start by receiving
for (testobject toSend: sendQueue) {
testobject received = (testobject)ois.readObject();
System.out.println("Received " + received.id);
receiveQueue.add(received);
System.out.println("Sending " + toSend.id);
oos.writeObject(toSend);
}
// Connection shutdown
ois.close();
oos.close();
s.close();
}
}
但是,由于在这种情况下,所有数据都是预先知道的,您可以通过不交错来节省一些网络往返,而是立即在两端发送所有数据,使用单独的线程来接收内容。但是,这将远离您的示例,所以我不会包含此代码。