java socket 发送write_Socket编程write和read对应问题,发送str长度读出来有误

该博客讨论了在Java Socket编程中遇到的问题,即使用DataOutputStream.write方法发送字符串后,使用DataInputStream.read读取时长度出现错误。作者展示了MyServer和MyClient的代码片段,并指出在MyServer的toInt方法中读取到的长度不正确。博主求助如何修改MyClient.java以正确读取并转换为字符串长度,同时强调MyServer的相关代码不能改动。
摘要由CSDN通过智能技术生成

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_

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方法都不可变。

请教一下,大神。万分感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值