![](/img/trans.png)
[英]Why does Java writeBytes method send only one byte in first TCP packet and write method does not?
[英]Merge two 99 byte TCP packets into one 198 byte TCP packet.
我需要将两个TCP数据包合并为一个。 我写了一个套接字仿真器,它从csv文件中读取一行数据,并将每一行数据每秒输出为两个99字节的二进制数据包。 现在,我需要编写另一个模拟器,将这两个99字节的数据包合并为一个198字节的数据包。
到目前为止,这是我整理的内容,它基本上从一个模拟器转发这两个99字节的数据包,并将其作为两个99字节的数据包中继到客户端。 我尝试了几种不同的方法,但似乎无法弄清楚如何将两者合并为一个198字节的数据包。 听起来很简单,但我无法解决,建议将不胜感激。 谢谢
package PacketFuser;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class PacketFuser {
public static void main(String args[]) throws IOException{
//Start server
Socket socket = null;
final ServerSocket ss = new ServerSocket(6666);
System.out.println("Waiting on connection...");
while (ss.isBound()){
try {
socket = ss.accept();
System.out.println("Connected to port: " +socket.toString());
}
catch (IOException e){
}
//Start Client Socket
InetAddress address=InetAddress.getLocalHost();
Socket c1=null;
boolean client = false;
while (client == false){
try{
System.out.println("waiting on Emulator");
Thread.sleep(1000);
c1=new Socket(address, 31982);
client = true;
}
catch (IOException e){}
catch (InterruptedException ex) {}
}
System.out.println("Emulator Connected");
//I need to figure out here how to catch two packets and merge them into one 198 byte packets here.
DataInputStream in = new DataInputStream(s1.getInputStream());
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int pread;
byte[] p1 = new byte[99];
while ((pread = in.read(p1, 0, p1.length)) != -1 ) {
buffer.write(p1, 0, pread);
buffer.flush();
socket.getOutputStream().write(p1);
}
}
}
}
将new byte[99]
更改为new byte[198]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.