将两个99个字节TCP数据包合并到一个198个字节TCP数据包中



我需要将两个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[198]更改new byte[99]

最新更新