TCP抓包、模拟发送

抓包工具用的是wireshark

1、服务端代码

package com.example.demo.tcp;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.SocketChannel;

public class Server {

    public static void main(String[] args) {

        Server s = new Server();

        s.doServer();

    }

    public void doServer() {

        try {

            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

            serverSocketChannel.socket().bind(new InetSocketAddress(8081));

            SocketChannel socketChannel = serverSocketChannel.accept();

            ByteBuffer dst = ByteBuffer.allocate(1024);

            FileOutputStream fileOut = new FileOutputStream("d://b.txt");

            FileChannel fileChannel = fileOut.getChannel();

            int len = 0;

            while ((len = socketChannel.read(dst)) != -1) {

                dst.flip();

                fileChannel.write(dst);

                dst.clear();

            }

            try {

Thread.sleep(1000l);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

            fileOut.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

2、客户端代码

package com.example.demo.tcp;

import java.io.FileInputStream;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.UnknownHostException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.channels.SocketChannel;

public class Client {

    public static void main(String[] args) {

        Client c = new Client();

        c.doClent();

    }

    public void doClent() {

        try {

            FileInputStream fileInputStream = new FileInputStream("d://a.txt");

            FileChannel fileChannel = fileInputStream.getChannel();

            ByteBuffer dst = ByteBuffer.allocate(1024*10);

            SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8081));

            int len = 0;

            while ((len = fileChannel.read(dst)) != -1) {

                dst.flip();

                socketChannel.write(dst);

                dst.clear();

            }

            try {

Thread.sleep(1000l);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

            fileInputStream.close();

            socketChannel.close();

        } catch (UnknownHostException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }


   }

}

3、打开wireshark,选择网卡,进入后选择筛选条件tcp.port == 8081



4、启动服务端(server)、启动客户端(client),打开wireshark,看到如下信息。

这些是tcp的握手,挥手。期中红色箭头指向的为发送的内容。



5、选择此条数据,选择Data,复制值。


6、打开jmeter,配置jmeter。删掉之前服务端生成的文件。


7、再次运行服务端,运行jmeter。可看到两个文件一致。成功。


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • BIO与NIO 1.传统BIO (1)特点 面向数据流 阻塞式传输 一个客户端对应一个线程 在客户机增多的情况下,...
    零度微笑_019c阅读 561评论 0 0
  • 一、Socket通道 新的socket通道类可以运行非阻塞模式并且是可选择的。这两个性能可以激活大程序(如网络服务...
    Java架构师笔记阅读 2,499评论 0 3
  • # Java NIO # Java NIO属于非阻塞IO,这是与传统IO最本质的区别。传统IO包括socket和文...
    Teddy_b阅读 626评论 0 0
  • 1、reactor(反应器)模式 使用单线程模拟多线程,提高资源利用率和程序的效率,增加系统吞吐量。下面例子比较形...
    哦00阅读 374评论 0 0
  • 佛亦是众生,众生皆是佛;你对佛如何,应对众生如何;你对众生如何,亦是对佛如何。若山中拜佛,世间作恶,人前行善,背后...
    糖蟹阅读 301评论 0 0