You are on page 1of 4

TCP/IP Threads

TCP/IP Threads

Some Background Using Java

Network Protocols

Using TCP/IP with Java


Claus Traulsen 14. November 2007

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

Using TCP/IP with Java

Slide 1

Claus Traulsen

Using TCP/IP with Java

Slide 3

TCP/IP Threads

TCP/IP Threads

Some Background Using Java

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

Some Background Using Java

Threads

Claus Traulsen

Using TCP/IP with Java

Slide 2

Claus Traulsen

Using TCP/IP with Java

Slide 4

TCP/IP Threads

Some Background Using Java

TCP/IP Threads

Some Background Using Java

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

TCP/IP and Java

Abstraction for network end-point


C lie n t s o c k e t c o n n e c t w r ite re a d 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

Using TCP/IP with Java

Slide 5

Claus Traulsen

Using TCP/IP with Java

Slide 7

TCP/IP Threads

Some Background Using Java

TCP/IP Threads

Some Background Using Java

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

-host : String -port : int

+main(args : String[]) : void

Claus Traulsen

Using TCP/IP with Java

Slide 6

Claus Traulsen

Using TCP/IP with Java

Slide 8

TCP/IP Threads

Some Background Using Java

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

Using TCP/IP with Java

Slide 9

Claus Traulsen

Using TCP/IP with Java

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

Using TCP/IP with Java

Slide 10

Claus Traulsen

Using TCP/IP with Java

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

Using TCP/IP with Java

Slide 12

Claus Traulsen

Using TCP/IP with Java

Slide 14

TCP/IP Threads

Race Condition

Multiple threads access same data Thread T1 and T2 want to increment x:


T1 T2 T1 T2 reads x and increments local register: r0 := x + 1 reads x and increments local register: r1 := x + 1 writes x := r0 writes x := r1

Ups x = x + 1, not the value we expect! Might be unlikely, but it occurs Synchronize threads, protect critical sections

Claus Traulsen

Using TCP/IP with Java

Slide 13

You might also like