我想使用 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;
}
}