Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming Section 9 James King 12 August 2003.

Similar presentations


Presentation on theme: "1 Programming Section 9 James King 12 August 2003."— Presentation transcript:

1

2 1 Programming Section 9 James King 12 August 2003

3 2 Internet and Network programming in TCP/IP Networking concepts Hello World in TCP Hello World in UDP Sending objects across the network Accepting multiple concurrent clients

4 3 Network programming Network concepts Networks allow several machines to communicate together. Within a room or department Internet Many different hardware and software technologies Ethernet, ATM, modem, ADSL, TCP/IP, IPX etc.. Enabling sharing of resources (files, printers etc) See computer networks modules for more detail We will look at how to write programs that transfer data using Internet Protocol – the internet standard

5 4 Internet Protocols IP provides the protocols to connect the internet together The only internet protocol Most common network protocol in small networks too... Relatively reliable and proven technology Provides two types of communication short packets called datagrams (UDP)  maximum packet size 64Kbytes  packets may not arrive in order  some packets may not arrive  Some packets may be duplicated Streams of bytes (TCP)  send any amount of data  data is sequenced (in order no duplicates) and reliable

6 5 Network programming Implementing network connections Each computer on network or internet has unique IP address which is made up of 4 numbers separated by. e.g. 100.12.11.13 127.0.0.1 always means yourself Each computer has a number of connection points available called ports e.g. port 6666 applications listen to one or more ports for data applications can not share the same port

7 6 Machine 192.12.21.7 Network programming Implementing network connections Network/Internet Actual Flow of Data Operating System Application Network Layer socket 1234 Machine 192.12.21.6 Operating System Application Network Layer socket 665 Apparent Flow of Data

8 7 TCP/IP to send Data across a network Networking Section

9 8 Simple TCP Networked Hello World Aim send the string “Hello World” from the client to the server using TCP/IP Server must be accepting on the port before the client can send Client must connect to the server before it can send Client and server should close the connection before exiting

10 9 Network programming Implementation of the TCP Server ServerSocket sv=new ServerSocket(6666); System.out.println("waitng for client"); Socket s=sv.accept(); InputStream i=s.getInputStream(); DataInputStream oi=new DataInputStream(i); String h=oi.readUTF(); System.out.println(h); port to listen to connects on wait for a connect. Conversation with client will use a temporary socket s read the one line message from the temporary socket display message on console

11 10 Implementation of the TCP Client Socket sv=new Socket("localhost",6666); OutputStream i=sv.getOutputStream(); DataOutputStream oi=new DataOutputStream(i); oi.writeUTF("Hello World"); i.flush(); oi.flush(); force sending the message by flushing any buffers write the message to the socket Java takes care of converting the string into something that can be send across a network port to connect to machine to connect to

12 11 What happens in what order Server creates main socket and blocks inside accept() waiting for a connection from the client Client creates socket and connects to server Server accept() returns with a temporary socket to talk to client on Server calls readUTF() and blocks Client sends a message using writeUTF() Server receives message and readUTF() returns the message as string Server prints message on console

13 12 UDP to send Data across a network Networking Section

14 13 Simple UDP Networked Hello World Aim send the string “Hello World” from the client to the server using UDP/IP there are no streams in UDP instead we have to make a UDP packet UDP packets can only contain an array of bytes so we have to convert the string to bytes before we can put it in a UDP packet To read the contents of a UDP packet we have to reverse this process UDP in C or C++ is much easier than in Java – fault of Java and not UDP or the internet

15 14 Implementation of a UDP server byte [] bytes=new byte[65536]; DatagramSocket sv=new DatagramSocket(6666); DatagramPacket p=new DatagramPacket(bytes,bytes.length); sv.receive(p); bytes=p.getData(); String h=new String(bytes,0,p.getLength()); System.out.println(h); create storage for the incoming data create a UDP socket on port 6666 Build a datagram packet and attach the storage to it receive a UDP packet and copy it into our DatagramPacket extract the bytes from the datagram convert the bytes into a string

16 15 Implementation of a UDP Client byte [] bytes="Hello World".getBytes(); DatagramSocket sv=new DatagramSocket(); DatagramPacket p=new DatagramPacket(bytes, bytes.length, InetAddress.getByName("localhost"), 6666); sv.send(p); convert the string hello world into an array of bytes create a datagram socket create a datagram packet, attach the data we want to send and the destination address send the packet

17 16 What happens in what order Server creates socket Server creates a byte array to store the incoming data Server builds a datagram packet to store the incoming datagram server calls receive and blocks waits for a datagram Client creates socket client marshals message into a byte array and puts the byte array into a Datagram packet Client sends message to server using send Server receives message and receive unblocks Server unmarshals the data from the datagram packet and converts it into String Server prints String on console

18 17 Serialisation to send Objects across a network Networking Section

19 18 Sending Instances of Classes across a network You can send your own instances of classes across a network using Java serialisation serialisation is the process of making a copy of the attributes that can be handled in a stream The class must implement Serializable The class must have a constructor with no parameters By default all attributes in the class are sent across the network You can implement your own read and write methods

20 19 Hello World Network Class public class Hello implements Serializable { private String hello=""; public Hello(String h) { hello=h; } public Hello() { hello=""; } public String message() { return hello; }

21 20 Sending Objects across the net using TCP Server Code ServerSocket sv=new ServerSocket(6666,2); Socket s=sv.accept(); InputStream i=s.getInputStream(); ObjectInputStream oi=new ObjectInputStream(i); Hello h=(Hello)oi.readObject(); System.out.println(h.message()); We use a ObjectInputStream instead of a DataInputStream We read an object using readObject and type cast it into what was sent instead of readUTF we ask the object to return its message

22 21 Sending Objects across the net using TCP Client Code Socket sv=new Socket("localhost",6666); OutputStream i=sv.getOutputStream(); ObjectOutputStream oi=new ObjectOutputStream(i); Hello h=new Hello("Hello World"); oi.writeObject(h); We use a ObjectOutputStream instead of a DataOutputStream We use writeObject instead of writeUTF


Download ppt "1 Programming Section 9 James King 12 August 2003."

Similar presentations


Ads by Google