java socket:problème de connection
Bonjour,
je voudrais créer mon premier socket en java, mais cela ne fonctionne pas en fait j'ai une classe Echoserver1
Code:
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
| import java.io.*;
import java.net.*;
public class EchoServer1 {
public static void main(String []args){
// if(args.length!=1){
// System.err.println("Aufruf:EchoClient<host>"+"<port>");
// System.exit(1);
// }
String host = "java EchoServer1";
int port = 5000;
// try{
// port = Integer.valueOf(args[1]).intValue();
// }
// catch (NumberFormatException e){
// System.err.println("ungueltige Portnummer");
// System.exit(1);
// }
try{
InetAddress addr = InetAddress.getLocalHost();
ServerSocket server = new ServerSocket(port);
System.out.println("EchoServer1 auf"+addr.getHostName()+" "+addr.getHostAddress()+" :"+port+"gestartet.....");
Socket client = server.accept();
InetAddress clientaddr = client.getInetAddress();
int clientport = client.getPort();
System.out.println("verbindung zu"+clientaddr.getHostAddress()+":"+clientport+"aufgebaut");
try{
BufferedReader sockin = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter sockout= new PrintWriter(client.getOutputStream(),true);
String input;
while((input=sockin.readLine())!=null){
sockout.println("Antwort vom Server:"+input);
}
}
catch (IOException e){
System.err.println(e);
}
finally {
try{
if(client!=null) client.close();
}
catch (IOException e){
}
System.out.println("verbindung zu +"+clientaddr.getHostAddress()+":"+clientport+"abgebaut");
}
}
catch(Exception e){
System.err.println(e);
}
}
} |
et une classe client
Code:
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
| import java.io.*;
import java.net.*;
public class EchoClient {
public static void main (String []args){
// if(args.length!=2){
// System.err.println("Aufruf:EchoClient<host>"+"<port>");
// System.exit(1);
// }
// String host = "locahost";
int port = 1131;
// try{
// port = Integer.valueOf(args[1]).intValue();
// }
// catch (NumberFormatException e){
// System.err.println("ungueltige Portnummer");
// System.exit(1);
// }
Socket socket = null;
try {
socket = new Socket("localhost",port);
System.out.println("Local host:"+socket.getLocalAddress().getHostAddress());
System.out.println("Local port:"+ socket.getLocalPort());
System.out.println("Remote host:"+socket.getInetAddress().getHostAddress());
System.out.println("Remote port:"+socket.getPort());
BufferedReader sockin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter sockout = new PrintWriter(socket.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input,str;
while(true){
System.out.print(">");
input = in.readLine();
if(input==null ||input.equals("q")) break;
sockout.print(input);
str=sockin.readLine();
System.out.println(str);
}
}
catch(Exception e){
System.err.println(e);
}
finally{
try{
if(socket!=null)socket.close();
}
catch (IOException e){
}
}
}
} |
Chaque fois que je compile j'ai un message d'erreur:java.net.ConnectException: Connection refused: connect
apparemment la connection ne peut pas être établi je ne comprends pas pourquoi, tout semble en place