LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2006, 06:46 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question Multiple connections on one socket? (Java)


I am trying to create an instant messenger server that accepts multiple connections over one socket. The server accepts multiple connections and maintains them, but it only allows communication with one client at a time. Is there some way to check incoming information from all connected clients?

This is my connection thread:
Code:
public void run(){
  try{
    myServerSocket = new ServerSocket(Integer.parseInt(Server.port));	
    while(true){
      communicationSocket = myServerSocket.accept();
				
      out = communicationSocket.getOutputStream();
      in = new BufferedReader(new InputStreamReader(
		communicationSocket.getInputStream()));
				
      prints = new PrintStream(out);
    }
  }
  catch(IOException e){
    System.out.println("IOException: " + e.getMessage());
  }
}
This is continuously being performed for each client that tries to connect.

Last edited by smoothdogg00; 04-09-2006 at 07:10 PM.
 
Old 04-09-2006, 08:33 PM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
I know you can get the IP address and the port number from communicationSocket so you can check to see what IP address has connected to the server. Is that what you are looking for?

Also if this is the code it looks like there is only one socket connection used anyway and it is going to be set to whom ever was the last client to connect to the server. Is that what you what or do you want to have an array of client sockets?

Or do you want only one connection to be active at a time to the server?
 
Old 04-09-2006, 09:15 PM   #3
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
hey, i dont know much about sockets in java, but if you want multiple clients on the same socket, you need to fork off process ( in java, create a new thread)

for example (psuedo code)
Code:
server.c

/*set up the connection */

for(;;) {

       /*waits for new connection */
       socket = accept();
       int pid = fork();
       if(pid == 0) {
              //new process or thread
              setup_steams(socket);  /* setup your streams and send data */
       } else {
             //the parent process
             //dont need the socket - the new thread has it
             close(socket);
             continue;
       }
}
hope this helps

djgerbavor3
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rsh connections : rcmd: socket: Permission denied tiuz Linux - Networking 8 08-21-2009 08:21 AM
java: need lots of socket connections open at once, but hit file limit BrianK Programming 6 01-10-2006 09:02 PM
connection between java socket and unix socket sathishkumar Programming 1 01-10-2006 03:21 PM
Single Socket Vs Multiple Socket Kumar Programming 1 10-05-2005 10:02 AM
Multiple internet connections kojie Linux - Networking 2 12-08-2004 03:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration