Results 1 to 9 of 9
Thread: Multi Client TCP or UDP
- 10-11-2008, 04:00 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 48
- Rep Power
- 0
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
- 10-13-2008, 06:18 AM #2
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.
- 10-15-2008, 05:29 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 48
- Rep Power
- 0
//------------------------------------------------------------------------
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?
- 10-15-2008, 01:47 PM #4
What happens when a second client connects to your server?
- 10-15-2008, 07:24 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 48
- Rep Power
- 0
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?
- 10-15-2008, 07:42 PM #6
How will the server get the addresses of the other clients?my server will only return the message to the client that sends a message to the server.
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.
- 10-15-2008, 08:16 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 48
- Rep Power
- 0
//------------------------------------------------------------------------
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?
- 10-15-2008, 09:46 PM #8
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.
- 10-17-2008, 04:10 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
Help me to do Multi View from one Document
By Nicz in forum AWT / SwingReplies: 4Last Post: 06-08-2008, 02:23 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM -
How come multi thread don't look like it?
By jkhoa in forum Threads and SynchronizationReplies: 1Last Post: 09-22-2007, 04:25 AM -
Axiomatic Multi-Platform C 1.6.4
By JavaBean in forum Java SoftwareReplies: 0Last Post: 06-21-2007, 11:49 PM


LinkBack URL
About LinkBacks

Bookmarks