tcp-ip-java

4
TCP/IP Threads Using TCP/IP with Java Claus Traulsen 14. November 2007 Claus Traulsen Using TCP/IP with Java Slide 1 TCP/IP Threads TCP/IP Some Background Using Java Threads Claus Traulsen Using TCP/IP with Java Slide 2 TCP/IP Threads Some Background Using Java Netwo rk Protocols 7 App lic atio n 6 Presen tat ion 5 Se ss ion 4 Transport 3 Netw or k 2 Da ta Li nk 1 Phys ical OSI reference model Ordered in layers Each layer oers some services Each layer only uses the services oered by the layer below. Applica tions only care about application layer Claus Traulsen Using TCP/IP with Java Slide 3 TCP/IP Threads Some Background Using Java TCP/IP 7 Applica tio n 6 5 4 Transport 3 Netw or k 2 Hos t-t o network 1 TCP/IP reference model We will use TCP/IP Cares about routing, failure det ection, .. . Clients use sockets to connect to server Independent of programming language, OS Read and write on network resources Have to dene a protocol Claus Traulsen Using TCP/IP with Java Slide 4

Transcript of tcp-ip-java

Page 1: tcp-ip-java

8/6/2019 tcp-ip-java

http://slidepdf.com/reader/full/tcp-ip-java 1/4

TCP/IPThreads

Using TCP/IP with Java

Claus Traulsen

14. November 2007

Claus Traulsen Using TCP/IP with Java Slide 1

TCP/IPThreads

TCP/IPSome BackgroundUsing Java

Threads

Claus Traulsen Using TCP/IP with Java Slide 2

TCP/IPThreads

Some BackgroundUsing Java

Network Protocols

7 Application6 Presentation5 Session

4 Transport3 Network2 Data Link1 Physical

OSI reference model

Ordered in layers

Each layer offers some services

Each layer only uses the servicesoffered by the layer below.

Applications only care aboutapplication layer

Claus Traulsen Using TCP/IP with Java Slide 3

TCP/IPThreads

Some BackgroundUsing Java

TCP/IP

7 Application654 Transport

3 Network2 Host-to network1

TCP/IPreference model

We will use TCP/IP

Cares about routing, failuredetection, . . .

Clients use sockets  to connect toserver

Independent of programminglanguage, OS

Read and write on networkresources

Have to define a protocol

Claus Traulsen Using TCP/IP with Java Slide 4

Page 2: tcp-ip-java

8/6/2019 tcp-ip-java

http://slidepdf.com/reader/full/tcp-ip-java 2/4

TCP/IPThreads

Some BackgroundUsing Java

Sockets

socket

bind

listen

accept connect

socket

writeread

write read

close close

Server 

Client

Abstraction for networkend-point

Specified by host-addressplus port

Server waits for clients toconnect

Multiple clients can connect

to one server

Claus Traulsen Using TCP/IP with Java Slide 5

TCP/IPThreads

Some BackgroundUsing Java

Further Information

A. Tannenbaum, Computer Networks

Java ist auch eine Insel, Chap 9, 16

Lectures by Prof. Luttenberger Internet Communications Computer Networks

Claus Traulsen Using TCP/IP with Java Slide 6

TCP/IPThreads

Some BackgroundUsing Java

TCP/IP and Java

TCP/IP is directly supported by Java

Classes java.net.socket and java.net.ServerSocket Only need host-address and port

Offer input- and output-stream→  just like file IO

Still have to interpret the data

Claus Traulsen Using TCP/IP with Java Slide 7

TCP/IPThreads

Some BackgroundUsing Java

Example

Data

+inc() : void

+dec() : void

+toString() : String

-sleep(sleeptime : int) : void

-x : int = 0

-y : int = 0

Server

+main(args : String[]) : void

-port : int

-d : Data

1

1

Client

+main(args : String[]) : void

-host : String

-port : int

Server stores data (x,y)

Invariant: y  = 2x 

Client sends command to

server:+ increment

- decrement

Server returns current values

Claus Traulsen Using TCP/IP with Java Slide 8

Page 3: tcp-ip-java

8/6/2019 tcp-ip-java

http://slidepdf.com/reader/full/tcp-ip-java 3/4

TCP/IPThreads

Some BackgroundUsing Java

Common Problems

Stream Encoding

Make sure sender and receiver use sameencoding

Otherwise, data might look the same, but is notequal.

Line Ending

Java/TCP/IP is independent of OS Network IO is the same as file IO Be aware of  \n vs. \r\n

Claus Traulsen Using TCP/IP with Java Slide 9

TCP/IPThreads

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

Claus Traulsen Using TCP/IP with Java Slide 10

TCP/IPThreads

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 10

TCP/IPThreads

Threads in Java

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 11

Page 4: tcp-ip-java

8/6/2019 tcp-ip-java

http://slidepdf.com/reader/full/tcp-ip-java 4/4

TCP/IPThreads

Example

Data

+inc(): void

+dec(): void

+toString(): String

-sleep(sleeptime :int) :void

-x : in t= 0

-y : in t= 0

Server

+main(args:String[]): void

-port:int

-d :Data

1

1

Connection

+Connection(client:void,Data :d) :void+run(): void

-client: java.net.Socket

-d :Data

java.lang.Thread

java.net.Socket

11

11

0..n

1

1

Multiple clients canconnect to the serversimultaneously

A new thread is startedfor each of them

Multiple inc, deccommands coming at thesame time

Claus Traulsen Using TCP/IP with Java Slide 12

TCP/IPThreads

Race Condition

Multiple threads access same data

Thread T1 and T2 want to increment x : T1 reads x  and increments local register: r 0 := x + 1 T2 reads x  and increments local register: r 1 := x + 1

T1 writes x  := r 0 T2 writes x  := r 1

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

TCP/IPThreads

Synchronize

Synchronization easy with java

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!

Claus Traulsen Using TCP/IP with Java Slide 14