Как отладить "java.net.UnknownHostException" в моем чате Java? - PullRequest
0 голосов
/ 26 мая 2018

У меня есть работающий сервер, но пользователь / клиент не будет подключаться, даже если они находятся на одном и том же порту.Ниже я разместил свой код и не могу найти, что не так, и не позволяю пользователю / клиенту подключиться.

Я не могу найти, какие могут быть проблемы, когда я посмотрел книгу и видео онлайн, чтобы сделатьуверен, что все правильно, но пользователь не подключится.Я не понимаюСуществует четыре различных класса Chatroom_Server, Chatroom_Server_Return, Chat_User_GUI, Chat_User

Это то, что вывод говорит

java.net.UnknownHostException: FIU ChatBUILD SUCCESSFUL (общее время: 13 секунд)

Код:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

/**
*
* @author Alex
*/
public class Chatroom_Server 
{
public static ArrayList<Socket> ConnectionArray = new ArrayList<Socket>();
public static ArrayList<String> CurrentUsers = new ArrayList<String>();

public static void main(String[] args) throws IOException
{
    try
    {
    final int PORT = 4338;
    ServerSocket SERVER = new ServerSocket(PORT);
    System.out.println("Waiting for User to connect");
        while (true)
        {
            Socket SOCK = SERVER.accept();
            ConnectionArray.add(SOCK);

            System.out.println("User connected from  " + SOCK.getLocalAddress().getHostName());

            AddUserName(SOCK);

            Chatroom_Server_Return CHAT = new Chatroom_Server_Return(SOCK);
            Thread X = new Thread(CHAT);
            X.start();   
        }
    }
    catch(Exception X) 
    {
        System.out.print(X);
    }
}  
    public static void AddUserName(Socket X) throws IOException
    {
        Scanner INPUT = new Scanner(X.getInputStream());
        String UserName = INPUT.nextLine();
        CurrentUsers.add(UserName);

        for(int i = 1; i <= Chatroom_Server.ConnectionArray.size(); i++)
        {
            Socket TEMP_SOCK = (Socket) Chatroom_Server.ConnectionArray.get(i-1);
            PrintWriter OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
            OUT.println("#?!" + CurrentUsers);
            OUT.flush();
        } 
    }
}



import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
*
* @author Alex
*/
public class Chatroom_Server_Return implements Runnable 
{
    Socket SOCK;
    private Scanner INPUT;
    private PrintWriter OUT;
    String MESSAGE = "";

    public Chatroom_Server_Return(Socket X)
    {
        this.SOCK = X;
    }

    public void CheckConnection() throws IOException
    {
        if(!SOCK.isConnected())
        {
            for(int i = 1; i <= Chatroom_Server.ConnectionArray.size(); i++)
            {
                if (Chatroom_Server.ConnectionArray.get(i) == SOCK)
                {
                    Chatroom_Server.ConnectionArray.remove(i);
                }
            }
            for(int i = 1; i <= Chatroom_Server.ConnectionArray.size(); i++)
            {
                Socket TEMP_SOCK = (Socket) 
Chatroom_Server.ConnectionArray.get(i-1);
                PrintWriter TEMP_OUT = new 
PrintWriter(TEMP_SOCK.getOutputStream());
                TEMP_OUT.println(TEMP_SOCK.getLocalAddress().getHostName() + 
"Disconnected");
                TEMP_OUT.flush();

                System.out.println(TEMP_SOCK.getLocalAddress().getHostName() 
+ "Disconnected");
            }
        }
    }

