socket connection

send a message to a specific client which is connected to the server

Page 1 of 1

4 Replies - 1537 Views - Last Post: 19 April 2009 - 01:41 PM Rate Topic: -----

#1 Cr.java   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 22-February 08

socket connection

Post icon  Posted 19 April 2009 - 10:25 AM

hello everyone :D

i have followed the tutorial in sun page which is about socket connection;

my intention is to allow just 2 clients to connect to the server;

lets say we have two clients connected to the server client1, and client2;

when one of the two clients (lets say client1 ) send a message to the server, the server forward that message to client2;

the same goes to client2;

so the two questions are:

1) how can i get the list of clients (ids-(IP+port)) which are connected to the server;

2) how can i "send" the message to a specific client;

Is This A Good Question/Topic? 1

Replies To: socket connection

#2 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: socket connection

Posted 19 April 2009 - 11:24 AM

Just keep a List of the clients connected, then when one sends a message, write it to the output stream for the other connections, and so on.
Was This Post Helpful? 0
  • +
  • -

#3 Cr.java   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 22-February 08

Re: socket connection

Posted 19 April 2009 - 11:43 AM

View PostBigAnt, on 19 Apr, 2009 - 10:24 AM, said:

Just keep a List of the clients connected, then when one sends a message, write it to the output stream for the other connections, and so on.


thanks for ur fast reply :D

ok am gonna get the ip address for a specific client by //getInetAddress()


but now how can i write the message to the other connection?

is there is any method that i can use which take the connection IP address and send the message to that specific client?
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: socket connection

Posted 19 April 2009 - 01:25 PM

Just get the specific output/inputstream from the two clients, and when one is read from the input stream write to the other's output stream. Might help to have each of the two clients on diffrerent threads monitoring the input/output for each of the other clients.
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: socket connection

Posted 19 April 2009 - 01:41 PM

View PostCr.java, on 19 Apr, 2009 - 10:43 AM, said:

View PostBigAnt, on 19 Apr, 2009 - 10:24 AM, said:

Just keep a List of the clients connected, then when one sends a message, write it to the output stream for the other connections, and so on.


thanks for ur fast reply :D

ok am gonna get the ip address for a specific client by //getInetAddress()


but now how can i write the message to the other connection?

is there is any method that i can use which take the connection IP address and send the message to that specific client?

Don't need client IP address to talk to it... and even if you had it you cannot "talk" to an IP address to talk to a connection that the server created

Client request for a connection
start a thread to communicate with that client
register it into an ArrayList (connection)

Then when you receive a message send it back to all other clients registered in the ArrayList
You never write to an IP address you need a connection first

This post has been edited by pbl: 19 April 2009 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1