雨果是程序员 2017-01-04 03:16 采纳率: 100%
浏览 1938

socket编程write和read方式对应

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 = "";

//向服务器发送消息
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方法都不可变。
请教一下,大神。万分感谢!

  • 写回答

1条回答 默认 最新

  • 雨果是程序员 2017-01-06 06:37
    关注

    大神,帮帮忙 希望有朋友帮忙解答一下

    评论

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波