Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Sockets Brad Vander Zanden. Agenda What are sockets Client/Server interaction Single client/server example Synchronized data objects Multi-threaded.

Similar presentations


Presentation on theme: "Java Sockets Brad Vander Zanden. Agenda What are sockets Client/Server interaction Single client/server example Synchronized data objects Multi-threaded."— 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 server client portport portport Connection Request list en

4 Server/Client Interaction (from Jonathan Engelsma’s Java Sockets Tutorial-- https://www.youtube.com/watch?v=aEDV0WlwXTs)Java Sockets Tutorial-- Client Server Processing Synchronization Point Communication

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 Client Server Processing Synchronization Point Communication

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

8 Multi-Threaded Servers Multi-Threaded KnockKnock Example

9 Multi-Threaded Client Chat Example Client

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


Download ppt "Java Sockets Brad Vander Zanden. Agenda What are sockets Client/Server interaction Single client/server example Synchronized data objects Multi-threaded."

Similar presentations


Ads by Google