1

Currently I use this little snippet of code to get my java server up and allowing connections to it.

ServerSocket serverSocket = new ServerSocket(8000)
        Socket socket = serverSocket.accept();

        DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
        DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());

I was wondering how I can see all connections to my server from the client. Meaning the ip address connected, and everything about the incoming connection.

Thanks

k9b
  • 1,457
  • 4
  • 24
  • 54
  • I was wondering is this your entire code or a snippet. This snippet would work with only 1 client right? U would need to service each client on a separate thread. – sethu Mar 13 '12 at 03:36

1 Answers1

1

Each time a client connects log the clients ip address etc. on your server, then have the client send a message to the server e.g. "get clients". When the server socket reads this String on the datainputstream send the list of clients and there information back

ghostbust555
  • 2,040
  • 16
  • 29
  • I see, and is it possible to disconnect a current user because of their ip adress? – k9b Mar 13 '12 at 01:41
  • you can just use socket.close() – ghostbust555 Mar 13 '12 at 01:43
  • but what if there are 15 ip addresses connected to the server, can I do it for a single one and not all? – k9b Mar 13 '12 at 01:47
  • yes, don't close the serverSocket, you want to keep a list of each socket as a client connects (ArrayList is usually easiest) then you would say something like - for (Socket s:socketArray){ if (s.getInetAddress()==theInetaddressToDelete) s.close;} – ghostbust555 Mar 13 '12 at 01:49
  • Thanks ghostbust555! Another quick question... are there any reasons that you can think of why i would be able to connect to my server socket from the same network, but not from a different one? (from the same network i use the network IP 192.168..... but from the other network I use the actual internet ip) – k9b Mar 13 '12 at 03:53
  • When I do System.out.println(serverSocket.getLocalSocketAddress().toString()); It outputs 0.0.0.0/0.0.0.0:8000 – k9b Mar 13 '12 at 03:55
  • Yes, if you don't port forward your router it will always block incoming connections outside fo the local network. Google port forwarding – ghostbust555 Mar 14 '12 at 17:24
  • Dont get the server socket address (unless I completely misunderstood the question), you wan't to get the socket address from the Sockets (not ServerSocket) – ghostbust555 Mar 14 '12 at 17:26