套接字—在客户端计算机和vps服务器java之间进行通信

fiei3ece  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(266)

我知道有很多帖子问如何在客户机和服务器之间通信,但大多数都是使用本地主机的人,我不得不为项目使用一个vps和几个客户机,不幸的是,其他帖子中给出的所有解决方案都不起作用,确实客户机似乎无法连接到我的vps
这是我的客户代码

public IRCClient() throws IOException {
        socket = new Socket(InetAddress.getByAddress(new byte[]{(byte) 185, (byte) 242, (byte) 180,97}), 2406);
        messages = new LinkedBlockingQueue<Object>();
        server = new ConnectionToServer(socket);

        Thread messageHandling = new Thread() {
            public void run() {
                while (true) {
                    try {
                        Object message = messages.take();
                        parse(message);
                        System.out.println("Message Received: " + message);
                    } catch (InterruptedException e) {
                    }
                }
            }
        };

        messageHandling.setDaemon(true);
        messageHandling.start();
    }

    private void parse(Object message) {
        String msg = String.valueOf(message);
        if (msg.contains("-")) {
            if (msg.contains("key")) {
                key = msg.split("-")[1];
            } else if (msg.contains("name")) {
                name = msg.split("-")[1];
            } else if (msg.contains("world")) {
                world = msg.split("-")[1];
            } else if (msg.contains("server")) {
                serverName = msg.split("-")[1];
            } else if (msg.contains("x")) {
                x = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("y")) {
                y = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("z")) {
                z = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("isEntity")) {
                entity = msg.split("-")[1].equalsIgnoreCase("true");
            } else if (msg.contains("ticks")) {
                lTicks = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("end")) {
                if (key.equalsIgnoreCase(MultiPingMod.getKey()) && !name.equalsIgnoreCase(Minecraft.getMinecraft().thePlayer.getName())) {
                    MultiPing m = new MultiPing(name, world, serverName, x, y, z, entity);
                    MultiPingMod.setTime(lTicks);
                    m.render = true;
                    if (MultiPingMod.render.getToRender().containsKey(name)) {
                        MultiPingMod.render.getToRender().get(name).render = false;
                        MultiPingMod.render.getToRender().remove(name);
                    }
                    MultiPingMod.render.addToRender(m);
                }
            }
        }
    }

    public void send(Object obj) {
        server.write(obj);
    }

    private class ConnectionToServer {
        ObjectInputStream in;
        ObjectOutputStream out;
        Socket socket;

        ConnectionToServer(Socket socket) throws IOException {
            this.socket = socket;
            in = new ObjectInputStream(socket.getInputStream());
            out = new ObjectOutputStream(socket.getOutputStream());

            Thread read = new Thread() {
                public void run() {
                    while (true) {
                        try {
                            Object obj = in.readObject();
                            messages.put(obj);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };

            read.setDaemon(true);
            read.start();
        }

        private void write(Object obj) {
            try {
                out.writeObject(obj);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

我是从这个论坛的某个地方得到这个代码的,因为经过多次尝试,服务器上的代码都不一样,这里是:

public ServerListener() {
       debug("Creating the server");
       clientList = new ArrayList<ConnectionToClient>();
       messages = new LinkedBlockingQueue<Object>();
       debug("Client list is created");
       try {
           serverSocket = new ServerSocket(2406);
           debug("opening the port 2406");
       } catch (IOException e) {
         e.printStackTrace();
           debug(e.getMessage());
       }
    debug("creating the accept thread");
       Thread accept = new Thread() {
           public void run() {
    debug("thread created");
               while (true) {
                   try {
                       Socket s = serverSocket.accept();
if(s!=null) {
                       debug("new client joined");
                       clientList.add(new ConnectionToClient(s));
                       debug("new client was accepted ["+ s.getInetAddress() + "/" + s.getPort()+"]");

}
 } catch (IOException e) {
                       e.printStackTrace();
                      debug(e.getMessage());
                   }
               }
           }
       };
       accept.setDaemon(true);
       accept.start();

       Thread messageHandling = new Thread() {
           public void run() {
               while (true) {
                   try {
                       Object message = messages.take();
                       debug("we parse the ping");
                       parse(message);
                       System.out.println("Message Received: " + message);
                   } catch (InterruptedException e) {
                   }
               }
           }
       };

       messageHandling.setDaemon(true);
       messageHandling.start();
   }

   private void parse(Object message) {
       String msg = String.valueOf(message);
       if(msg.contains("-")) {
           if(msg.contains("key")) {
               debug("key detected");
               key = msg.split("-")[1];
           } else if(msg.contains("name")) {
               debug("name detected");
               name = msg.split("-")[1];
           } else if(msg.contains("world")) {
               debug("worldname detected");
               world = msg.split("-")[1];
           } else if(msg.contains("server")) {
               debug("servername detected");
               server = msg.split("-")[1];
           } else if(msg.contains("x")) {
               debug("x detected");
               x = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("y")) {
           debug("y detected");
               y = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("z")) {
                debug("z detected");
               z = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("isEntity")) {
                   debug("entity detected");
               entity = msg.split("-")[1].equalsIgnoreCase("true");
           } else if(msg.contains("ticks")) {
               debug("ticks detected");
               lTicks = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("end")) {
               debug("we got everything detected");
               toSend = new IrcMPING(key, name, world, server, x, y, z, entity, lTicks);
               for(String str : toSend.getArgs()) {

                   sendToAll(str);
               }
               toSend=null;
           }
       }
   }

   public void sendToOne(int index, Object message) throws IndexOutOfBoundsException {
       clientList.get(index).write(message);
   }

   public void sendToAll(Object message) {
       for (ConnectionToClient client : clientList) {
           debug("sending the ping to " + client.socket.getInetAddress());
           client.write(message);
       }
   }

   public void debug(String str) {
           System.out.println("SOROS DEBUG [MULTIPING] : " + str) ;

       }

   private class ConnectionToClient {
       ObjectInputStream in;
       ObjectOutputStream out;
       Socket socket;

       ConnectionToClient(Socket socket) throws IOException {
           this.socket = socket;
           in = new ObjectInputStream(socket.getInputStream());
           out = new ObjectOutputStream(socket.getOutputStream());

           Thread read = new Thread() {
               public void run() {
                   while (true) {
                       try {
                           Object obj = in.readObject();
                           messages.put(obj);
                       } catch (IOException e) {
                           e.printStackTrace();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       } catch (ClassNotFoundException e) {
                           e.printStackTrace();
                       }
                   }
               }
           };

           read.setDaemon(true); // terminate when main ends
           read.start();
       }

       public void write(Object obj) {
           try {

               out.writeObject(obj);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

   }

}

我使用javajar命令从vps(基于debian)运行jar,但是没有做什么,我只在终端上得到这个
https://cdn.discordapp.com/attachments/832578338567487497/833752196017029170/unknown.png //链接到图像
我该怎么做才能让它工作?除了socket,还有什么方法可以让两个jar从远程计算机/服务器进行通信?谢谢你读我的书,如果你想帮助我,也谢谢你

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题