该应用程序由服务器和客户端组成。当我使用 IP (127.0.0.1) 在本地运行应用程序时,聊天工作正常。当然,重点是在在线服务器上运行消息传递程序,以便我可以从另一台计算机连接到它,并充满自豪和喜悦。
我的问题是(我知道这听起来像是我没有做任何研究而且我很懒......相信我我很困惑)我需要对该程序做什么以及我可以在哪里上传它?
我想从本地网络外部访问我的程序,而不仅仅是在我的计算机上测试它。
<小时/> 客户端应用程序:package helloworldC;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class client extends JFrame {
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String serverIP;
private Socket connection;
//constructor
public client(String host){
super("Client");
serverIP = host;
userText= new JTextField();
userText.setEditable(false);
userText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow= new JTextArea();
add(new JScrollPane(chatWindow), BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
}
//connect
public void startRunning(){
try{
connectToServer();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Client terminated") ;
}catch(IOException ioException){
ioException.printStackTrace();
}
finally{
closeCrap();
}
}
//conect to server
private void connectToServer() throws IOException{
showMessage("connecting...\n");
connection = new Socket(InetAddress.getByName(serverIP),6789);//52
showMessage("Connected to: " + connection.getInetAddress().getHostName());
}
// set up streams 53
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n streams are ready");
}
//while chatting 54
private void whileChatting() throws IOException{
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n"+message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n class problen");
}
}while(!message.equals("SERVER -END"));
}
//close the streams and sockets
private void closeCrap(){
showMessage("\n closing sheet");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send messages 56
private void sendMessage(String message){
try{
output.writeObject("CLIENT - "+ message);
output.flush();
showMessage("\nCLIENT -"+message);
}catch(IOException ioException){
chatWindow.append("\n something is wrong");
}
}
//update chatWindow57
private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
chatWindow.append(m);
}
});
}
//permission to type
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
userText.setEditable(tof);
}
});
}
}
服务器:
package helloworld;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
//constructor
public Server(){
super("Messenger");
userText=new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
setSize(300,150);
setVisible(true);
}
// set up and run the server
public void startRunning(){
try{
server = new ServerSocket(6789, 100);
while(true){
try{
//connect and have conversation
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection! ");
}finally{
closeCrap();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException {
showMessage("Waiting for connection...\n");
connection = server.accept();
showMessage("now connected to "+connection.getInetAddress().getHostName()+"\n");
}
//get stream to send and receive data
private void setupStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are good!\n");
}
//during the chat conversation
private void whileChatting() throws IOException{
String message = " You are now connected! ";
sendMessage(message);
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n"+message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n wtf???");
}
}while(!message.equals("CLIENT - END"));
}
// close streams and sockets
private void closeCrap(){
showMessage("\n Closing all...\n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send a message to client
private void sendMessage(String message){
try{
output.writeObject("SERVER - "+message);
output.flush();
showMessage("\nSERVER - " + message);
}catch(IOException ioException){
chatWindow.append("\n ERROR: cant send");
}
}
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
chatWindow.append(text);
}
}
);
}
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
userText.setEditable(tof);
}
}
);
}
}
最佳答案
IP 127.0.0.1 起作用的原因是因为这是一个名为 localhost 的 IP。由于您在机器上托管服务器和客户端,所以工作得很好。
当您想要在本地网络内的一台计算机上运行服务器程序并在另一台计算机上运行客户端程序时(只能从网络内部访问),您必须指向客户端的IP地址到您服务器的 IP 地址。
但是当您想从网络外部访问它时,情况会有点不同。它与以前类似,只是您必须使用端口(在您的情况下为 6789)从路由器端口转发服务器的 IP。这样做的作用是,当您的路由器公共(public) IP 通过端口 6789 接收数据时,它知道将其发送到哪里。如果它不知道,它就会丢弃它。
当您将路由器端口转发为接受来自端口 6789 的数据时。您需要找出您的公共(public) IP 地址。 (这非常容易做到,只需前往: http://www.whatsmyip.org/ ) 然后从本地网络外部在计算机上启动客户端。然后将客户端应连接的 IP 替换为您的公共(public) IP。
注意:您的路由器的公共(public) IP 地址会不时发生变化,因此,如果您有一天尝试连接,您的路由器的公共(public) IP 地址可能已更改。避免这种情况的一种方法是使用 DDNS(动态域名系统),但这是另一个主题。
希望这有帮助!
-卡德
关于java - 我有一个java服务器和客户端聊天程序,但是我可以在哪里上传它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333949/