Socket编程write和read对应问题

雨果是程序员 2016-12-28 10:10:50
1.NumberConverter.java
public interface NumberConverter {
public int toInt(byte[] b,int x,int y);

}

2.MyServer.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {
private int lengthOffset;
private int lengthSize;
private int lengthAddtive;
public int getLengthOffset() {
return lengthOffset;
}
public void setLengthOffset(int lengthOffset) {
this.lengthOffset = lengthOffset;
}
public int getLengthSize() {
return lengthSize;
}
public void setLengthSize(int lengthSize) {
this.lengthSize = lengthSize;
}
public int getLengthAddtive() {
return lengthAddtive;
}
public void setLengthAddtive(int lengthAddtive) {
this.lengthAddtive = lengthAddtive;
}

public void setNumberConverter(NumberConverter numberConverter){
this.numberConverter = numberConverter;
}
private NumberConverter numberConverter = new NumberConverter(){
public int toInt(byte[] b, int i, int i_0_) {
String temp = "";
int i_1_ = 0;
for(int i_2_=0;i_2_<i_0_;i_2_++){
temp = Byte.toString(b[i+i_2_]);
if(!("32").equals(temp)){
i_1_ = i_1_*10 + ((b[i+i_2_] & 0xff) - 48);
}
}
return i_1_;
}

};

public byte[] getBytesByLength(InputStream in0,int off,int len,NumberConverter converter,int addtive) throws IOException{
DataInputStream in1 = new DataInputStream(in0);
byte[] lengthBytes = new byte[off+len];
in1.readFully(lengthBytes);
int length = converter.toInt(lengthBytes, off, len);
length+=addtive;
System.out.println("接收客户端传输的字符长度为:"+length);
return lengthBytes;
}

private byte[] resolve(InputStream in) throws IOException {
lengthOffset = 0;
lengthSize = 7;

if(lengthSize>0){
return (getBytesByLength(in, lengthOffset, lengthSize, numberConverter, lengthAddtive));
}else{
throw new IllegalArgumentException("length size must > 0");
}

}

public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9999);
System.out.println("我是服务器,等待客户端连接...");
Socket ssoc = null;
String sendStr = "你好,我是服务器。";
while(true){
ssoc = server.accept();

//测试--重要步骤,关键
MyServer ms = new MyServer();
ms.resolve(ssoc.getInputStream());

//读取客户端的请求信息
InputStream in = ssoc.getInputStream();
DataInputStream din = new DataInputStream(in);
String receiveStr = null;
byte[] rBuf = new byte[7];
din.read(rBuf);
receiveStr = new String(rBuf,0,7);
System.out.println("我是服务器,客户端请求的信息:"+receiveStr);

//响应客户端的请求
OutputStream out = ssoc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);

byte[] buf = sendStr.getBytes();
out.write(buf);

//关闭资源
out.close();
in.close();
ssoc.close();

}
}


}
3.MyClient.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class MyClient {
public static void main(String[] args) throws IOException{
Socket soc = new Socket("127.0.0.1",9999);
System.out.println("我是客户端,连接中...");
String sendStr = "<ap></ap>";

//向服务器发送消息
OutputStream out = soc.getOutputStream();
DataOutputStream dout = new DataOutputStream(out);

byte[] buf = sendStr.getBytes();
out.write(buf);

//读取服务器响应消息
InputStream in = soc.getInputStream();
DataInputStream din = new DataInputStream(in);

String receiveStr = null;
byte[] rBuf = new byte[1024];
din.read(rBuf);
receiveStr = new String(rBuf);
System.out.println("我是客户端,服务器响应的信息:"+receiveStr);

//关闭资源
in.close();
out.close();
soc.close();
}
}


疑问:System.out.println("接收客户端传输的字符长度为:"+length);接收客户端传输的字符长度为:17555239;
怎样通过修改MyClient.java,让此长度正确读取后转换为正确的字符串长度。
MyServer中resolve方法相关都不可以变更。因为这部分是框架中的内容,包括toInt方法都不可变。
请教一下,大神。万分感谢!
...全文
343 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
雨果是程序员 2017-01-05
  • 打赏
  • 举报
回复
有朋友帮忙解答一下吗
雨果是程序员 2016-12-29
  • 打赏
  • 举报
回复
有朋友帮忙解答一下吗 十分感谢

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