150 likes | 425 Views
Java Sockets Tutorial. Rahul Malik Nov 12 , 2005. Outline. What is a Socket?. Benefits of Using Java. Client Sockets Server Sockets Programming Example. What is a Socket?. Okay……so what does that mean? And how does it differ for the Client and Server?.
E N D
Java Sockets Tutorial Rahul Malik Nov12, 2005
Outline • What is a Socket? • Benefits of Using Java • Client Sockets • Server Sockets • Programming Example
What is a Socket? Okay……so what does that mean? And how does it differ for the Client and Server? • Definition: A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.
What is a Socket ? : Client • The client knows the hostname of the machine on which the server is running and the port number to which the server is connected. To make a connection request, the client tries to rendezvous with the server on the server's machine and port. • If the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server
What is a Socket? : Server • The server accepts the connection. • Upon acceptance, the server gets a new socket bound to a different port. • It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client. • The client and server can now communicate by writing to or reading from their sockets.
Java Sockets Pros/Cons Pros: • Classes are already in java.net package that make lower level Beej’s usage unnecessary. • There are separate classes for servers and clients. • Simplicity : It’s very functional and relatively easy to learn in a short amount of time. • Allows us to focus on the development and bugs of our program versus • Inner workings are similar to Beej’s Guide Cons: • Similar to Beej’s Guide
Outline for Sockets Program: • Open a socket. • Open an input stream and output stream to the socket. • Read from and write to the stream according to the server's protocol. • Close the streams. • Close the socket.
Opening a Socket : Client • How do we create a socket? Socket mySocket = new Socket(“hostname”, port); • Slightly easier than Beej’s right? This goes from defining a socket and connecting all in one line!!! • “Hostname” is the host you are trying to connect • The “port” is the port you wish to bind to. • When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root). ) Simple enough?? This is only for clients, servers actually use ServerSockets which listen on specific ports and handle connections versus making them.
Client : Open an input stream to the socket How do I create an Input Stream?? • Start with creating a socket • Socket mySocket = new Socket(“hostname”, port); • For input streams (to receive data from the server) we must utilize the DataInputStream class. • DataInputStream in = new DataStreamInput ( mySocket.getInputStream() ); • The Data Input Stream Class allows you to read lines of text and Java primitive types. It contains methods like read, readChar, readInt, readDouble, and readLine, etc.
Client : Open an output stream How do I create an Output Stream?? • Continuing with the same socket • We must use the DataOutputStream or PrintStream Class to do this. • PrintStream Output = new PrintStream (mySocket.getOutputStream() ); • PrintStream allows for .write() and .println() functions that are very useful. • DataOutputStream however prints one primitive type at a time and contains functions like .writebytes();
Server : Creating the Socket • On the Server side we have our own class, ServerSocket. • To open a Socket to listen on a specific port we would use : • ServerSocket myServer = new ServerSocket(“Port #”); • A server socket is useless without the input and output streams associated with it.
Server : I/O Stream’s • When implementing a server you also need to create a socket object from the ServerSocket in order to listen for and accept connections from clients. • Socket clientSocket = new Socket(); • clientSocket = myServer.accept(); • .accept() returns the socket that is connecting to the server. The advantage of that is that we can use .getInputStream() and .getOutputStream() from the socket class to handle our communication
Server : Closing Up • The fact that we interact with clients on individual sockets is good because : • We only close sockets that are connected to clients • The server is still listening and can receive connections.
Extra Notes : Required Libraries • You must use exception handling try{….}catch{……}finally{……} • try { ServerSocket ss = new ServerSocket(80); } catch (IOException e) {System.err.println(e);} • To do Java Sockets programming you must import(include) : • java.io.* • java.net.*
Practice Java Sockets : Our Task • Task: To create a Echo Client/Server model. The Echo server simply receives data from its client and echoes it back. The Echo server is a well-known service that clients can rendezvous with on port 7. • Sun's Java Echo Client Example