210 likes | 404 Views
Programming Section 9. James King 12 August 2003. 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. Network programming Network concepts.
E N D
Programming Section 9 James King 12 August 2003
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
Network programmingNetwork 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
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
Network programmingImplementing 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
Network programmingImplementing network connections Machine 192.12.21.6 Machine 192.12.21.7 Application Application Apparent Flow of Data Operating System Operating System Network Layer Network Layer socket 665 socket 1234 Actual Flow of Data Network/Internet
TCP/IP to send Data across a network Networking Section
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
Network programmingImplementation of the TCP Server port to listen to connects on 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); 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
Implementation of the TCP Client machine to connect to Socket sv=new Socket("localhost",6666); OutputStream i=sv.getOutputStream(); DataOutputStream oi=new DataOutputStream(i); oi.writeUTF("Hello World"); i.flush(); oi.flush(); port to connect to write the message to the socket Java takes care of converting the string into something that can be send across a network force sending the message by flushing any buffers
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
UDP to send Data across a network Networking Section
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
Implementation of a UDP server create storage for the incoming data 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 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
Implementation of a UDP Client convert the string hello world into an array of bytes byte [] bytes="Hello World".getBytes(); DatagramSocket sv=new DatagramSocket(); DatagramPacket p=new DatagramPacket(bytes, bytes.length, InetAddress.getByName("localhost"), 6666); sv.send(p); create a datagram socket create a datagram packet, attach the data we want to send and the destination address send the packet
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
Serialisation to send Objects across a network Networking Section
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
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; } }
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
Sending Objects across the net using TCP Client Code We use a ObjectOutputStream instead of a DataOutputStream 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 writeObject instead of writeUTF