?? client.java
字號:
package code.client;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import jpcap.*;
public class Client
{
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
FrameIO f = new FrameIO();
}
}
class FrameIO extends JFrame implements ActionListener,Runnable
{
Socket ClientSocket;
PrintWriter os;
BufferedReader is;
JLabel dev_pmt; //網卡選擇提示框
JComboBox dev_chs; //網卡選擇菜單
JPanel dev_Panel; //網卡選擇Panel
JLabel ip_pmt; //輸入IP地址提示框
JTextField ip_TextField; //輸入IP地址文本框
JPanel ip_Panel; //IP輸入Panel
JPanel btg_Panel; //按鈕組
JPanel input_Panel; //輸入Panel
JPanel qita_Panel; //其他信息Panel
JButton btg_con; //連接服務器
JButton btg_ext; //斷開服務器
JTextArea state_TextArea; //狀態文本框
JScrollPane state_Panel; //狀態文本框滾動Panel
String devices[] = Jpcap.getDeviceDescription(); //獲取網卡列表
int tmr;
int nmb;
int cmpn;
int ClientCount;
long TimeExcursion;
FrameIO()
{
setTitle("Client Window");
this.addWindowListener(new WinAdptClient(this));
UIManager.put("Button.font",new Font("宋體",Font.PLAIN,12));
UIManager.put("Label.font",new Font("宋體",Font.PLAIN,12));
UIManager.put("Lable.color",new Color(255,0,0));
GridBagConstraints gridBag = new GridBagConstraints();
gridBag.fill = GridBagConstraints.BOTH; //以水平、垂直填充方式布局
dev_pmt = new JLabel("請選擇目標網卡:");
dev_chs = new JComboBox();
for(int i=0;i<devices.length;i++)
{
devices[i]=devices[i].replaceAll("\\(Microsoft's Packet Scheduler\\)","");
dev_chs.addItem(devices[i]);
}
dev_chs.addActionListener(this);
dev_chs.setPreferredSize(new Dimension(400,27));//設定大小
dev_Panel = new JPanel(new GridBagLayout());
dev_Panel.setBorder(BorderFactory.createTitledBorder("網卡"));
ip_pmt = new JLabel("請輸入服務器端的IP地址或主機名:");
ip_TextField = new JTextField(10);
ip_TextField.addActionListener(this);
ip_Panel = new JPanel(new FlowLayout());
ip_Panel.setBorder(BorderFactory.createTitledBorder("IP"));
ip_Panel.add(ip_pmt);
ip_Panel.add(ip_TextField);
btg_con = new JButton("開始");
btg_ext = new JButton("退出");
btg_con.addActionListener(this);
btg_ext.addActionListener(this);
btg_Panel = new JPanel(new FlowLayout());
btg_Panel.add(btg_con);
btg_Panel.add(btg_ext);
input_Panel = new JPanel(new GridBagLayout());
input_Panel.setBorder(new TitledBorder( new BevelBorder(BevelBorder.LOWERED),"網絡設置",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
qita_Panel =new JPanel();
qita_Panel.setLayout(new FlowLayout());
qita_Panel.setBorder(new TitledBorder( new BevelBorder(BevelBorder.LOWERED),"其他",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
state_TextArea =new JTextArea();
state_TextArea.setLineWrap(true);
state_Panel =new JScrollPane(state_TextArea);
state_Panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//VERTICAL_SCROLLBAR_ALWAYS);
state_Panel.setBorder(new TitledBorder( new BevelBorder(BevelBorder.LOWERED),"狀態信息",TitledBorder.LEFT,TitledBorder.ABOVE_TOP));
this.setLayout(new GridBagLayout());
this.add(this,input_Panel,gridBag,0,0,3,1,1,0);
this.add(this,state_Panel,gridBag,1,0,5,3,1,6);
this.add(dev_Panel,dev_pmt,gridBag,0,0,2,1,0,0);
this.add(dev_Panel,dev_chs,gridBag,1,0,5,1,0,0);
this.add(input_Panel,ip_Panel,gridBag,0,0,1,1,1,0);
this.add(input_Panel,dev_Panel,gridBag,1,0,1,1,1,0);
this.add(input_Panel,btg_Panel,gridBag,2,0,1,1,1,0);
this.setResizable(false);
setSize(440,370);
show();
}
public void connect(String Access)
{
try{
Access=Access.replaceAll("\n","");
ClientSocket = new Socket(Access,8000); //連向Server主機的8000端口
Thread ct = new Thread(this);
ct.start();
}catch (Exception e){}
}
public void run()
{
try{
is = new BufferedReader(
new InputStreamReader(ClientSocket.getInputStream()));
os = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(ClientSocket.getOutputStream())),true);
os.println("Hello! Wellcome connect to our server!\r");
os.flush();
String s = is.readLine();
while (!s.equals("Bye"))
{
if(s.indexOf("#CommunicationCommand#")>=0)
{
Command(s);
capture cpt = new capture(tmr,nmb,cmpn,ClientCount,TimeExcursion,dev_chs.getSelectedIndex());
}
else
state_TextArea.append(s+"\n");
if(state_TextArea.getText().equals("!Statr Capture!"));
s = is.readLine(); //讀入Client端寫入的下一行信息
}
ClientSocket.close(); //若Client端寫入"Bye"則結束通信
}catch(Exception e){}
}
public void Command(String cmd)
{
tmr =Integer.parseInt(cmd.substring((cmd.indexOf("時間片大小")+5),cmd.indexOf("\t數據包個數")));
nmb =Integer.parseInt(cmd.substring((cmd.indexOf("數據包個數")+5),cmd.indexOf("\t客戶端數量")));
cmpn =Integer.parseInt(cmd.substring((cmd.indexOf("客戶端數量")+5),cmd.indexOf("\t客戶端序號")));
ClientCount =Integer.parseInt(cmd.substring((cmd.indexOf("客戶端序號")+5),cmd.indexOf("\t時間偏移量")));
TimeExcursion =Long.parseLong(cmd.substring((cmd.indexOf("時間偏移量")+5),cmd.indexOf("#END#")));
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btg_con)
connect(ip_TextField.getText());
if(e.getSource()==btg_ext) //捕獲數據包
{
System.exit(0);
}
}
private void add(Container cn,Component c,GridBagConstraints gbc,int y,int x,int w,int h,int gx,int gy)
{ //cn為c的容器
gbc.gridx = x; //水平起始位置
gbc.gridy = y; //垂直起始位置
gbc.gridheight = h; //一行單元格數
gbc.gridwidth = w; //一列單元格數
gbc.weightx = gx; //額外的水平空間
gbc.weighty = gy; //額外的垂直空間
cn.add(c, gbc);
}
}
class WinAdptClient extends WindowAdapter
{
FrameIO m_Parent;
WinAdptClient(FrameIO p)
{
m_Parent = p;
}
public void windowClosing(WindowEvent e)
{
try{ //關閉窗口前先向Server端發送結束信息,并關閉各輸入輸出流與連接
m_Parent.os.println("Bye");
m_Parent.os.flush();
m_Parent.is.close();
m_Parent.os.close();
m_Parent.ClientSocket.close();
m_Parent.dispose();
System.exit(0);
}
catch(Exception ex){}
}
}
class capture implements Runnable
{
Thread cptt;
int time; //時間片方式時,時間片大小(單位為秒)
int number; //數據包方式時,數據包個數
int sts; //客戶端數目
int cc; //本客戶端序號
long TE;
Jpcap jpcap;
int dev;
capture(int t,int n,int s,int c,long tiex,int d)
{
time=t;
number=n;
sts=s;
cc=c;
TE=tiex;
dev=d;
start("MainThread");
}
public void start(String a)
{
Thread thr = new Thread(this,a);
thr.start();
}
public void run()
{
if(Thread.currentThread().getName().equals("MainThread"))
run_MainThread();
else if(Thread.currentThread().getName().equals("JpcapTimerThread"))
run_JpcapTimerThread();
}
private void run_MainThread()
{
try{
Thread.sleep(((time*(cc-1)-TE/1000)%(time*cc))*1000);
String[] lists=Jpcap.getDeviceDescription();
jpcap=Jpcap.openDevice(Jpcap.getDeviceList()[dev],2000,true,20);
if(time<0)
jpcap.loopPacket(number,new Tcpdump());
else
{
for(int k=0;k<sts;k++)
{
if(k==1)
{
start("JpcapTimerThread");
jpcap=Jpcap.openDevice(Jpcap.getDeviceList()[dev],2000,true,20);
jpcap.loopPacket(-1,new Tcpdump());
System.out.println("<=時間片結束");
}
else
{
Thread.sleep(1000*time);
}
if(k==sts-1)
k-=sts;
}
}
}catch(Exception e){}
}
private void run_JpcapTimerThread()
{
try{
System.out.println("=>時間片開始");
for(int i=0;i<time;i++)
{
Thread.sleep(1000);
}
jpcap.close();
}catch(Exception e){}
}
}
class Tcpdump implements JpcapHandler
{
public void handlePacket(Packet packet)
{
System.out.println(packet);
try{
RandomAccessFile ff = new RandomAccessFile("Client.txt", "rw");
ff.seek(ff.length());
ff.writeBytes(packet.toString());
ff.close();
}catch(Exception e){}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -