1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
public class TCPreceiver implements Runnable {
ServerSocket _socket;
String pcap_message;
Socket pcapclient;
int local_port = 2222;
BufferedReader inFromPcapServer;
@Override
public void run() {
Log.d(TAG, "...TCP SERVER...");
long start = System.currentTimeMillis();
try {
// Set new Socket
_socket = new ServerSocket(local_port);
} catch (IOException e1) {
Log.e(TAG,"Could not listen on port:"+ local_port);
}
Log.i(TAG,"waiting for connection...");
try {
// Accept new TCP client
pcapclient = _socket.accept();
Log.i(TAG,"current time " + System.currentTimeMillis());
} catch (IOException e)
{
Log.e(ERROR,"Can't accept client connection.");
}
try {
inFromPcapServer = new BufferedReader(
new InputStreamReader(pcapclient.getInputStream()));
} catch (IOException e1) {
Log.e(ERROR,"Problem in BufferedReader : ",e1);
}
try {
pcap_message = inFromPcapServer.readLine();
} catch (IOException e1) {
Log.e(ERROR,"Reading from socket failed : ",e1);
}
Log.d(TAG, "New client, address " + pcapclient.getInetAddress()
+ " on " + pcapclient.getPort() + ".");
// boolean isnextline = true;
// while(isnextline){
if(pcap_message != null){
Log.d(TAG, "Received from pcap program \n" + pcap_message);
// Write the message and close the connection
}
// else
// isnextline = false;
// }
Log.d(TAG, "File received from pcap_server");
long end = System.currentTimeMillis();
Log.d(TAG,"time execution : " + (end -start));
try {
pcapclient.close();
_socket.close();
} catch (IOException e) {
Log.e(ERROR,"Problem on closing the sockets", e);
}
}
} |
Partager