📄 clientcom.java
字号:
import jpcap.*; //引用jpcap包
import jpcap.NetworkInterface;
import jpcap.packet.*;
import java.util.*;
import java.net.*;
import java.io.*;
class clientcom implements PacketReceiver
{
public final static int port=5231;
IPPacket ip;
public void receivePacket(Packet packet)
{
if(packet instanceof IPPacket)
{
int count=1;
if(count%10==0)
System.out.println("第"+count+"次抓包成功!");
count++;
ip=(IPPacket)packet;
System.out.print(ip.src_ip+"\t\t"+ip.dst_ip+"\t"+ip.protocol+"\t"+
ip.length+"\t"+ip.version+"\t\t"+ip.ident+"\t\t"+
ip.rsv_frag+"\t\t"+ip.offset+"\t\t"+ip.hop_limit+"\t\t"+
ip.rsv_tos+"\r\n");
try{
RandomAccessFile rf = new RandomAccessFile("packet.txt", "rw");
rf.seek(rf.length());
rf.writeBytes(ip.src_ip+"\t"+ip.dst_ip+"\t"+ip.protocol+"\t"+
ip.length+"\t"+ip.version+"\t"+ip.ident+"\t"+
ip.rsv_frag+"\t"+ip.offset+"\t"+ip.hop_limit+"\t"+
ip.rsv_tos+"\r\n"); /*向txt文件写入IP的源地址,目标地址,高层协议,长度, 版本号,标识符(序号),标志,段偏移量,数据报的寿命数值,服务型号,数据*/
rf.close();
}
catch (Exception e)
{e.printStackTrace();}
}
}
public static void main(String args[])throws java.io.IOException
{
String str;
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
NetworkInterface deviceName = devices[0];
JpcapCaptor jpcap1 = JpcapCaptor.openDevice(deviceName, 1028, true, 1000);
//即将打开的设备名
//从设备上一次读取的最大字节数
//说明是否将设备设为混杂模式的Boolean值
//超时值
try{
InetAddress add=InetAddress.getByName("127.0.0.1");
//System.out.println("本地电脑连接信息:"+add);
Socket socket=new Socket(add,5230);
System.out.println("Socket状态:"+socket);
InputStream fln=socket.getInputStream();
OutputStream fout=socket.getOutputStream();
InputStreamReader isr=new InputStreamReader(fln);
BufferedReader in=new BufferedReader(isr);
PrintStream out=new PrintStream(fout);
InputStreamReader userisr=new InputStreamReader(System.in);
BufferedReader userin=new BufferedReader(userisr);
while(true)
{
System.out.println("发送消息");
str=userin.readLine();
out.println(str);
System.out.println("等待服务器消息........");
str=in.readLine();
System.out.println("服务器消息:"+str);
if(str.equals("#"))break; //#为退出标志
if(str.equals("1"))
{
System.out.println("服务器要求抓包!");
str="正在抓包中.......";
out.println(str);
jpcap1.loopPacket(1000, new clientcom());
System.out.println("抓包完毕!");
}
System.out.println("发送消息给服务器");
str=userin.readLine();
out.println(str);
if(str.equals("#"))break; //#为退出标志
}
socket.close();
}
catch(Exception e)
{System.out.println("异常来自"+e);}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -