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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* Classe dont le but est uniquement d'écouter un socket
*
* @author millie
*
*/
class Reader extends Thread {
private Socket socket;
public Reader(Socket s) {
this.socket = s;
}
public void run() {
InputStream iStream = null;
try {
iStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
iStream)); // pour lire ligne par ligne
String g;
while((g=reader.readLine()) != null) {
System.err.println(g);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
iStream.close(); //on ferme tout
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/**
* @author millie
*
*/
public class Netcat {
/**
* @param args
*/
public static void main(String[] args) {
String host = "www.developpez.com";
int port = 80;
Socket s = null;
OutputStream oStream = null;
BufferedReader iReader = new BufferedReader(new InputStreamReader(
System.in)); //pour permettre la lecture des entrées au clavier
try {
s = new Socket(host, port);
Reader reader = new Reader(s);
reader.start(); //démarrage du thread d'écoute
oStream = s.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
oStream));
while (true) {
String toSend = iReader.readLine(); //lecture d'une ligne entrée au clavier
toSend += '\n'; //ajout d'un retour à la ligne
writer.write(toSend);
writer.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
return ;
} catch (IOException e) {
e.printStackTrace();
}
try {
s.close(); //on ferme tout
oStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager