抓包工具用的是wireshark
1围苫、服務(wù)端代碼
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剂府,選擇網(wǎng)卡拧揽,進(jìn)入后選擇篩選條件tcp.port == 8081
4腺占、啟動服務(wù)端(server)淤袜、啟動客戶端(client),打開wireshark衰伯,看到如下信息铡羡。
這些是tcp的握手,揮手意鲸。期中紅色箭頭指向的為發(fā)送的內(nèi)容烦周。
5、選擇此條數(shù)據(jù)怎顾,選擇Data,復(fù)制值读慎。
6、打開jmeter槐雾,配置jmeter夭委。刪掉之前服務(wù)端生成的文件。
7募强、再次運(yùn)行服務(wù)端株灸,運(yùn)行jmeter∽曜ⅲ可看到兩個文件一致蚂且。成功。