Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-11-2008, 06:00 PM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
Multi Client TCP or UDP
Hi, I want to make a messenger feature on one of my programs.

one thing I came across is that while I can connect one client to my server, I don't know how to make the server respond to multiple clients.

what is the best way to make a multi client feature?

I don't care whether it's tcp or udp, because I have made a version of my program for both tcp networking and udp.

thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-13-2008, 08:18 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
What have you tried?

If the interaction with the server is "quick enough" you can just let one server handle it.

wait for command, Accept it, do it, format a response, send it, close the connection, repeat.

If you take "too long" then the usual approach is to setup a thread pool, and kick off a thread for each request.

UDP or TCP has nothing to do with this.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-15-2008, 07:29 AM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
//------------------------------------------------------------------------
public void run() {

socket = new DatagramSocket(1985);
while(b)
{

//receive message from client.
byte[] buf = new byte[100];
DatagramPacket packet1 = new DatagramPacket(buf,buf.length);
socket.receive(packet1);


//declare string message.
String message = new String(packet1.getData(),0 ,packet.getLength());

//declare bytes for return message.
byte[] buf2 = new byte[100];
buf2 = message.getBytes();

//get address and port from packet1.
InetAddress address = packet1.getAddress();
int port = packet1.getPort();
DatagramPacket packet2 = new DatagramPacket(buf2,buf2 .length,address,port);

//send packet2.
socket.send(packet2);



}
socket.close();


}
//------------------------------------------------------------------------

This is my server- with the try and catch statements omitted, because they make it messy and confusing to analyze.
It is in a thread.

yes, basic...but its what I need for my messenger at the moment.

any ideas on how I would make this multi client?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-15-2008, 03:47 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
What happens when a second client connects to your server?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-15-2008, 09:24 PM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
the script above, which is my server, only returns the message to the sender.

a thousand clients can be using this server, but my server will only return the message to the client that sends a message to the server.

as you can see, it uses the getAddress() method to get the address of the sender.



So more accurately, my question is: how can I make the server get a message from one single client, and send it to all clients that have connected?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-15-2008, 09:42 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
my server will only return the message to the client that sends a message to the server.
How will the server get the addresses of the other clients?
If the server does have the addresses of the others, say in a table, then it will need to connect to them. They will each have to be servers to THE server who would be acting like a client. Ie the clients will wait for THE server to connect to them and send them a message.

I'd think you'd want it so that each client connects to the server(logins) and then can receive messages from the server. A client after logging in would send a message to the server that was to be sent to other clients and the server would have connections to those clients and be able to send them the message.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-15-2008, 10:16 PM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
//------------------------------------------------------------------------
public void run()
{
//create socket

socket = new DatagramSocket(5000);

//create arrays: one for addresses, 50
//slots long, which means the server can serve 50 users.
//slot 0 is not used.

address = new InetAddress[51];
port = new int[51];
int k;
for(k = 0; k<=50; ++k)//set the arrays to default values
{

address[k] = null;


}


//this is how much space has been taken in the arrays.


//listen for packet, receive it, then do stuff.
while(b)
{
//receive message from client.
byte[] buf = new byte[3];
DatagramPacket packet = new DatagramPacket(buf,buf.length);

socket.receive(packet);

//get the address of the client that sent the message.
InetAddress current_address = packet.getAddress();

//get string from packet.
String message_receive = new String(packet.getData(),0 ,packet.getLength());

//test the packet's message.

if(message_receive.equals("add"))
{
boolean bool = true;
int c;
for(c=0; c<=50;++c)
{
if((address[c] == null) && (bool = true))
{

address[c] = current_address;
bool = false;
}
}

}
else if(message_receive.equals("sub"))
{
int c;
for(c=0; c<=50;++c)
{
if(address[c] == current_address)
{
address[c] = null;

}

}
}



}
socket.close();

}//end while loop/listener
}//end method run().
//------------------------------------------------------------------------
what you see above is a second server I made to run simultaneously with the first.

what it does is, when the client first logs on, it sends a message to this second server, and the second server puts the address into an array.

when i start the two servers, they work fine. however, when i start the client, and try to access the array of addresses, I get this error:

"
Exception in thread "main" java.lang.NullPointerException

"

pretty much what this second server is supposed to do is store the addresses so that my first server, when add the feature to do so, will be able to use the array of Inetaddresses from the second server,sending a message to every address stored in the array.

i plan to do this by writing the firstserver to make a datagram for all 50 addresses in the second server's array.

however, because of the error above, am I doing something wrong in accessing my array from the second server?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-15-2008, 11:46 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
There is an object that is NULL in your code.
Copy and post the FULL text of the error message. The error message will have a line number in your program that you can look at to see what object was null.

I'd change the design to have only one server. When it receives a login message from a client, it should create an object for that client(Define a new class here) and save it in some kind of collection, like an ArrayList. Then when the server gets a message to send to all clients, it can go thru the collection of logged in clients, get their addresses and send them the message.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-17-2008, 06:10 AM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
it works!
I fixed it! WAHOO!

thankyou very much, norm and fishtoprecords for your input.




WAHOO!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagram Client and Server, client timer question saru88 Networking 1 10-05-2008 05:12 PM
Help me to do Multi View from one Document Nicz AWT / Swing 4 06-08-2008 04:23 PM
Identify Client in Socket Client Server Application masadjie Networking 1 12-20-2007 11:18 AM
How come multi thread don't look like it? jkhoa Threads and Synchronization 1 09-22-2007 06:25 AM
Axiomatic Multi-Platform C 1.6.4 JavaBean Java Announcements 0 06-22-2007 01:49 AM


All times are GMT +3. The time now is 01:21 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org