我正在尝试实施这样的协议:
Client side: 1) client sends command (String) 2) client sends object 3) client receives object or data [depends on command] Server side: 1) server reads command (String) 2) server receives object [type depends on command] 3) server sends object or data [depends on command]
在客户端,我正在做这样的事情(程序块在线,标有“!!!”):
/** Retrieves required wrapper streams */
private void getSocketStreams() {
try {
inputStream = new DataInputStream(
connection.getInputStream());
/* !!! here is a problem: can I do next line's stuff? */
inputObjectStream = new ObjectInputStream(
connection.getInputStream());
outputWriter = new BufferedWriter(
new OutputStreamWriter(
connection.getOutputStream()));
outputObjectStream = new ObjectOutputStream(
connection.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/** "put" command processor */
private int processCmdPut(OrderInfo orderInfo) {
/* Send command to peer */
try {
outputWriter.write("put");
outputWriter.newLine();
outputWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
/* Send inserted object to peer */
sendObject(orderInfo);
/* Get from peer inserted info id */
int id = -1;
try {
id = inputStream.readInt();
} catch (IOException e) {
e.printStackTrace();
}
return id;
}
/**
* Sends object to peer.
* @param obj object to send.
*/
public void sendObject(Object obj){
try {
outputObjectStream.writeObject(obj);
outputObjectStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
服务器端操作镜像客户端。
关于行的问题,用“!!!”标记:是否可以包装套接字的字节流 有两个不同的高杠杆流并轮流读取/写入(逐个)? 我错了吗?有我的错误或误解吗?
答案 0 :(得分:2)
缓冲倾向于使得使用不同的装饰器链充其量是困难的。您真的不希望在同一个流上混合文本和二进制数据。我建议使用与二进制数据相同的格式编写文本。
答案 1 :(得分:0)
这是服务器端代码,以防万一:
/** Retrieves required wrapper streams */
private void getSocketStreams() {
try {
inputReader = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
inputObjectStream = new ObjectInputStream(
clientSocket.getInputStream());
outputStream = new DataOutputStream(
clientSocket.getOutputStream());
outputObjectStream = new ObjectOutputStream(
clientSocket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Process distinct command from client.
* @param cmd command to process
* @return connection state flag.
*/
private boolean processCmd(String cmd) {
if ("put".equals(cmd)) {
System.out.println(cmd);
processCmdPut();
} else if ("bye".equals(cmd)) {
System.out.println(cmd);
return false;
}
return true;
}
/** "put" command processor */
private void processCmdPut() {
/* Reciever from a peer an object to put into data source */
OrderInfo orderInfo = (OrderInfo) receiveObject();
/* Put recieved object into a data source */
int id = ordersService.put(orderInfo);
/* Send to peer inserted data id */
try {
outputStream.writeInt(id);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Recieves an object from peer.
* @return recieved object, or <tt>null</tt> on error.
*/
public Object receiveObject() {
Object res = null;
try {
res = inputObjectStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return res;
}