Understanding Java Sockets Communication

java sockets n.w
1 / 12
Embed
Share

Explore the fundamentals of Java sockets, including client/server interaction, synchronized data objects, and multi-threaded servers and clients. Learn about socket communication, server/client synchronization, and examples such as echo client and KnockKnock server. Dive into multi-threaded server and client chat examples for a comprehensive understanding of Java socket programming.

  • Java Socket
  • Communication
  • Synchronization
  • Multi-Threaded
  • Examples

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Java Sockets Brad Vander Zanden

  2. Agenda What are sockets Client/Server interaction Single client/server example Synchronized data objects Multi-threaded servers Multi-threaded clients Detecting client/server shutdown

  3. Socket One end-point of a two-way communication link between two programs running on the network Typically one process is called the server Typically one process is called the client Connection Request p o r t p o r t lis te n client server

  4. Server/Client Interaction (from Jonathan Engelsma s Java Sockets Tutorial-- https://www.youtube.com/watch?v=aEDV0WlwXTs) Processing New accept read write close SocketServer Server Synchronization Point Communication New Socket write read close Client stdin stdout

  5. Single Client/Server Example echo client try with resources only works for Java 1.7 or later hydra machines have Java 1.6 (4/2014)

  6. Synchronized Data Objects Data Object Processing New accept read write close SocketServer Server Synchronization Point Communication New Socket write read close Client stdin stdout

  7. Synchronized Data Objects Not Who s There KnockKnock Server Example serve Knock Knock SENTKNOCK KNOCK WAITING serve KnockKnock Serve Try again. KnockKnock Serve Bye Who s There? Serve Clue No Input matches Clue + who ANOTHER SENTCLUE Input does not match Clue + who serve answer + Want another?

  8. Multi-Threaded Servers Multi-Threaded KnockKnock Example

  9. Multi-Threaded Client Chat Example New Socket write read close Client client thread stdout stdin

  10. Multi-Threaded Client (Pseudo Code) Main Thread New Socket Get SocketRead and SocketWrite objects Create WriteConsoleThread w/ SocketRead obj While (more Stdin) Process/Write to SocketWrite Obj WriteConsoleThread While (more Socket Read Obj) Process line Write to console

  11. Detecting client/server shutdown Occurs when socket read object returns null Socket.close() on other end does not signal other process of termination Server thread: Exit while loop, wrap up processing, and terminate

  12. Multi-Threaded Client (Detecting Shutdown) Main Thread New Socket interrupted = false Get SocketRead and SocketWrite objects Create WriteConsoleThread w/ SocketRead obj While (more Stdin) if (interrupted) break Process/Write to SocketWrite Obj WriteConsoleThread While (more Socket Read Obj) Process line Write to console Notify Main thread of server shutdown by setting interrupted to true

Related


More Related Content