Computer Science Canada [Tutorial] Networking: Sockets and ServerSockets |
Author: | rizzix [ Sun Mar 13, 2005 7:27 pm ] | ||||||
Post subject: | [Tutorial] Networking: Sockets and ServerSockets | ||||||
The Java networking model is based of the BSD network model, as do most network APIs. This basically invovles the idea of Sockets which are communication points on your computers. A program creates socket of either a client or server type. The server socket establishes a listening port on a computer. The cleint sockets establishes a listening port and connects it self to a server socket. Upon a successful connection attempt the server socket is required to accept the connection, after which a two-way communication system is established. The classes required for networking are in the java.net package. It may be helpful to know that the Socket clases encapsulates the TCP protocol. ServerSocket The methods/constuctors you should be aware of: ServerSocket(int port); This is one of the 4 constuctors and the most commonly used Socket accept(); This method is called, to listen for a client void close(); Close the bounded connection Socket The methods/constuctors you should be aware of: Socket(String host, int port); This is one of 7 constuctors and the most commmonly used boolean isConnected(); Returns true if connected InputStream getInputStream(); Returns the InputStream for reading data OutputStream getOutputStream() Returns the OutputStream for writing data void close(); Closes the connection to server Example
|
Author: | Krabjuice [ Thu Mar 16, 2006 5:04 pm ] | ||
Post subject: | Re: [Tutorial] Networking: Sockets and ServerSockets | ||
Built on the code above, this server allows connections from a user definable amount of clients. However, there are limitations on this specific server: ie: It expects the first client to respond before the second, and the second client before the third..and so on. Another minus is that the server must be "full" before it can start recieving from the clients. A start, perhaps. It works with the above client.
Note, this will not compile in Ready. It requires a IDE running on the latest JDK. Java 1.4 won't work, 1.5 will. Oh--and i'm back, bitches. |
Author: | JMG [ Sun Feb 24, 2008 2:10 am ] |
Post subject: | RE:[Tutorial] Networking: Sockets and ServerSockets |
thanx to both. really helpful |