2014-10-03 2 views
0

자바에서 여러 클라이언트를 처리 할 수있는 에코 서버를 구현 중입니다. 다중 클라이언트를 실행하는 경우에도 모든 것이 로컬로 작동합니다. 그러나 다른 컴퓨터에서 클라이언트를 실행하려고하면 출력에자바 에코 서버 연결 시간 초과 문제

java.net.ConnectException: Connection timed out: connect 

이 표시됩니다. 그래서 내 질문은 무엇이 이것을 일으킬 수 있는가? 서버로 작업 한 것은 이번이 처음입니다.

포트는 서버의 클래스는 다음 번호 1100

이다.

ThreadedEchoServer :

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

public class ThreadedEchoServer { 

public static void main(String[] args) throws IOException { 

if (args.length != 1) { 
    System.err.println("Usage: java ThreadedEchoServer <port number>"); 
    System.exit(1); 
} 

    int portNumber = Integer.parseInt(args[0]); 
    boolean listening = true; 

    try (ServerSocket serverSocket = new ServerSocket(portNumber)) { 
     while (listening) { 

      new EchoThread(serverSocket.accept()).start(); 
       System.out.println("connected"); 
     } 
    }catch (IOException e) { 

    System.err.println("Could not listen on port " + portNumber); 
    System.exit(-1); 
    } 
} 
} 

EchoThread :

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

public class EchoThread extends Thread { 
private Socket socket = null; 

public EchoThread(Socket socket) { 
    super("EchoThread"); 
    this.socket = socket; 
} 

public void run() { 

    try (
     PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 
     BufferedReader in = new BufferedReader(
      new InputStreamReader(
       socket.getInputStream())); 
    ) { 
     String inputLine, outputLine; 
     while ((inputLine = in.readLine()) != null) { 
      out.println(inputLine); 
      outputLine = inputLine; 
      if (outputLine.equals("Bye")) //If neccesassy to break using a command 
       break; 
     } 

     socket.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

}

그리고 마지막으로, 에코 클라이언트 :

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

Epublic class EchoClient { 
public static void main(String[] args) throws IOException { 

    if (args.length != 2) { 
     System.err.println(
      "Usage: java EchoClient <host name> <port number>"); 
     System.exit(1); 
    } 

    String hostName = args[0]; 
    int portNumber = Integer.parseInt(args[1]); 

    try (
     Socket echoSocket = new Socket(hostName, portNumber); 
     PrintWriter out = 
      new PrintWriter(echoSocket.getOutputStream(), true); 
     BufferedReader in = 
      new BufferedReader(
       new InputStreamReader(echoSocket.getInputStream())); 
     BufferedReader stdIn = 
      new BufferedReader(
       new InputStreamReader(System.in)) 
    ) { 
     String userInput; 
     while ((userInput = stdIn.readLine()) != null) { 
      out.println(userInput); 
      System.out.println("echo: " + in.readLine()); 
     } 
    } catch (UnknownHostException e) { 
     System.err.println("Don't know about host " + hostName); 
     System.exit(1); 
    } catch (IOException e) { 
     System.err.println("Couldn't get I/O for the connection to " + 
      hostName); 
     System.exit(1); 
    } 
} 

}

+0

방화벽이 있습니까? – Smutje

+0

우리는 방화벽을 모두 껐습니다. – user2833297

+0

하지만 둘 다 같은 로컬 네트워크에 있습니까? 인터넷을 통해 그와 연결되어 있지 않습니까? hamachi – user2504380

답변

0

이 코드는 Oracle Java 및 OpenJDK 1.7.0_55에서 클라이언트와 서버가 하나의 Ubuntu 13.10 및 Windows 7 (하나는 실행중인 클라이언트로 전환되고 다른 하나는 실행중인 서버)에서 실행됩니다.

확실히 네트워크 구성 문제 (방화벽, NAT)가 있습니다. 원격 클라이언트 컴퓨터에서 다른 열린 포트에 대해 telnet host port (즉 telnet megadeth 1100)을 시도하여 시간 초과가되었는지 확인할 수 있습니다.