Results 1 to 8 of 8
  1. #1
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Obtaining refrence of client socket

    Hey everyone,
    I am developing a clients(s)/server application using sockets.I have a problem in getting a refrence to that client which have recently send the message to the server..how can do so?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Obtaining refrence of client socket

    You might want to check the nio package; you can get a 'channel' from a socket and register that channel with a Selector; the Selector can do what you want: select those channels that are ready for a certain operation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Obtaining refrence of client socket

    Sorry but i could not really understand.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Obtaining refrence of client socket

    I can imagine that without you having read the API documentation for the nio package.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Kkenjoy is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: Obtaining refrence of client socket

    How are you doing it?
    Are you using a ServerSocket as a listener waiting for connections?
    If so, are you launching a new thread for each connection accepted by the listener?

    If not, that's a way to do a server for several clients

  6. #6
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Obtaining refrence of client socket

    import java.net.*;
    import java.io.*;
    import java.util.*;
    class MySocketServer
    { public static ServerSocket server;
    public static Socket socket;public static BufferedReader in;
    public static PrintWriter out;Vector v;
    public MySocketServer()
    { try
    {
    server=new ServerSocket(8081);

    v=new Vector();



    Send snd=new Send();
    snd.start();
    recieve rcv=new recieve();
    rcv.start();
    in=new BufferedReader(new InputStreamReader(socket.getInputStream()));

    while(true)
    {
    socket=server.accept();

    v.add(socket);
    }

    }
    catch(IOException e)
    {

    }
    }
    public static void main(String args[])
    {
    new MySocketServer();

    }




    class Send extends Thread
    {
    public void run()
    { System.out.print("Send thread");

    try
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str=null;
    boolean b=true;

    while(b)
    {
    str=br.readLine();
    if(!str.equalsIgnoreCase("END"))
    {
    out.println(str);

    }
    else
    {
    b=false;
    }
    }
    }
    catch(IOException e)
    {

    }
    finally
    { try{
    socket.close();
    }
    catch(IOException e)
    {

    }

    }
    }
    }


    class recieve extends Thread
    {
    public void run()
    {System.out.print("recieve thread");
    try
    {




    while(true)
    {
    String str=in.readLine();/////////////////At this Point i need to know which socket sent the message to the server.....

    for(int i=0;i<v.size();i++)
    {
    Socket nayasck=(Socket)v.get(i);
    if(socket==nayasck)
    {
    i++;
    }
    else
    {
    out=new PrintWriter(nayasck.getOutputStream(),true);
    out.println(str);
    }
    }

    // if(str.equalsIgnoreCase("End"))
    // {
    // break;
    // }
    }

    }

    catch(IOException e)
    {
    System.out.print(e.getMessage());
    }
    finally
    {
    System.out.print("Connection is closing");
    try {server.close();
    }
    catch(IOException e)
    {

    }
    }
    }
    }
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Obtaining refrence of client socket

    Please wrap your code in [code] tags [/code] when posting in order to retain formatting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Obtaining refrence of client socket

    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;
    class MySocketServer 
    {		public static ServerSocket server;
    		public static Socket socket;public static BufferedReader in;
    		public static PrintWriter out;Vector v;
    		public MySocketServer()
    		{	try
    			{
    			server=new ServerSocket(8081);
    			
    			v=new Vector();
    			
    			
    			
    			Send snd=new Send();
    			snd.start();
    			recieve rcv=new recieve();
    			rcv.start();
    			in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    			
    			while(true)
    			{
    				socket=server.accept();
    				
    				v.add(socket);
    			}
    			
    			}
    			catch(IOException e)
    			{
    				
    			}
    		}
    	public static void main(String args[])
    	{	
    		new MySocketServer();
    		
    	}	
    
    
    
    
    class Send extends Thread
    {
    		public void run()
    		{		System.out.print("Send thread");
    				
    				try
    				{
    				BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    				String str=null;
    				boolean b=true;
    				
    				while(b)
    				{
    					str=br.readLine();
    					if(!str.equalsIgnoreCase("END"))
    					{
    						out.println(str);
    						
    					}
    					else
    					{
    						b=false;
    					}
    				}
    				}
    				catch(IOException e)
    				{
    					
    				}
    				finally 
    				{	try{
    					socket.close();
    					}
    					catch(IOException e)
    					{
    						
    					}
    				
    				}
    }
    }
    
    
    class recieve extends Thread
    {	
    	public void run()
    	{System.out.print("recieve thread");
    		try
    		{
    			
    			
    			
    			
    			while(true)
    			{	
    				String str=in.readLine();
    				
    				for(int i=0;i<v.size();i++)
    				{
    					Socket nayasck=(Socket)v.get(i);
    					if(socket==nayasck)
    					{
    						i++;
    					}
    					else
    					{
    						out=new PrintWriter(nayasck.getOutputStream(),true);
    						out.println(str);
    					}
    				}
    			
    //					if(str.equalsIgnoreCase("End"))
    //					{
    //						break;
    //					}
    			}
    			
    		}
    		
    		catch(IOException e)
    		{
    			System.out.print(e.getMessage());
    		}
    		finally
    		{
    			System.out.print("Connection is closing");
    			try {server.close();
    		}
    		catch(IOException e)
    		{
    			
    		}
    		}
    	}
    }
    }

Similar Threads

  1. multithreaded client socket
    By mdpkishore in forum Advanced Java
    Replies: 4
    Last Post: 08-26-2012, 01:31 PM
  2. POP3 Client Socket Problem
    By danielinthebed in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 03:09 PM
  3. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  4. Replies: 1
    Last Post: 11-16-2007, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •