2013-05-03 101 views
0

这可能是一个愚蠢的问题,但在这里。 林编写这个聊天程序,有一个服务器,和客户端可以连接到它。我想在程序中实现私人消息传递,但我不知道如何让客户端直接连接到彼此。对于服务器,我使用了一个在单个端口上运行的ServerSocket。为了实现这一点,我需要将端口转发到服务器。有没有办法让客户端等待连接,而无需将端口转发给他们?使用没有端口转发的ServerSocket?

感谢

回答

1

TCP/IP的整点是一个单一的客户端连接到一个预定义的端口的服务器上。所以是的,你还需要在客户端上有一个ServerSocket接受直接连接。你几乎总会遇到端口转发等问题,这就是为什么UPnP有一天被发明出来的原因。

你想要做的是'点对点'连接,又名P2P,它总是因为它的定义而受到防火墙问题的困扰。因此,通常情况下,特别是聊天时,更容易将中央服务器用作“交换台”服务器并转发私人消息。

1

我已经写了很久以前的multiple client - server应用程序的模板,这可能会帮助您解决您的问题。我认为你的问题的其余部分已经被@Niels回答了;)

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

class ServeConnection extends Thread { 
     private Socket socket = null; 
     private BufferedReader in = null; 
     private PrintWriter out = null; 

     public ServeConnection(Socket s) throws IOException { 

       // init connection with client 
       socket = s; 
       try { 
         in = new BufferedReader(new InputStreamReader(
             this.socket.getInputStream())); 
         out = new PrintWriter(this.socket.getOutputStream(), true); 
       } catch (IOException e) { 
         System.err.println("Couldn't get I/O."); 
         System.exit(1); 
       }     
       start(); 
     } 

     public void run() { 

       System.out.println("client accepted from: " + socket.getInetAddress() 
           + ":" + socket.getPort()); 

      // get commands from client, until is he communicating or until no error 
      // occurs 
       String inputLine, outputLine; 

       try { 
         while ((inputLine = in.readLine()) != null) { 


           System.out.println("request: " + inputLine); 
           outputLine = inputLine;  
           out.println("I've recived "+outputLine);         
       } catch (IOException e) { 
         e.printStackTrace(); 
       } 

       System.out.println("server ending"); 
       out.close(); 
       try { 
         in.close(); 
         socket.close(); 
       } catch (IOException e) { 
         e.printStackTrace(); 
       } 
     } 
} 

class Server { 
     public static void svr_main(int port) throws IOException { 
       ServerSocket serverSocket = null; 
       try { 
         serverSocket = new ServerSocket(port); 
       } catch (IOException e) { 
         System.err.println("Could not listen on port: " + port); 
         System.exit(1); 
       } 

       System.out.println("Server ready"); 

       try { 
         while (true) { 
           Socket socket = serverSocket.accept(); 
           try { 
             new ServeConnection(socket); 
           } catch (IOException e) { 
             System.err.println("IO Exception"); 
           } 
         } 
       } finally { 
         serverSocket.close(); 
       } 
     } 
} 

class Client {  
     static Socket echoSocket = null; 
     static PrintWriter out = null; 
     static BufferedReader in = null; 





     public static void cli_main(int port, String servername) throws 
IOException { 
       try { 
         echoSocket = new Socket(servername, port); 
         out = new PrintWriter(echoSocket.getOutputStream(), true); 
         in = new BufferedReader(new InputStreamReader(
             echoSocket.getInputStream())); 
       } catch (UnknownHostException e) { 
         System.err.println("Don't know about host: " + servername); 
         System.exit(1); 
       } catch (IOException e) { 
         System.err.println("Couldn't get I/O for " + servername); 
         System.exit(1); 
       } 

       System.out.println("Client ready!"); 
       while (true) { 

         inputLine = (in.readLine().toString()); 
         if (inputLine == null) { 
           System.out.println("Client closing!"); 
           break; 
         } 

         // get the input and tokenize it 
         String[] tokens = inputLine.split(" "); 


       } 

       out.close(); 
       in.close(); 
       echoSocket.close(); 
       System.out.println("Client closing"); 
     } 
} 

public class MyClientServerSnippet{ 
     public static void main(String[] args) throws IOException { 
       if (args.length == 0) { 
         System.err.println("Client: java snippet.MyClientServerSnippet<hostname> <port>"); 
         System.err.println("Server: java snippet.MyClientServerSnippet<port>"); 
         System.exit(1); 
       } 
       else if (args.length > 1) {     
         System.out.println("Starting client...\n"); 
         Client client = new Client(); 
         client.cli_main(3049, "127.0.0.1"); 
       } else { 
         System.out.println("Starting server...\n"); 
         Server server = new Server(); 
         server.svr_main(3049); 
       } 
     } 
}