    public void run()
    {
        try
        {
            try
            {
                INPUT = new Scanner(SOCK.getInputStream());
                OUT = new PrintWriter(SOCK.getOutputStream());

                while(true)
                {
                    CheckConnection();

                    if(!INPUT.hasNext())
                    {
                        return;
                    }

                    MESSAGE = INPUT.nextLine();

                    System.out.println("User said;" + MESSAGE);

                    for(int i = 1; i <= 
Chatroom_Server.ConnectionArray.size(); i++)
                {
                   Socket TEMP_SOCK = (Socket) 
    Chatroom_Server.ConnectionArray.get(i-1);
                   PrintWriter TEMP_OUT = new 
    PrintWriter(TEMP_SOCK.getOutputStream());
                       TEMP_OUT.println(MESSAGE);
                       TEMP_OUT.flush();
                       System.out.println("Sent to; " + 
    TEMP_SOCK.getLocalAddress().getHostName());
                    }

                }
            }
            finally 
            {
                SOCK.close();
            }
        }
        catch(Exception X)
        {
            System.out.print(X);
        }
    }
}
import javax.swing.*;
import java.io.PrintWriter;
import java.net.*;

/**
*
* @author Alex
*/
public class Chat_User_GUI 
{

private static Chat_User ChatUser;
static String UserName = "Annoymous";

public static JFrame MainWindow = new JFrame();
private static JButton B_ABOUT = new JButton();
private static JButton B_CONNECT = new JButton();
private static JButton B_DISCONNECT = new JButton();
private static JButton B_HELP = new JButton();
private static JButton B_SEND = new JButton();
private static JLabel L_Message = new JLabel("Message: ");
public static JTextField TF_Message = new JTextField(20);
private static JLabel L_Conversation = new JLabel();
public static JTextArea TA_CONVERSATION = new JTextArea();
private static JScrollPane SP_CONVERSATION = new JScrollPane();
private static JLabel L_ONLINE = new JLabel();
public static JList JL_ONLINE = new JList();
private static JScrollPane SP_ONLINE = new JScrollPane();
private static JLabel L_LoggedInAs = new JLabel();
private static JLabel L_LoggedInAsBox = new JLabel();

public static JFrame LogInWindow = new JFrame();
public static JTextField TF_UserNameBox = new JTextField(20);
private static JButton B_ENTER = new JButton("ENTER");
private static JLabel L_EnterUserName = new JLabel("Enter username; ");
private static JPanel P_LogIn = new JPanel();

public static void main (String [] args)
{
  BuildMainWindow();
  Initialize();
}

public static void Connect()
{
    try
    {
        final int PORT = 4338;
        final String HOST = "FIU Chat";
        Socket SOCK = new Socket(HOST,PORT);
        System.out.println("You connected to: " + HOST);

        ChatUser = new Chat_User(SOCK);

        PrintWriter OUT = new PrintWriter(SOCK.getOutputStream());
        OUT.println(UserName);
        OUT.flush();

        Thread X = new Thread(ChatUser);
        X.start();
    }
    catch(Exception X)
    {
        System.out.print(X);
        JOptionPane.showMessageDialog(null,"Server not responding.");
        System.exit(0);
    }
}

public static void Initialize()
{
    B_SEND.setEnabled(false);
    B_DISCONNECT.setEnabled(false);
    B_CONNECT.setEnabled(true);
}

public static void BuildLogInWindow()
{
    LogInWindow.setTitle("Whats your name?");
    LogInWindow.setSize(400,100);
    LogInWindow.setLocation(250,200);
    LogInWindow.setResizable(false);
    P_LogIn = new JPanel();
    P_LogIn.add(L_EnterUserName);
    P_LogIn.add(TF_UserNameBox);
    P_LogIn.add(B_ENTER);
    LogInWindow.add(P_LogIn);

    LoginAction();
    LogInWindow.setVisible(true);
}

public static void BuildMainWindow()
{
   MainWindow.setTitle(UserName + "'s ChatBox");
   MainWindow.setSize(450,500);
   MainWindow.setLocation(220,180);
   MainWindow.setResizable(false);
   ConfigureMainWindow();
   MainWindow_Action();
   MainWindow.setVisible(true);
}

public static void ConfigureMainWindow() 
{
    MainWindow.setBackground(new java.awt.Color(255,255,255));
    MainWindow.setSize(500,320);
    MainWindow.getContentPane().setLayout(null);

    B_SEND.setBackground(new java.awt.Color(0, 0, 255));
    B_SEND.setForeground(new java.awt.Color(255, 255, 255));
    B_SEND.setText("SEND");
    MainWindow.getContentPane().add(B_SEND);
    B_SEND.setBounds(250,40,81,25);

    B_DISCONNECT.setBackground(new java.awt.Color(0, 0, 255));
    B_DISCONNECT.setForeground(new java.awt.Color(255, 255, 255));
    B_DISCONNECT.setText("DISCONNECT");
    MainWindow.getContentPane().add(B_DISCONNECT);
    B_DISCONNECT.setBounds(10,40,110,25);

    B_CONNECT.setBackground(new java.awt.Color(0, 0, 255));
    B_CONNECT.setForeground(new java.awt.Color(255, 255, 255));
    B_CONNECT.setText("CONNECT");
    B_CONNECT.setToolTipText("");
    MainWindow.getContentPane().add(B_CONNECT);
    B_CONNECT.setBounds(130,40,110,25);

    B_HELP.setBackground(new java.awt.Color(0, 0, 255));
    B_HELP.setForeground(new java.awt.Color(255, 255, 255));
    B_HELP.setText("HELP");
    MainWindow.getContentPane().add(B_HELP);
    B_HELP.setBounds(420,40,70,25);

    B_ABOUT.setBackground(new java.awt.Color(0, 0, 255));
    B_ABOUT.setForeground(new java.awt.Color(255, 255, 255));
    B_ABOUT.setText("ABOUT");
    MainWindow.getContentPane().add(B_ABOUT);
    B_ABOUT.setBounds(340,40,75,25);

    L_Message.setText("Message;");
    MainWindow.getContentPane().add(L_Message);
    L_Message.setBounds(10,10,60,20);

    TF_Message.setForeground(new java.awt.Color(0, 0, 255));
    TF_Message.requestFocus();
    MainWindow.getContentPane().add(TF_Message);
    TF_Message.setBounds(70,4,260,30);

    L_Conversation.setHorizontalAlignment(SwingConstants.CENTER);
    L_Conversation.setText("Conversation");
    MainWindow.getContentPane().add(L_Conversation);
    L_Conversation.setBounds(100,70,140,16);

    TA_CONVERSATION.setColumns(20);
    TA_CONVERSATION.setFont(new java.awt.Font("NewTimesRoman", 0, 12));
    TA_CONVERSATION.setForeground(new java.awt.Color(0, 0, 255));
    TA_CONVERSATION.setLineWrap(true);
    TA_CONVERSATION.setRows(5);
    TA_CONVERSATION.setEditable(false);

    SP_CONVERSATION.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    SP_CONVERSATION.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    SP_CONVERSATION.setViewportView(TA_CONVERSATION);
    MainWindow.getContentPane().add(SP_CONVERSATION);
    SP_CONVERSATION.setBounds(10,90,330,180);

    L_ONLINE.setHorizontalAlignment(SwingConstants.CENTER);
    L_ONLINE.setText("Currently Online");
    L_ONLINE.setToolTipText("");
    MainWindow.getContentPane().add(L_ONLINE);
    L_ONLINE.setBounds(350,70,130,16);

    //String[] TestNames = {"John","Ken","Jen","Al"};
    JL_ONLINE.setForeground(new java.awt.Color(0, 0, 255));
    //JL_ONLINE.setListData(TestNames);


SP_ONLINE.setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

SP_ONLINE.setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    SP_ONLINE.setViewportView(JL_ONLINE);
    MainWindow.getContentPane().add(SP_ONLINE);
    SP_ONLINE.setBounds(350,90,130,180);

    L_LoggedInAs.setFont(new java.awt.Font("NewTimesRoman", 0, 12));
    L_LoggedInAs.setText("Currently logged in as");
    MainWindow.getContentPane().add(L_LoggedInAs);
    L_LoggedInAs.setBounds(348,0,140,15);

    L_LoggedInAsBox.setHorizontalAlignment(SwingConstants.CENTER);
    L_LoggedInAsBox.setFont(new java.awt.Font("NewTimesRoman", 0, 12));
    L_LoggedInAsBox.setForeground(new java.awt.Color(255, 0, 0));
    L_LoggedInAsBox.setBorder(BorderFactory.createLineBorder(new 
    java.awt.Color(0, 0, 0)));
    MainWindow.getContentPane().add(L_LoggedInAsBox);
    L_LoggedInAsBox.setBounds(340,17,150,20);
}

public static void LoginAction()
{
    B_ENTER.addActionListener(
            new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    ACTION_B_ENTER();
                }
            }
    );
}

