
Understanding Java Sockets Communication
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.
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
Java Sockets Brad Vander Zanden
Agenda What are sockets Client/Server interaction Single client/server example Synchronized data objects Multi-threaded servers Multi-threaded clients Detecting client/server shutdown
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
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
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)
Synchronized Data Objects Data Object Processing New accept read write close SocketServer Server Synchronization Point Communication New Socket write read close Client stdin stdout
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?
Multi-Threaded Servers Multi-Threaded KnockKnock Example
Multi-Threaded Client Chat Example New Socket write read close Client client thread stdout stdin
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
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
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