Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用没有端口转发的ServerSocket?_Java_Sockets_Client Server_Port - Fatal编程技术网

Java 使用没有端口转发的ServerSocket?

Java 使用没有端口转发的ServerSocket?,java,sockets,client-server,port,Java,Sockets,Client Server,Port,这可能是一个愚蠢的问题,但问题来了。 我正在写这个聊天程序,里面有一个服务器和可以连接到它的客户端。我想在程序中实现私有消息传递,但我不知道如何让客户端直接相互连接。对于服务器,我使用了一个ServerSocket,它在一个端口上运行。为了让它正常工作,我需要向服务器转发一个端口。有没有一种方法可以让客户端等待连接,而不向其转发端口 感谢TCP/IP的全部要点在于,单个客户端连接到服务器上预定义的端口。因此,是的,您还需要在接受直接连接的客户端上安装一个ServerSocket。您几乎总是会遇到

这可能是一个愚蠢的问题,但问题来了。 我正在写这个聊天程序,里面有一个服务器和可以连接到它的客户端。我想在程序中实现私有消息传递,但我不知道如何让客户端直接相互连接。对于服务器,我使用了一个ServerSocket,它在一个端口上运行。为了让它正常工作,我需要向服务器转发一个端口。有没有一种方法可以让客户端等待连接,而不向其转发端口


感谢

TCP/IP的全部要点在于,单个客户端连接到服务器上预定义的端口。因此,是的,您还需要在接受直接连接的客户端上安装一个
ServerSocket
。您几乎总是会遇到端口转发之类的问题,这就是UPnP有一天被发明的原因


你要做的是“点对点”的连接,也就是P2P,从其定义来看,它总是受到防火墙问题的困扰。因此,使用中央服务器作为“交换台”服务器并中继私人消息通常更容易,尤其是对于聊天而言。

TCP/IP的整个要点是,单个客户端连接到服务器上的预定义端口。因此,是的,您还需要在接受直接连接的客户端上安装一个
ServerSocket
。您几乎总是会遇到端口转发之类的问题,这就是UPnP有一天被发明的原因


你要做的是“点对点”的连接,也就是P2P,从其定义来看,它总是受到防火墙问题的困扰。因此,使用中央服务器作为“交换台”服务器并转发私人消息通常更容易,尤其是对于聊天而言。

不久前我为
多客户端-服务器应用程序编写了一个模板,它可能会帮助您解决问题。我想@Niels已经回答了你其余的问题;)

import java.net.*;
导入java.io.*;
类ServeConnection扩展了线程{
私有套接字=空;
private BufferedReader in=null;
private PrintWriter out=null;
公共ServeConnection(套接字)引发IOException{
//初始化与客户端的连接
插座=s;
试一试{
in=新的BufferedReader(新的InputStreamReader(
this.socket.getInputStream());
out=新的PrintWriter(this.socket.getOutputStream(),true);
}捕获(IOE异常){
System.err.println(“无法获取I/O”);
系统出口(1);
}                
start();
}
公开募捐{
System.out.println(“客户端接受自:”+socket.getInetAddress()
+“:”+socket.getPort());
//从客户端获取命令,直到客户端正在通信或无错误为止
//发生
字符串输入行、输出行;
试一试{
而((inputLine=in.readLine())!=null){
System.out.println(“请求:+inputLine”);
输出线=输入线;
println(“我已收到”+输出行);
}捕获(IOE异常){
e、 printStackTrace();
}
System.out.println(“服务器结束”);
out.close();
试一试{
in.close();
socket.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
类服务器{
公共静态void svr_main(int端口)引发IOException{
ServerSocket ServerSocket=null;
试一试{
serverSocket=新的serverSocket(端口);
}捕获(IOE异常){
System.err.println(“无法侦听端口:“+端口”);
系统出口(1);
}
System.out.println(“服务器就绪”);
试一试{
while(true){
Socket=serverSocket.accept();
试一试{
新的ServeConnection(套接字);
}捕获(IOE异常){
System.err.println(“IO异常”);
}
}
}最后{
serverSocket.close();
}
}
}
类客户端{
静态套接字echoSocket=null;
静态PrintWriter输出=空;
静态BufferedReader in=null;
公共静态void cli_main(int端口,字符串servername)抛出
IOException{
试一试{
echoSocket=新套接字(服务器名、端口);
out=新的PrintWriter(echoSocket.getOutputStream(),true);
in=新的BufferedReader(新的InputStreamReader(
echoSocket.getInputStream());
}捕获(未知后异常e){
System.err.println(“不知道主机:+servername”);
系统出口(1);
}捕获(IOE异常){
System.err.println(“无法获取“+servername”的I/O);
系统出口(1);
}
System.out.println(“客户端就绪!”);
while(true){
inputLine=(in.readLine().toString());
if(inputLine==null){
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);
                }
        }
}