Another socket (multiclient) problem....
Hi!
So basically I've go two classes "Connector" and "ChatServer" it's a socket server with threads. So the problem is when client sends message (loging info, <<username + " " + userpassword>>) the server doesn't get that line until client sends it again but server gets it wrong:
Code:
Username: username
User Password: userpassword username
I don't know how to fix that...
Help?
Connector:
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class Connector
{
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);
ChatServer CHAT = new ChatServer(SOCK);
Thread X = new Thread(CHAT);
X.start();
}
}
catch(Exception X) {System.out.print(X);}
}
}
ChatServer:
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer implements Runnable {
Socket SOCK;
private Scanner INPUT;
private PrintWriter OUT;
String MESSAGE = "";
public ChatServer(Socket X){
this.SOCK = X;
}
char EOF = (char)0x00;
public void CheckConnection() throws IOException
{
if(!SOCK.isConnected())
{
for(int i = 0; i<Connector.ConnectionArray.size(); i++)
{
if(Connector.ConnectionArray.get(i) == SOCK)
{
Connector.ConnectionArray.remove(i);
}
}
for(int i = 0; i <Connector.ConnectionArray.size(); i++)
{
Socket TEMP_SOCK = (Socket) Connector.ConnectionArray.get(i);
System.out.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected");
}
}
}
public void run(){
try
{
try
{
INPUT = new Scanner(SOCK.getInputStream());
OUT = new PrintWriter(SOCK.getOutputStream());
boolean gotusername = false;
boolean gotpassword = false;
boolean goteverything = false;
while(true)
{
CheckConnection();
if(!INPUT.hasNext())
{ return;}
if(goteverything == false)
{
String username = INPUT.next();
String userpassword = INPUT.next();
System.out.println("User's username: " + username +"\n" + "User's password: " + userpassword);
goteverything = true;
}
/*for(int i = 0; i<Connector.ConnectionArray.size(); i++)
{
//Socket TEMP_SOCK = (Socket) Connector.ConnectionArray.get(i);
//PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
//TEMP_OUT.println("Someone: " + MESSAGE + EOF);
//TEMP_OUT.flush();
// System.out.println("Sent to: " + TEMP_SOCK.getLocalAddress().getHostName());
}*/
}
}
finally
{
//SOCK.close();
}
}
catch(Exception X) { System.out.print(X);}
}
}
And client is based on actionscript2 but I don't think that the problem is in the client.
Hope you will help!
Re: Another socket (multiclient) problem....
Your server and client should be in their own true OOP classes, meaning the class is more than a large static main method, but rather each have constructors, non-static variables and non-static methods. Also, please read on Java naming conventions and abide by them, as it is very hard for us to read and understand your code until you do this. Also, have you gone through the sockets tutorials yet because your code posted above suggests that you haven't. Please do this first as it will help you immeasurably.
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Fubarable
Your server and client should be in their own true OOP classes, meaning the class is more than a large static main method, but rather each have constructors, non-static variables and non-static methods. Also, please read on Java naming conventions and abide by them, as it is very hard for us to read and understand your code until you do this. Also, have you gone through the sockets tutorials yet because your code posted above suggests that you haven't. Please do this first as it will help you immeasurably.
I wouldn't posted this if I didn't read and understood my code.. I was sitting few days trying to figure it out how fix the problem.. I am stuck.. So asking people's help. Got nothing else to do.
And also my client project is based on actionscript 2. Not on java
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Lionlev
I wouldn't posted this if I didn't read and understood my code..
You misunderstand me... by using non-standard naming conventions, you're making it difficult for us to understand your code. If we're going to be able to help you, we need to know what's going on.
Quote:
I was sitting few days trying to figure it out how fix the problem.. I am stuck.. So asking people's help. Got nothing else to do.
And that's all well and good, and I hope I'm able to help you.
Quote:
And also my client project is based on actionscript 2. Not on java
Ah, an important detail that should probably be in your original post.
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Fubarable
You misunderstand me... by using non-standard naming conventions, you're making it difficult for us to understand your code. If we're going to be able to help you, we need to know what's going on.
And that's all well and good, and I hope I'm able to help you.
Ah, an important detail that should probably be in your original post.
Oh sorry then.. I am just Russian and don't speak advanced English xD
Anyways, what else do you need to know about the project?
The flash client is fine, I know actionscript almost like my five fingers.. I don't think that the problem is on the client side...
Connector class and ChatServer class are in one project named URserver. As you see, connector starting socket server and the loop that waits for a client to connect. When client connects it starts a thread "chatserver" class, and there I have to get the login information from the client. in this case, client has two input boxes user name and password, when client click on login button, client sends to the server login info like this <<username + " " + userpassword>>
So that's basically it.. and I described the problem already.. I hope that is easier to understand. Thanks for trying to help by the way!
Re: Another socket (multiclient) problem....
Moved from Advanced Java.
db
Re: Another socket (multiclient) problem....
Oh comeon... No one gonna help me?
Re: Another socket (multiclient) problem....
Have you fixed up your code as suggested earlier so it is more readable?
What does "actionscript 2" have to do with this program?
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Norm
Have you fixed up your code as suggested earlier so it is more readable?
What does "actionscript 2" have to do with this program?
As I said, actionscript 2 is a client. I've made a game in flash, and trying to make it online by making java sockets, it is working, but not as I expect.
Re: Another socket (multiclient) problem....
Try debugging the code by changing how you are reading the input to use InputStream class's read() method and print out every byte it reads.
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Norm
Try debugging the code by changing how you are reading the input to use InputStream class's read() method and print out every byte it reads.
I've tried that, it stops printing when it reaches the Code:
if(goteverything == false)
for some reason it doesn't do that loop until I click on login (in client) second time
Re: Another socket (multiclient) problem....
Quote:
it stops printing when it reaches the
Is goteverything true or false when you get to that statement?
Does the loop go forever or does the return statement cause an exit?
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Norm
Is goteverything true or false when you get to that statement?
Does the loop go forever or does the return statement cause an exit?
When the script starts the goteverything is false until it completes the loop
Re: Another socket (multiclient) problem....
I dont' understand what you are saying. Add lots of println statements to the code to show where it executes and hwo the values of the variables change. Copy the print out and the code that generated the print outs.
You need to create a client program for testing with that you can post here for others to test with. Without a test program there is no way to see what is happening.
I changed the port to 8080 and used the browser with your server URL=http://127.0.0.1:8080/
and got this print out: ( I added a loop to read and print what was received)
Running: java Connector
Server launched.
User id 1 connected.
run
User's username: GET
User's password: /
HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: wowbb=%7C%7C%7C%7C-240
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Norm
I dont' understand what you are saying. Add lots of println statements to the code to show where it executes and hwo the values of the variables change. Copy the print out and the code that generated the print outs.
You need to create a client program for testing with that you can post here for others to test with. Without a test program there is no way to see what is happening.
I changed the port to 8080 and used the browser with your server URL=http://127.0.0.1:8080/
and got this print out: ( I added a loop to read and print what was received)
Running: java Connector
Server launched.
User id 1 connected.
run
User's username: GET
User's password: /
HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: wowbb=%7C%7C%7C%7C-240
I have a client lol,
http://www.swfcabin.com/open/1338908375
All this client does is sends <<username + " "+ userpassword>> once you click on "login" button.
Re: Another socket (multiclient) problem....
The browser worked as a simple client for testing the server code. It looks like the server worked.
Can you post the source for client code? I don't like going to 3rd party sites.
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Norm
The browser worked as a simple client for testing the server code. It looks like the server worked.
Can you post the source for client code? I don't like going to 3rd party sites.
You've got adobe flash?
testClient.fla
It's a project source
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Norm
The browser worked as a simple client for testing the server code. It looks like the server worked.
Can you post the source for client code? I don't like going to 3rd party sites.
I am sorry if I am disturbing you, I just really need to fix it..
Re: Another socket (multiclient) problem....
Sorry, I don't have adobe flash.
What did the program read when you changed it to use the read() method to read the bytes one by one and print them as they were read?
Re: Another socket (multiclient) problem....
Quote:
Originally Posted by
Norm
Sorry, I don't have adobe flash.
What did the program read when you changed it to use the read() method to read the bytes one by one and print them as they were read?
Client doesn't read anything right now, it only sends
Do you have skype so it would be easier to talk?