LAN上的Java连接 - 拒绝连接:连接

时间:2017-08-02 03:08:37

标签: java eclipse lan

我正在尝试为逃生室制作单向邮件系统,以便您可以在一台计算机上键入邮件,然后它们出现在另一台计算机上。 这适用于具有localhost或127.0.0.1的一台计算机,但在具有两台计算机的LAN上,它会出现Connection refused:Connect。其他一些人问了类似的问题,除了没有任何暗示有效的问题。 我试过了:

  • Ping服务器(已工作)
  • telnet 10.0.0.10 4676(已工作)
  • 在一台计算机上试用(适用于两台计算机)
  • 交换服务器和客户端(不起作用)
  • 更改端口(不起作用)
  • 允许Java通过两个防火墙(不起作用)
  • 在两台计算机上搜索其他防火墙软件(卸载另一个程序但仍然无效)
  • 调整代码以测试它是否正常工作(不起作用)
  • 完全禁用两个防火墙(不起作用)
  • 在一台计算机上运行服务器和客户端,并将localhost / 127.0.0.1更改为计算机的ipv4(适用于两台计算机)
  • 可能还有很多我忘记的事情

如果有人真的想要深入了解代码,我已经用附近的评论标记了所有重要的部分。 (代码是这样编写的,因为稍后可能会扩展程序)

客户端

import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Client extends JFrame{

private JTextField userText;
private JTextArea sentHints;
private ObjectOutputStream output;
private Socket connection;
private String serverIP;
private String finalMessage;
public Client(String host){
    super("EscapeRoomHint-Client");
    userText = new JTextField();
    userText.setEditable(false);
    sentHints = new JTextArea();
    sentHints.setEditable(false);

    userText.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    sendMessage(event.getActionCommand());
                    userText.setText("");
                }
            }
            );
    add(userText,BorderLayout.SOUTH);
    add(new JScrollPane(sentHints));
    setSize(800,400);
    setVisible(true);

}

public void startRunning(){
    try{
        connectToServer();    //Connect to server method
        setupStreams();       //Set up Streams
        sendingMessages();    //While running (basically a loop)
    }catch(EOFException a){
        showMessage("\nServer Closed");
    }catch(IOException a1){
        a1.printStackTrace();
        showMessage("\nServer Closed");
    }finally{
        closeConnection();
    }
}

private void connectToServer() throws IOException{ //connect to server method
    showMessage("\nConnecting...");
    connection = new Socket(InetAddress.getByName(serverIP), 4676); //Connects to the Server here
    showMessage("\nConnected");
}

private void setupStreams() throws IOException{
    output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    showMessage("\nStreams setup\n----------------------------------------\n");
}

private void sendingMessages(){

    userText.setEditable(true);
    while(true){

    }
}

private void closeConnection(){
    showMessage("\nClosing Connection");
    userText.setEditable(false);
    try{
        output.close();
        connection.close();
    }catch(IOException b){
        b.printStackTrace();
    }
}

private void sendMessage(String message){
    try {
        output.writeObject(message);
        output.flush();
        finalMessage = message;
        showMessage(message);
    } catch (IOException e) {
        e.printStackTrace();
    }

}
private void showMessage(String message){
    SwingUtilities.invokeLater(
                new Runnable(){
                    public void run(){
                        sentHints.append("\n" + message);
                    }
                }
            );
}   
}

ClientBoot

import javax.swing.JFrame;

public class ClientBoot extends JFrame{
    public static void main(String[] args){
        Client hintMessenger = new Client("10.0.0.10"); //inputs IP Address here
        hintMessenger.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        hintMessenger.startRunning(); //Run the start running method
    }
}

服务器

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame{
    private JTextArea hintArea;
    private ObjectInputStream input;
    private ServerSocket server;
    private Socket connection;

    public Server(){
        super("EscapeRoomHint-Server");
        hintArea = new JTextArea();
        hintArea.setEditable(false);
        add(hintArea, BorderLayout.SOUTH);
        setSize(800,400);
        setVisible(true);
        }

public void startRunning(){

    try {
        server = new ServerSocket(4676, 2); //Set up server socket (4676)
            while(true){

                try {
                    waitForConnection(); //program sits around and waits for people to join
                    setupStreams(); //if they join then set up streams
                    whileRunning(); //loop waiting to receive messages
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
    } catch (EOFException e) {
        showMessage("Failed To Connect");
    } catch (IOException e1) {
        e1.printStackTrace();
    }finally{
        closeConnection();
    }

}

private void waitForConnection() throws IOException{
    connection = server.accept();            //accept connection here

}
private void setupStreams() throws IOException{
    input = new ObjectInputStream(connection.getInputStream());
}
private void whileRunning() throws IOException{
    String message = "";
    do{
        try{
            message=(String) input.readObject();
                showMessage(message);
        }catch(ClassNotFoundException a){
            showMessage("An error Occurred");
        }
    }while(true);
}
private void closeConnection(){
    try{
        input.close();
        connection.close();
    }catch(IOException a){
        a.printStackTrace();
    }

}
private void showMessage(String message){
    SwingUtilities.invokeLater(
                new Runnable(){
                    public void run(){
                        hintArea.setText(message);
                    }
                }
            );
}
}

ServerBoot

import javax.swing.JFrame;

public class ServerBoot extends JFrame{
    public static void main(String[] args){
        Server hintMessenger = new Server();
        hintMessenger.setDefaultCloseOperation(EXIT_ON_CLOSE);
        hintMessenger.startRunning();         //run the startRunning method
    }

}

我一直试图解决这个问题无数个小时,我终于被打败了。有谁知道可能的修复方法?

编辑:我终于解决了!这不是网络的问题,而是代码中的问题。我通过添加serverIP = host来修复它;在构造函数中。

0 个答案:

没有答案