Java Sockets and Server Sockets
Java Sockets and Server Sockets
Sockets
• How data is transmitted across the Internet
• Sockets
• Server Sockets
• UDP
Applet Network Security
Restrictions
• Applets may:
– send data to the code base
– receive data from the code base
• Applets may not:
– send data to hosts other than the code base
– receive data from hosts other than the code
base
Datagrams
• Before data is sent across the Internet
from one host to another using TCP/IP, it
is split into packets of varying but finite
size called datagrams.
• Datagrams range in size from a few dozen
bytes to about 60,000 bytes.
• Packets larger than this, and often smaller
than this, must be split into smaller pieces
before they can be transmitted.
Packets Allow Error Correction
• If one packet is lost, it can be retransmitted
without requiring redelivery of all other
packets.
• If packets arrive out of order they can be
reordered at the receiving end of the
connection.
Abstraction
• Datagrams are mostly hidden from the Java
programmer.
• The host's native networking software
transparently splits data into packets on the
sending end of a connection, and then
reassembles packets on the receiving end.
• Instead, the Java programmer is presented
with a higher level abstraction called a socket.
Sockets
• A socket is a reliable connection for the
transmission of data between two hosts.
• Sockets isolate programmers from the
details of packet encodings, lost and
retransmitted packets, and packets that
arrive out of order.
• There are limits. Sockets are more likely to
throw IOExceptions than files.
Socket Operations
• There are four fundamental operations a socket
performs. These are:
1. Connect to a remote machine
2. Send data
3. Receive data
4. Close the connection
• A socket may not be connected to more than one
host at a time.
• A socket may not reconnect after it's closed.
The java.net.Socket class
• The java.net.Socket class allows you to
create socket objects that perform all four
fundamental socket operations.
• You can connect to remote machines; you can
send data; you can receive data; you can
close the connection.
• Each Socket object is associated with exactly
one remote host. To connect to a different
host, you must create a new Socket object.
Constructing a Socket
• Connection is accomplished through the
constructors.
– class scantest
– {
–
– public static void main(String[] args) {
– String host = "localhost";
– if(args.length>0)
– {
– host=args[0];
– }
– try {
– InetAddress theAddress= InetAddress.getByName(host);
– for(int i=1024;i<65536;i++)
– {
–
try
{
Socket thesocket =new Socket(theAddress,i);
System.out.println("A server is listening on port "+i + " of " +
host);
}
Picking an IP address
• The last two constructors also specify the
host and port you're connecting from.
• On a system with multiple IP addresses,
like many web servers, this allows you to
pick your network interface and IP address.
Choosing a Local Port
• You can also specify a local port number,
• Setting the port to 0 tells the system to
randomly choose an available port.
• If you need to know the port you're connecting
from, you can always get it with
getLocalPort().
Socket webMetalab = new Socket("metalab.unc.edu",
80, "calzone.oit.unc.edu", 0);
Sending and Receiving Data
• Data is sent and received with output and
input streams.
• There are methods to get an input stream for
a socket and an output stream for the socket.
public InputStream getInputStream() throws
IOException
public OutputStream getOutputStream() throws
IOException
• try {
• InetAddress ia =
InetAddress.getByName("199.1.32.90");
• ServerSocket ss = new ServerSocket(80, 50, ia);
•}
• catch (IOException e) {
• System.err.println(e);
•}
• On a server with multiple IP addresses, the
getInetAddress() method tells you
which one this server socket is listening to.
public InetAddress getInetAddress()
• try {
• InetAddress metalab = new InetAddess("metalab.unc.edu");
• int chargen = 19;
• String s = "My second UDP Packet"
• byte[] b = s.getBytes();
• DatagramPacket dp = new DatagramPacket(b, b.length, metalab,
chargen);
• }
• catch (UnknownHostException e) {
• System.err.println(e);
• }
DatagramPackets are not
immutable.
• public synchronized void setAddress(InetAddress iaddr)
• public synchronized void setPort(int iport)
• public synchronized void setData(byte ibuf[])
• public synchronized void setLength(int ilength)
• public synchronized InetAddress getAddress()
• public synchronized int getPort()
• public synchronized byte[] getData()
• public synchronized int getLength()
• These methods are primarily useful when
you're receiving datagrams.
java.net.DatagramSocket
• public DatagramSocket() throws SocketException
• public DatagramSocket(int port) throws SocketException
• public DatagramSocket(int port, InetAddress laddr) throws
SocketException
• The first is for client datagram sockets; that is
sockets that send datagrams before receiving
any.
• The second two are for server datagram sockets
since they specify the port and optionally the IP
address of the socket
Sending UDP Datagrams
• To send data to a particular server
– Convert the data into byte array.
– Pass this byte array, the length of the data in
the array (most of the time this will be the
length of the array) and the InetAddress and
port to which you wish to send it into the
DatagramPacket() constructor.
– Next create a DatagramSocket and pass the
packet to its send() method
For example,
• InetAddress metalab = new
InetAddess("metalab.unc.edu");
• int chargen = 19;
• String s = "My second UDP Packet";
• byte[] b = s.getBytes();
• DatagramPacket dp = new DatagramPacket(b,
b.length, ia, chargen);
• DatagramSocket sender = new DatagramSocket();
• sender.send(dp);
Receiving UDP Datagrams