是否可以使用java检查客户端发送到服务器的内容

时间:2014-03-14 20:54:53

标签: java

我正在编写一个基于客户端服务器的java程序。我有两个问题,我希望有人能够回答。 第一个查询,有没有办法检查客户端发送到服务器的内容? 第二个查询是,是否有代码允许服务器检查他们从客户端收到的文件?我一直无法在网上找到答案

这是我的服务器代码

package servers;

import java.io.*;
import java.net.*;

public class Servers {

public static void main(String [] args)throws Exception
{
    try 
    {
        // server variable
        ServerSocket server=new ServerSocket(9999);
        Socket client=server.accept();


        BufferedReader fromClient=new BufferedReader(new InputStreamReader(client.getInputStream()));
        String temp=fromClient.readLine();
        System.out.println(temp);
        System.out.println("Recieved files");
        server.close();


   while ((temp = fromClient.readLine()) != null) 
    {
        System.out.println(temp);
    }

    }
    catch (Exception ex)
      {
      }
}
}

这是我的客户代码

package client;

import java.io.*;
import java.net.*;

public class Client{

public static void main(String [] args)throws Exception
{   
    try 
    {
       File file=new File("C:\\Users\\User\\Desktop\\College 2013\\FYP\\Attribute Data\\Alex.txt"); 
    BufferedReader br=new BufferedReader(new FileReader(file));

    String line;
    while ((line = br.readLine()) != null) 
    {
        System.out.println(line);
    }


    Socket socket=new Socket("Localhost", 9999);
    PrintWriter toServer=new PrintWriter(socket.getOutputStream(),true);
    BufferedReader fromServer=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String toSend,rec="";

       if(!file.exists())
{
    System.out.println("File not found terminating program.");

} 

br.close();
toServer.println("this is the end");
System.out.println("Finished sending file.");
System.out.println("Begining to receive Server output.");


 while (!(rec = fromServer.readLine()).equals("this is the end"))
{
    System.out.println(rec);
}

toServer.close();
br.close();
socket.close();

return;
} 
catch (Exception e) 
    {

    } 
    }
}

非常感谢任何建议

3 个答案:

答案 0 :(得分:0)

使用tcpdumpwireshark等程序来观察来回的网络流量。我通常从命令行使用tcpdump;但是,wireshark有一个GUI,也许有些人会更好。

我认为最好使用单独的程序来观察流量。这样您就不必使程序复杂化,并且外部程序也可以应用于任何产生网络流量的程序。

答案 1 :(得分:0)

您还可以设置拦截代理,例如OWASP Hatkit Proxy Project

答案 2 :(得分:0)

tcptrace工作得非常好,非常小,仅适用于Windows http://www.pocketsoap.com/tcptrace/