Java socket server problem
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
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);}
}
}
Here is threader class:
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
Re: Java socket server problem
Quote:
Originally Posted by
Lionlev
Java Socket server with threads (multiple connections)
Code :
for(int i = 1; i<server.ConnectionArray.size(); i++)
Would be REALLY awesome if someone gonna help me
I'm not positive that this is the problem but is Java zero based or one based when it comes to counting? What is the index of the first element in the ArrayList? I see that you're subtracting one sometimes but you're making your life much harder than it needs to be.
Re: Java socket server problem
Quote:
Originally Posted by
stdunbar
I'm not positive that this is the problem but is Java zero based or one based when it comes to counting? What is the index of the first element in the ArrayList? I see that you're subtracting one sometimes but you're making your life much harder than it needs to be.
The code is written in eclipse
And for the truth, the problem is that I am actionscript 2 user and then I came to that I need a server, I choose java. And this is not fully my code, I followed the tutorial. if you want I can send you the link to the video
Re: Java socket server problem
Quote:
Originally Posted by
Lionlev
The code is written in eclipse
And for the truth, the problem is that I am actionscript 2 user and then I came to that I need a server, I choose java. And this is not fully my code, I followed the tutorial. if you want I can send you the link to the video
Well, the code is written in Java. Eclipse is development tool that can help you program in Java. Java is zero based - don't start your loops at one.
Start with that and see what breaks next :)
Re: Java socket server problem
Quote:
Originally Posted by
stdunbar
Well, the code is written in Java. Eclipse is development tool that can help you program in Java. Java is zero based - don't start your loops at one.
Start with that and see what breaks next :)
Ok, I've fixed the loops but didn't change the problem as I expected, but you was right, 2 same loops were looking stupid xD
Re: Java socket server problem
Quote:
Originally Posted by
stdunbar
Well, the code is written in Java. Eclipse is development tool that can help you program in Java. Java is zero based - don't start your loops at one.
Start with that and see what breaks next :)
Sorry for my English tho.. I am Russian..
Re: Java socket server problem
Quote:
Originally Posted by
Lionlev
Why wont you start a new topic?
That post has been moved to its own thread
http://www.javaprogrammingforums.com...r-problem.html