Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this socket null?
    primarykey
    data
    text
    <p>I am creating a multi client chat server and i am pretty confident that it will work (Correct me if i'm wrong), I have the issue that on the socket that the client connects to is null so the connections can't be created because i use if(Socket != null) so i don't get errors but i will explain my layout real fast. The server starts with a starter class called (LaunchServer) that uses the class object ClientConnector as Minecraft and then starts the method runServer(). Here is the code for this class:</p> <pre><code>public class LaunchServer { public static void main(String[] args) { System.out.println("[Info] Running"); ClientConnector Minecraft = new ClientConnector(); Minecraft.runServer(); } } </code></pre> <p>It's fairly simple. This brings us to the ClientConnector class. Here we start at the method runServer(). Right away we have a try catch block. in that block we print a message that the server is trying to connect to the port 1337. we then create a new ServerSocket called serversocket. We then send a message to the console saying that we have bound to port and that we are awaiting a connection. While true, we create a new Socket socket that equals ServerSocket.accept(); OMG fuck it. Heres the code. you know what it does...</p> <pre><code>import java.util.ArrayList; import java.net.*; import java.io.*; public class ClientConnector { public static ArrayList&lt;Socket&gt; Connections = new ArrayList&lt;Socket&gt;(); 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(); } } } </code></pre> <p>This takes us to the handler class:</p> <pre><code>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 &lt; 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")); } } </code></pre> <p>Read the run method. There you will see the null problem</p> <p>Would the connection get accepted then passed to the hander class? How can a null connection get accepted? My question is how can i fix this problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload