자바에서 여러 클라이언트를 처리 할 수있는 에코 서버를 구현 중입니다. 다중 클라이언트를 실행하는 경우에도 모든 것이 로컬로 작동합니다. 그러나 다른 컴퓨터에서 클라이언트를 실행하려고하면 출력에자바 에코 서버 연결 시간 초과 문제
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);
}
}
}
방화벽이 있습니까? – Smutje
우리는 방화벽을 모두 껐습니다. – user2833297
하지만 둘 다 같은 로컬 네트워크에 있습니까? 인터넷을 통해 그와 연결되어 있지 않습니까? hamachi – user2504380