Socket programming in Java (PPTX)


Published on

Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Socket programming in Java (PPTX)

  1. 1. Socket programming in java<br />NarendranThangarajan,<br />@naren_live,<br />II Year, BE, CSE,<br />SSN College of Engg, Chennai.<br />
  2. 2. What started it all..<br />Internet has emerged as a global ubiquitous media for communication<br />It has changed the way we live, learn, enjoy, communicate, interact, engage, etc.<br />To take advantage of this, businesses are ceaselessly trying to offer their services through the internet.<br />So a huge demand for software developers good in creating internet-enabled applications.<br />
  3. 3. Client Server Applications<br />The Web Service is provided by the server <br />and the clients use these services<br />Client<br />Request<br />Server<br />Network<br />Result<br />Client machine<br />Server machine<br />A client, A server, and network<br />This is where sockets come in !!<br />
  4. 4. Sockets<br />Introduced in BSD 4.1 UNIX 1981.<br />Sockets are the endpoints of any communication over the internet.<br />Sockets are identified by socket addresses.<br />Socket Address = IP Address + Port Number<br />
  5. 5. Why IP Address + Port number?<br /><ul><li> IP Address is of the form
  6. 6. Port Number can be anything from 0 to 65,535.</li></li></ul><li>Destination Socket = : 80<br />IP Address – Choose network<br /><br /><br /><br /><br />
  7. 7. IP Address -> MAC Address – Locate the specific system<br />Destination Socket = : 80<br /><br /><br />
  8. 8. Port Number – Process specific<br />Port 10000<br />Port 11000<br />Port 120000<br />
  9. 9. Understanding Ports<br />Gmail<br />YM<br />AVG <br />Update<br />OutLook<br />Express<br />Port 1<br />Port 2<br />Port 3<br />Port 4<br />Transport Layer<br />Packet<br />Port num<br />data<br />Network layer<br />
  10. 10. Thus virtually, sockets are a connection between the two processes in different systems.<br />Eg : Let the socket pairs be<br /> : 80 and : 2000<br /> : 3000 and : 6000<br />
  11. 11. Networking Basics – the larger picture<br /><ul><li>Applications Layer
  12. 12. Standard apps
  13. 13. HTTP
  14. 14. FTP
  15. 15. Telnet
  16. 16. User apps
  17. 17. Transport Layer
  18. 18. TCP
  19. 19. UDP
  20. 20. Network Layer
  21. 21. IP
  22. 22. Link Layer
  23. 23. Device drivers</li></ul>TCP STACK<br />Application<br />(http,ftp,telnet,…)<br />Transport<br />(TCP, UDP,..)<br />Network<br />(IP,..)<br />Link<br />(device driver,..)<br />
  24. 24. Network Basics - Where are these sockets?<br /><ul><li>Applications Layer
  25. 25. Standard apps
  26. 26. HTTP
  27. 27. FTP
  28. 28. Telnet
  29. 29. User apps
  30. 30. Programming Interface:
  31. 31. SOCKETS
  32. 32. Transport Layer
  33. 33. TCP
  34. 34. UDP
  35. 35. Network Layer
  36. 36. IP
  37. 37. Link Layer
  38. 38. Device drivers</li></ul>TCP STACK<br />Application<br />(http,ftp,telnet,…)<br />Sockets<br />Transport<br />(TCP, UDP,..)<br />Network<br />(IP,..)<br />Link<br />(device driver,..)<br />
  39. 39. Now into Socket programming..<br />
  40. 40. Socket Programming with TCP<br />Server starts first..<br />Server Process must be running first<br />Server must have created a socket which welcomes client’s connection. (Welcoming socket)<br />Client contacts server by..<br />Creating Client local TCP socket<br />Specify IP Address and port number of server process.<br />When Client socket is created, the connection is established.<br />When connection is established, server creates a new socket (Connection Socket) to communicate with that client and the Welcoming socket is once again waiting for connections for other clients.<br />
  41. 41. create socket,<br />connect to hostid, port=x<br />create socket,<br />port=x, for<br />incoming request:<br />clientSocket = <br />Socket()<br />welcomeSocket = <br />ServerSocket()<br />wait for incoming<br />connection request<br />connectionSocket =<br />welcomeSocket.accept()<br />send request using<br />clientSocket<br />read request from<br />connectionSocket<br />write reply to<br />connectionSocket<br />read reply from<br />connectionSocket<br />close<br />connectionSocket<br />close<br />clientSocket<br />Client/Server Socket Interaction in TCP<br />Server<br />Client<br />TCP <br />connection setup<br />
  42. 42. Step 1 :<br />Step 2 :<br />Server<br />port<br />Server<br />Connection request<br />port<br />Client<br />port<br />port<br />Client<br />Connection<br />
  43. 43. Types of Sockets in TCP<br />ServerSocket – the socket used by servers<br />Socket – Socket used by clients<br />Create a ServerSocket in the server and make it to wait for connections from Sockets from other clients<br />
  44. 44. The concept of Streams<br />Client output stream<br />Server input stream<br />Server socket<br />Client socket<br />Client input stream<br />Server output stream<br />
  45. 45. Socket Programming with UDP<br />No Connection between client and server.<br />No handshaking<br />The sender has to explicitly mention the IP address and the port of the destination.<br />The server should extract the IP Address of the datagram everytime.<br />Uses DatagramSocket.<br />
  46. 46. Client<br />create socket,<br />port=x, for<br />incoming request:<br />serverSocket = <br />DatagramSocket()<br />create socket,<br />clientSocket = <br />DatagramSocket()<br />Create, address (hostid, port=x),<br />send datagram request <br />using clientSocket<br />read request from<br />serverSocket<br />write reply to<br />serverSocket<br />specifying client<br />host address,<br />port number<br />read reply from<br />clientSocket<br />close<br />clientSocket<br />Client/server socket interaction: UDP<br />Server<br />
  47. 47. Coding time..<br />
  48. 48. Conclusion<br />Socket Programming is very easy in Java.<br />Usually each and every socket is handled by a separate thread in real-time client/server environments.<br />
  49. 49. Queries<br />