Professional Documents
Culture Documents
TCP/IP Threads
Network Protocols
7 6 5 4 3 2 1 OSI
Application Presentation Session Transport Network Data Link Physical reference model
Ordered in layers Each layer oers some services Each layer only uses the services oered by the layer below. Applications only care about application layer
Claus Traulsen
Slide 1
Claus Traulsen
Slide 3
TCP/IP Threads
TCP/IP Threads
TCP/IP
7 6 5 4 3 2 1 Application Transport Network Host-to network TCP/IP reference model We will use TCP/IP Cares about routing, failure detection, . . . Clients use sockets to connect to server Independent of programming language, OS Read and write on network resources Have to dene a protocol
TCP/IP
Threads
Claus Traulsen
Slide 2
Claus Traulsen
Slide 4
TCP/IP Threads
TCP/IP Threads
Sockets
S e rv e r s o c k e t b in d lis te n a c c e p t re a d w r ite c lo s e
Specied by host-address plus port Server waits for clients to connect Multiple clients can connect to one server
TCP/IP is directly supported by Java Classes java.net.socket and java.net.ServerSocket Only need host-address and port Oer input- and output-stream just like le IO Still have to interpret the data
Claus Traulsen
Slide 5
Claus Traulsen
Slide 7
TCP/IP Threads
TCP/IP Threads
Further Information
Example
Data
Server stores data (x,y) Invariant: y = 2x Client sends command to server: + increment - decrement Server returns current values
A. Tannenbaum, Computer Networks Java ist auch eine Insel, Chap 9, 16 Lectures by Prof. Luttenberger
Internet Communications Computer Networks
-x : int = 0 -y : int = 0 +inc() : void +dec() : void +toString() : String -sleep(sleeptime : int) : void 1
Client
1
Server
-port : int -d : Data +main(args : String[]) : void
Claus Traulsen
Slide 6
Claus Traulsen
Slide 8
TCP/IP Threads
TCP/IP Threads
Common Problems
Stream Encoding Make sure sender and receiver use same encoding Otherwise, data might look the same, but is not equal. Line Ending Java/TCP/IP is independent of OS Network IO is the same as le IO Be aware of \n vs. \r\n
Multiple Clients
Want to connect multiple clients to the server Might want to listen to socket while computing Cannot send and receive at the same time Use threads
Claus Traulsen
Slide 9
Claus Traulsen
Slide 10
TCP/IP Threads
TCP/IP Threads
Multiple Clients
Threads in Java
Want to connect multiple clients to the server Might want to listen to socket while computing Cannot send and receive at the same time
Create subclass C of java.lang.Thread Overwrite method run() Start new thread with C.start() The thread terminates, when its run method terminates
Claus Traulsen
Slide 10
Claus Traulsen
Slide 11
TCP/IP Threads
TCP/IP Threads
Example
Data
-x : int = 0 -y : int = 0 +inc() : void +dec() : void +toString() : String -sleep(sleeptime : int) : void 1 1 1 1
Synchronize
java.lang.Thread java.net.Socket
1
Synchronization easy with java Multiple clients can connect to the server simultaneously A new thread is started for each of them Multiple inc, dec commands coming at the same time Declare a method synchronize: Only one thread can execute it at the same time Other threads are blocked Can declare a code region synchronized as well Sycnhronization lock is always the object To much synchronization is bad for performance!
Connection
-client : java.net.Socket -d : Data +Connection(client : void,Data : d) : void +run() : void 0..n 1 1 1
Server
-port : int -d : Data +main(args : String[]) : void
Claus Traulsen
Slide 12
Claus Traulsen
Slide 14
TCP/IP Threads
Race Condition
Ups x = x + 1, not the value we expect! Might be unlikely, but it occurs Synchronize threads, protect critical sections
Claus Traulsen
Slide 13