public static void ACTION_B_ENTER()
{
    if(!TF_UserNameBox.getText().equals(""))
    {

        UserName = TF_UserNameBox.getText().trim();
        L_LoggedInAsBox.setText(UserName);
        Chatroom_Server.CurrentUsers.add(UserName);
        MainWindow.setTitle(UserName + "'s ChatBox");
        LogInWindow.setVisible(false);
        B_SEND.setEnabled(true);
        B_DISCONNECT.setEnabled(true);
        B_CONNECT.setEnabled(false);
        Connect();
    }
    else
    {
        JOptionPane.showMessageDialog(null,"Please enter a name");
    }
}
public static void MainWindow_Action()
{
    B_SEND.addActionListener(
           new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                    {
                    ACTION_B_SEND();
                    }
            }
    );

    B_DISCONNECT.addActionListener(
            new java.awt.event.ActionListener()
            {
            public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    ACTION_B_DISCONNECT();
                }
        }
    );

    B_CONNECT.addActionListener(
            new java.awt.event.ActionListener()
            {
            public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    BuildLogInWindow();
                }
        }
    );

    B_HELP.addActionListener(
            new java.awt.event.ActionListener()
            {
            public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    ACTION_B_HELP();
                }
        }
    );  

     B_ABOUT.addActionListener(
            new java.awt.event.ActionListener()
            {
            public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    ACTION_B_HELP();
                }
        }
    );      
}


