为什么这个套接字为空?

问题描述 投票:0回答:1

我正在创建一个多客户端聊天服务器,我非常有信心它会起作用(如果我错了,请纠正我)。

我有一个问题,客户端连接的套接字为空,因此无法创建连接,因为我使用

if(Socket != null)
,所以我不会收到错误。

服务器以一个名为 (LaunchServer) 的启动类启动,该类使用类对象 ClientConnector 作为 Minecraft,然后启动方法

runServer()

启动服务器类:

public class LaunchServer
{
    public static void main(String[] args)
    {
        System.out.println("[Info] Running");
        ClientConnector Minecraft = new ClientConnector();
        Minecraft.runServer();
    }
}

ClientConnector 类:

import java.util.ArrayList;
import java.net.*;
import java.io.*;

public class ClientConnector
{
    
    public static ArrayList<Socket> Connections = new ArrayList<Socket>();
    
    public static void runServer()
    {
        try
        {   
            System.out.println("[Info] Attempting to bind to port 1337.");
            @SuppressWarnings("resource")
            ServerSocket serversocket = new ServerSocket(1337);
            System.out.println("[Info] Bound to port 1337.");
            System.out.println("[Info] Waiting for client connections...");
            while(true)
            {
                Socket socket = serversocket.accept();
                new ClientHandler(socket).start();
                Connections.add(socket);
            }
            
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

处理程序类:

import java.io.*;
import java.net.*;

public class ClientHandler extends Thread
{
    Socket Socket;

    public ClientHandler(Socket socket)
    {
        socket = Socket;
        System.out.println("[Info] Client connected on port 1337.");
    }
    
    public void run()
    {
        while(true)
        {
            for(int i = 0; i < ClientConnector.Connections.size(); i++)
            {
                
                try
                {
                    if(Socket != null)//This stays null...
                    {
                        ObjectOutputStream Output = new //These can't be created... 
                        ObjectOutputStream(Socket.getOutputStream());
                        ObjectInputStream Input = new ObjectInputStream(Socket.getInputStream());
                        whileChatting(Input, Output);
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void sendMessage(String message, String returnedMessage, ObjectOutputStream out)
    {
        try
        {
            
            if(!message.isEmpty())
            {
                out.writeObject("\247c[Server]\247d " + message);
                out.flush();
                System.out.println("[Chat] Sent: " + message);
            }
            else
            {
                out.writeObject(returnedMessage);
                System.out.println("[Chat] Sent: " + returnedMessage);
            }
            out.flush();
            System.out.println("[Info] Fluching remaining data to stream.");
            System.out.println("\n[Server] " + message);
        }
        catch(IOException ioException)
        {
            System.out.println("[Warning] Error: ioException @ sendMessage line 76.");
        }
     }
    
    public static void whileChatting(ObjectInputStream input, ObjectOutputStream output) throws IOException
    {
        String message = "";
        do
         {
             try
             {
                 message = (String) input.readObject();
                 System.out.println("\n" + message);
                 sendMessage("", message, output); 
             }
             catch(ClassNotFoundException classNotFoundException)
             {
                 System.out.println("[Warning] Error: ClassNotFoundException @ whileChatting line 1-7.");
                 System.out.println("\n idk wtf that user sent!");
             }
         }while(!message.equals("/stop"));
     }
}

run方法存在null问题。

连接会被接受,然后传递给处理程序类吗?空连接如何被接受?

如何解决这个问题?

java sockets null connection serversocket
1个回答
5
投票

问题是由于不推荐的命名约定而导致逻辑错误。您不应该使用关键字命名变量,例如

Socket
变量,并且每个变量都应该有一个可区分的名称。例如不是
socket1, socket2
而是
serverSocket, clientSocket
,因为这将使您和其他人更轻松地阅读和修复您的代码。

改变

Socket Socket;

Socket connectedSocket;

在你的构造函数中

socket = Socket;

connectedSocket = socket;

最后,在你的

run()
方法中改变

if(Socket != null)

if(connectedSocket != null)
© www.soinside.com 2019 - 2024. All rights reserved.