Download - Programming Section 9
1
Programming Section 9
James King12 August 2003
2
Internet and Network programming in TCP/IP
Networking conceptsHello World in TCPHello World in UDPSending objects across the networkAccepting multiple concurrent clients
3
Network programming
Network conceptsNetworks 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 detailWe will look at how to write programs that transfer data using Internet Protocol – the internet standard
4
Internet ProtocolsIP 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 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
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
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/IPServer must be accepting on the port before the client can sendClient must connect to the server before it can sendClient and server should close the connection before exiting
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
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
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 serverServer accept() returns with a temporary socket to talk to client onServer calls readUTF() and blocksClient sends a message using writeUTF()Server receives message and readUTF() returns the message as stringServer 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/IPthere are no streams in UDP instead we have to make a UDP packetUDP 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 packetTo read the contents of a UDP packet we have to reverse this processUDP 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
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
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 bytescreate 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 dataServer builds a datagram packet to store the incoming datagramserver calls receive and blocks waits for a datagramClient creates socketclient marshals message into a byte array and puts the byte array into a Datagram packetClient sends message to server using sendServer receives message and receive unblocksServer unmarshals the data from the datagram packet and converts it into StringServer 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 serialisationserialisation is the process of making a copy of the attributes that can be handled in a streamThe class must implement SerializableThe class must have a constructor with no parametersBy 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 readUTFwe ask the object to
return its message
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