Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   socket Multithreading - & - Obtaining the IP of a client! (http://www.programmingforums.org/showthread.php?t=14465)

bluebarca Nov 15th, 2007 6:11 PM

socket Multithreading - & - Obtaining the IP of a client!
 
hey everybody, hope u r all having a gr8 time!
i have 2 questions:
1st i have a socket lets a call it x that runs on a pc that acts as a server, when multiple clients try to connect and send data to this socket will there be a problem ( will the java code multithread automatically and manage) or should i implement my multithreading.

2nd when one of the users sends an input stream to the server ... is there a method so as i can obtain the IP address of this user ?


i would really appreciate your help guys !

lectricpharaoh Nov 15th, 2007 7:25 PM

Re: socket Multithreading - & - Obtaining the IP of a client!
 
C#, which is quite similar to Java in many ways, supports asynchronous I/O. I'd expect Java to offer similar functionality. Basically, the way it works is you call a method to begin an operation, and supply this method with a reference to (ie, the address of) a 'callback method'. This callback method is invoked when the operation is complete, and you can do whatever you like, such as raising events or what have you.

DaWei Nov 15th, 2007 8:58 PM

Re: socket Multithreading - & - Obtaining the IP of a client!
 
<off-topic>
Welcome to the forum. Please read the rules. I refer specifically to this part:
Quote:

All posts are strongly encouraged to be in full-sentence English. Please do not use "leet" speak or "chatroom" speak. It makes what you're saying hard to understand, especially for those of us who aren't native English speakers.
For the most part, we're mature, big boys and girls here. Have a great time in our community.
</off-topic>

Harakim Nov 16th, 2007 3:37 AM

Re: socket Multithreading - & - Obtaining the IP of a client!
 
Quote:

Originally Posted by bluebarca (Post 136881)
1st i have a socket lets a call it x that runs on a pc that acts as a server, when multiple clients try to connect and send data to this socket will there be a problem ( will the java code multithread automatically and manage) or should i implement my multithreading.

First, this part:
Quote:

Originally Posted by bluebarca (Post 136881)
( will the java code multithread automatically and manage)

The operating system takes care of this. It will create seperate buffers for

For the rest, you have two choices:
1) Use blocking code and threads
2) Use Java's Non-blocking Facilities

Either way, you are going to end up with a seperate Socket (or SocketChannel) for each user that connects to the server. This abstracts the fact that you might have several connections.

ex.
ServerSocket.accept() returns a Socket,
ServerSocketChannel.accept() returns a SocketChannel

You can pass those to a thread that handles the connection, or if you are using non-blocking I/O, you can read from the socket and go on if not enough data was sent.

Quote:

Originally Posted by bluebarca (Post 136881)
2nd when one of the users sends an input stream to the server ... is there a method so as i can obtain the IP address of this user ?

For sockets, use:
String address = ((InetSocketAddress)socket.getRemoteSocketAddress()).getHostName();

For socket channels, use:
String address = ((InetSocketAddress)socketChannel.socket().getRemoteSocketAddress()).getHostName();


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

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC