Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Socket Programming By Ratnakar Kamath

2 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.

3 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.

4 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.

5 Communication What the client needs to do  Connect to server.  Send data.  Receive data.  Close the connection.

6 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.

7 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.

8 Methods of java.net.Socket Bind(SocketAddress) close() connect(SocketAddress ) isConnected() isClosed() isBound()

9 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket

10 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket client = new Socket( server, port_id ); server = new ServerSocket( PORT );

11 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket DataOutputStream os; DataInputStream is;

12 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket Socket client = server.accept();

13 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket is = new DataInputStream( client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() ); String line = is.readLine(); os.writeBytes("Hello\n");

14 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket client.close();

15 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket is = new DataInputStream(client.getInputStream() ); os = new DataOutputStream( client.getOutputStream() );

16 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket String line = is.readLine(); os.writeBytes("Hello\n");

17 What happens under the hood? Server Client Create Socket Create streams to connect with client Wait for client Communicate with client Close connection Create Socket object Create streams to connect with server Communicate with server Close socket client.close();

18 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 streams try { 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); } } }

19 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(); }

20 TCP/IP and UDP/IP communications Datagram communication:  Connectionless Stream communication:  Connection oriented UDP or TCP?  UDP is unreliable  Setup time for TCP

21 Sockets and Security Means by which computers communicate Possible attacks  DOS  Buffer overflow Machine becomes a mule. Firewall  Blocks or restricts access to ports.

22 Just scratching the surface! Multiple clients Multi-threaded server sockets Proxies

23 If you didn’t believe what I said, go here…(References) http://java.sun.com/docs/books/tutorial/net working/sockets/index.html http://java.sun.com/docs/books/tutorial/net working/sockets/index.html http://www.javaworld.com/javaworld/jw-12- 1996/jw-12-sockets.html http://www.javaworld.com/javaworld/jw-12- 1996/jw-12-sockets.html http://www.ccs.neu.edu/home/kenb/com13 35/socket_tut.html


Download ppt "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."

Similar presentations


Ads by Google