Results 1 to 5 of 5
- 05-27-2012, 07:44 PM #1
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Java socket server problem (Multipleconnections/threads)
[FIXED]
Java Socket server with threads (multiple connections)
Ok so I've got two classes, (main)"server" and "threader". I've got client program and it's working great (the problem is not in the client program) (also it's client is actionsctipt2.0) How it should work:
Launching the server, launching few clients and start chatting with eachother, each client sends message to the server and server sends it to all the clients.
So the problem is that it's working incorrectly. When I launch server and then two same clients, and start chatting, server sends client's messages only to one of the clients. (It's quite hard to explain) and if I launch only one client, server doesn't output any on this client's messages. If I launch 3 clients, only two of them receiving the messages
Here is server class
Here is threader class:Java Code:import java.io.*; import java.net.*; import java.util.*; public class 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 = 2807; ServerSocket SERVER = new ServerSocket(PORT); System.out.println("Server launched."); int userid = 0; while(true) { Socket SOCK = SERVER.accept(); ConnectionArray.add(SOCK); userid += 1; System.out.println("User id " + userid +" connected."); //AddUserName(SOCK); threader CHAT = new threader(SOCK); Thread X = new Thread(CHAT); X.start(); } } catch(Exception X) {System.out.print(X);} } }
Java Code:import java.io.*; import java.net.*; import java.util.*; public class threader implements Runnable { Socket SOCK; private Scanner INPUT; private PrintWriter OUT; String MESSAGE = ""; public threader(Socket X){ this.SOCK = X; } char EOF = (char)0x00; public void CheckConnection() throws IOException { if(!SOCK.isConnected()) { for(int i = 1; i<server.ConnectionArray.size(); i++) { if(server.ConnectionArray.get(i) == SOCK) { server.ConnectionArray.remove(i); } } for(int i = 1; i <server.ConnectionArray.size(); i++) { Socket TEMP_SOCK = (Socket) server.ConnectionArray.get(i-1); PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream()); TEMP_OUT.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected" + EOF); TEMP_OUT.flush(); System.out.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected"); } } } 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: "+ MESSAGE); for(int i = 1; i<server.ConnectionArray.size(); i++) { Socket TEMP_SOCK = (Socket) server.ConnectionArray.get(i-1); PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream()); TEMP_OUT.println("Someone: " + MESSAGE.trim() + EOF); TEMP_OUT.flush(); // System.out.println("Sent to: " + TEMP_SOCK.getLocalAddress().getHostName()); } } } finally { //SOCK.close(); } } catch(Exception X) { System.out.print(X);} } }
Would be REALLY awesome if someone gonna help me, I already was in many forums, but none helped me..Last edited by Lionlev; 05-27-2012 at 11:55 PM.
- 05-27-2012, 08:06 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Java socket server problem (Multipleconnections/threads)
Check your loop boundaries; e.g. in the loop starting at line #58 your code doesn't send anything to the last socket in the ConnectionArray; the other loops have similar problems.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-27-2012, 08:22 PM #3
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
- 05-27-2012, 08:24 PM #4
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
- 05-27-2012, 08:34 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Java Server Socket Problem...
By santafan in forum Advanced JavaReplies: 5Last Post: 12-30-2011, 05:13 PM -
Problem reading from server socket
By glauber in forum Advanced JavaReplies: 5Last Post: 02-17-2011, 12:11 PM -
Java Socket server with C client problem
By rnvrnv in forum NetworkingReplies: 6Last Post: 11-09-2010, 12:47 AM -
Problem on server side (Socket Programming)
By ersachinjain in forum NetworkingReplies: 9Last Post: 05-06-2010, 04:21 PM -
Client Server socket problem - help needed
By kellaw in forum Threads and SynchronizationReplies: 6Last Post: 10-03-2008, 06:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks