Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Multi-User Server__Please need Help!  RSS feed

 
Nik lammar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello There,

I'm trying to develope a Server that handles multi-users.
Users in my code,(That i am posting here), are allready many...
Please, I am trying to communicate every user with each other, but Unfortunatly i can't found the wrong in my code.

My server test is:
===============================================================

import java.net.*;
import java.io.*;
public class TheServer {//extends Thread {
protected int PORT=23;
/** Creates a new instance of AMUD */
public TherServer() {
}


public static void main(String[] args)throws Exception {
TheServer Server=new TheServer();
try{
if ( args.length != 0 )Server.PORT = Integer.valueOf(args[0]).intValue ();
ServerSocket SERVER=new ServerSocket(Server.PORT);
System.out.println("SERVER started on port "+Server.PORT);

boolean header=true;

while (header)
{

Socket CLIENT=SERVER.accept();

testClass PROTOCOL=new testClass(CLIENT);



new Thread(PROTOCOL,"Server"+Thread.activeCount() + 1).start ();


}
System.out.println(Thread.activeCount());
}catch(Exception e){System.out.println(e);}
}

}
===================================================================

And the *protocol* class of the server is:

*******************************************************************
import java.io.*;
import java.net.*;
import java.util.*;
public class testClass implements Runnable {

Socket connection;

testClass(Socket connection) throws Exception {
this.connection=connection;

}


public void run() {

System.out.println("Allocation Input Verifier:System");String command=new String();


boolean state=false;
try{


while(state==false)
{

PrintWriter ud = new PrintWriter(connection.getOutputStream());
BufferedReader ind =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
ud.flush();

ud.print(">");
ud.flush();

command=ind.readLine();




System.out.println("User %"+user+"% :["+command+"] at port :"+connection.getPort());

if(command.compareToIgnoreCase("exit")==0)
{
System.out.println("===========================================================================");
System.out.println("User %"+user+"% :["+command+"] at port :"+connection.getPort()+" DISCONNECTED");
System.out.println("===========================================================================");
state=true;
connection.close();
}

}

}catch(Exception eX){System.out.println("Run Exception + "+eX);}


}


}
*********************************************************************

When I compile Server class, and When i run it, Server is alive. Users Connecting with telnet using:
%> telnet 127.0.0.1 23
So instances of users connecting without problem.

My problem is:
I can't do users to communicate with each other.Something like Chat server.
What should I use?
Can you reply me a easy sample code to understand the way of true multi-users connections?


Thanks for the help, if any!
Nik Lammar.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Idonno much about telnet, but for peer-to-peer chat the simplest approach might be plain sockets. Here's one way to look at it ...

Each client opens a ServerSocket on some port. You can start by trying a predefined port number, but any port is ok.

Each client registers with the server by sending it's IP address & port number. This tells the server that the client is available. Clients might register with a variety of lookup keys, eg chatroom names or topic names that they want to participate in.

One client sends a message to the server. The server looks through its registry, finds all other clients interested in the topic, gets the server socket address for each one and forwards the message.

This is kind of a "painting on cave walls" level approach. You can get a lot fancier fer sure, but maybe it will get some ideas started.
 
Nik lammar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Stan,
I solve the problem, thanks for your time.
I think, P2P connections are more difficult, and especially when there is a server that tracks all user-clients for statistics and other perposes.

Anyway thanks again for your help.
These you've post surelly will help me in my developement proccess later this project!


Nik Lammar
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!