230 likes | 409 Views
Socket Programming. By Ratnakar Kamath. What Is a Socket?. Server has a socket bound to a specific port number. Client makes a connection request. Server accepts. A socket is one endpoint of a two-way communication link between two programs running on the network. Sockets in Java.
E N D
Socket Programming By Ratnakar Kamath
What Is a Socket? • Server has a socket bound to a specific port number. • Client makes a connection request. • Server accepts. • A socket is one endpoint of a two-way communication link between two programs running on the network.
Sockets in Java • Java.net package provides a class , Socket, that implements one side of a two way communication. • Sits on a platform-dependent implementation, hiding the details of any particular system. • Also has ServerSocket class.
Communication • What the server needs to do. • Create a server socket. • Listen for incoming connection attempts. • Communicate with client. • Interact until it is time to close the connection. • Server or client can close the connection. • Go back to listening.
Communication • What the client needs to do • Connect to server. • Send data. • Receive data. • Close the connection.
java.net.Socket • Constructors: • Socket(): Creates an unconnected socket. • Socket(InetAddress address, int port) • Creates a stream socket and connects to the specified port at the specified address. • Socket(InetAddress address, int port, InetAddress localAddr, int localPort): • Creates a socket and connects it to the specified remote address on the specified remote port.
Things I didn’t tell you • The actual work of the socket is performed by an instance of the SocketImpl class. • The abstract class SocketImpl is a common superclass of all classes that actually implement sockets. It is used to create both client and server sockets.
Methods of java.net.Socket • Bind(SocketAddress) • close() • connect(SocketAddress ) • isConnected() • isClosed() • isBound()
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection client = new Socket( server, port_id ); server = new ServerSocket( PORT );
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection DataOutputStream os; DataInputStream is;
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Socket client = server.accept();
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection is = new DataInputStream( client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); String line = is.readLine(); os.writeBytes("Hello\n");
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection client.close();
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection is = new DataInputStream(client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() );
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection String line = is.readLine(); os.writeBytes("Hello\n");
What happens under the hood? Client Server Create Socket object Create streams to connect with server Communicate with server Close socket Create Socket Create streams to connect with client Wait for client Communicate with client Close connection client.close();
Sample Program import java.io.*;import java.net.*;public class echo3 { public static void main(String args[]) {// declaration section:// declare a server socket and a client socket for the server// declare an input and an output stream ServerSocket echoServer = null; String line; DataInputStream is; PrintStream os; Socket clientSocket = null;// Try to open a server socket on port 9999// Note that we can't choose a port less than 1023 if we are not// privileged users (root) try { echoServer = new ServerSocket(9999); } catch (IOException e) { System.out.println(e); } // Create a socket object from the ServerSocket to listen and accept // connections.// Open input and output streamstry { clientSocket = echoServer.accept(); is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream());// As long as we receive data, echo that data back to the client. while (true) { line = is.readLine(); os.println(line); } } catch (IOException e) { System.out.println(e); } }}
Client Program import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket("taranis", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to:taranis."); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: " + in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } }
TCP/IP and UDP/IP communications • Datagram communication: • Connectionless • Stream communication: • Connection oriented • UDP or TCP? • UDP is unreliable • Setup time for TCP
Sockets and Security • Means by which computers communicate • Possible attacks • DOS • Buffer overflow • Machine becomes a mule. • Firewall • Blocks or restricts access to ports.
Just scratching the surface! Multiple clients Multi-threaded server sockets Proxies
If you didn’t believe what I said, go here…(References) • http://java.sun.com/docs/books/tutorial/networking/sockets/index.html • http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html • http://www.ccs.neu.edu/home/kenb/com1335/socket_tut.html