public static void ACTION_B_SEND()
{
    if(!TF_Message.getText().equals(""));
    {
        ChatUser.SEND(TF_Message.getText());
        TF_Message.requestFocus();
    }
}

public static void ACTION_B_DISCONNECT()
{
  try
  {
      ChatUser.DISCONNECT();
  }
  catch(Exception Y)
  {
      Y.printStackTrace();
  }
}

public static void ACTION_B_HELP()
{
  JOptionPane.showMessageDialog(null,"FIU Chat");
}
}

Chat_User:

package chatroom;

import java.net.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
 *
 * @author Alex
 */
public class Chat_User implements Runnable
{
Socket SOCK;
Scanner INPUT;
Scanner SEND = new Scanner(System.in);
PrintWriter OUT;

public Chat_User(Socket X)
{
    this.SOCK = X;
}

public void run()
{
    try
    {
        try
        {
          INPUT = new Scanner(SOCK.getInputStream());
          OUT = new PrintWriter(SOCK.getOutputStream());
          OUT.flush();
          CheckStream();
        }
        finally
        {
            SOCK.close();
        }
    }
    catch(Exception X)
    {
        System.out.print(X);
    }
}

public void DISCONNECT() throws IOException
{
   OUT.println(Chat_User_GUI.UserName + "has disconnected");
   OUT.flush();
   SOCK.close();
   JOptionPane.showMessageDialog(null, "You disconnected");
   System.exit(0);
}

public void CheckStream()
{
    while(true)
    {
        RECEIVE();
    }
}

public void RECEIVE()
{
    if(INPUT.hasNext());
    {
        String MESSAGE = INPUT.nextLine();
        if(MESSAGE.contains("#?!"))
        {
            String TEMP1 = MESSAGE.substring(3);
                   TEMP1 = TEMP1.replace("[","");
                   TEMP1 = TEMP1.replace("]","");
            String[] CurrentUsers = TEMP1.split(",  ");
            Chat_User_GUI.JL_ONLINE.setListData(CurrentUsers);
        }
        else
        {
          Chat_User_GUI.TA_CONVERSATION.append(MESSAGE + "\n");
        }
    }
}

public void SEND(String X)
{
    OUT.println(Chat_User_GUI.UserName + ": " + X);
    OUT.flush();
    Chat_User_GUI.TF_Message.setText("");
}
}
...