我需要将两个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);
}
}
}
}
答案 0 :(得分:0)
更改new byte[99]
的{{1}}。