![]() |
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 ! |
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.
|
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:
</off-topic> |
Re: socket Multithreading - & - Obtaining the IP of a client!
Quote:
Quote:
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:
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