gpt4 book ai didi

java - 如何使我的 Java 服务器可供世界各地使用?

转载 作者:行者123 更新时间:2023-12-01 17:33:40 26 4
gpt4 key购买 nike

我对所有网络知识都很陌生。我最近一直在用Java开发客户端和服务器。客户端按其应有的方式连接到服务器,并从服务器获取响应。只要客户端和服务器在同一网络中的计算机上运行,​​一切都工作正常。当我尝试从与服务器不在同一网络中的计算机连接到服务器时,客户端找不到服务器。

我该怎么做才能使客户端可以从另一个网络连接到服务器?这是否意味着我可以从世界各地连接到服务器?

在这里您可以找到我的服务器代码:

    package zwei;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

public static int Port = 55571;

static ServerSocket ServerSocket;
static Socket ClientSocket;

static PrintWriter out;
static BufferedReader in;

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

}


static void handleConnection(int port) throws IOException {
ServerSocket = new ServerSocket(port);
System.out.println("Waiting for connections...");

ClientSocket = ServerSocket.accept();
System.out.println("Connected to Client!");

out = new PrintWriter(new OutputStreamWriter(ClientSocket.getOutputStream()));
in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));

while(true) {
try {
char[] buffer = new char[200];
int AnzahlZeichen = in.read(buffer, 0, 200);
String message = new String(buffer, 0, AnzahlZeichen);
System.out.println("[Client] " + message);

out.print("Dear Client, I got your message! [ -" + message + "- ]");
out.flush();

if (message.equals("DISCONNECT")) {
System.out.println("Client disconnected");
ServerSocket.close();
ClientSocket.close();
handleConnection(Port);
}
}
catch(Exception e) {
System.out.println("Client disconnected");
ServerSocket.close();
ClientSocket.close();
handleConnection(Port);
}

}

}

}

在这里您可以找到客户端的代码:

package zwei;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Client extends JFrame{

static int port = 55570;
static String ServerAdress = ""; //Here I put the IP-Adress of the computer the server is running on

static Socket ClientSocket;

static PrintWriter out;
static BufferedReader in;

JTextField EnterMessage;
JButton SendMessage;

Client(){
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
this.setSize(700, 700);
//this.setTitle("Vokabeltrainer");

EnterMessage = new JTextField();
EnterMessage.setBounds(50, 50, 500, 50);
this.add(EnterMessage);

SendMessage = new JButton("send");
SendMessage.setBounds(50, 150, 100, 100);
SendMessage.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {
SendMessageToServer(EnterMessage.getText());
ReceiveMessageFromServer();
} catch (IOException e1) {
e1.printStackTrace();
}

}});
this.add(SendMessage);
}

private void connectToServer(String ServerAdress, int port) throws IOException {

try {
this.ClientSocket = new Socket(ServerAdress,port);
System.out.println("Successfully established a connection to the server on port " + port + " .");
}

catch(Exception e) {
System.out.println("An error occured while trying to connect to the server!");
System.out.println("");
e.printStackTrace();
}
}

private void SendMessageToServer(String Nachricht) throws IOException {
out = new PrintWriter(new OutputStreamWriter(ClientSocket.getOutputStream()));
out.print(Nachricht);
out.flush();
}

private void ReceiveMessageFromServer() throws IOException{
in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
char[] buffer = new char[200];
int AnzahlZeichen = in.read(buffer, 0, 200);
String message = new String(buffer, 0, AnzahlZeichen);
System.out.println("[Server] " + message);
}

public static void main(String[] args) throws Exception {
Client client = new Client();
client.connectToServer(ServerAdress, port);
//in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));


}

}

最佳答案

如您所知,每台计算机都有一个 IP 地址和端口号,服务器是一种计算机。如果你想让你的应用程序工作,你应该有一个可以运行java的服务器,或者如果你只需要测试,你应该有你的计算机的静态IP。这样,您的客户端应用程序始终知道应使用哪个端口和哪个 IP 地址来连接到您的服务器应用程序。如果您想测试并且不想支付服务器或静态IP地址费用,请在客户端应用程序中提供用户的IP地址和端口,我认为您可以在两台不同的计算机上运行您的应用程序,但值得一提的是您的设置rooter 可能会影响您的连接。

关于java - 如何使我的 Java 服务器可供世界各地使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61078395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com