1 / 21

Programming Section 9

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.

Download Presentation

Programming Section 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Section 9 James King 12 August 2003

  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

  3. 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

  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

  5. 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

  6. 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

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

  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

  9. 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

  10. 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

  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

  12. UDP to send Data across a network Networking Section

  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

  14. 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

  15. 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

  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

  17. Serialisation to send Objects across a network Networking Section

  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

  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; } }

  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

  21. 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

More Related