Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Computer Science IIB 2010 Java - Sockets Intro: DR Marijke Coetzee E-Ring 230 (011) 559 - 2907

The document provides an introduction to sockets in Java. It discusses how the sockets API allows programs to communicate over networks using the client-server model. It describes how sockets handle low-level networking details like packetization and shield programmers from these details. Key classes for sockets programming in Java include the Socket class for client connections and common port numbers for standard network services.

Uploaded by

moshane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Computer Science IIB 2010 Java - Sockets Intro: DR Marijke Coetzee E-Ring 230 (011) 559 - 2907

The document provides an introduction to sockets in Java. It discusses how the sockets API allows programs to communicate over networks using the client-server model. It describes how sockets handle low-level networking details like packetization and shield programmers from these details. Key classes for sockets programming in Java include the Socket class for client connections and common port numbers for standard network services.

Uploaded by

moshane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Computer Science IIB

2010
Java – Sockets intro

Dr Marijke Coetzee
E-Ring 230
 (011) 559 -2907
 marijkec@uj.ac.za
JAVA: Sockets

• One of the most pervasive and longest-lasting interfaces in


software is the sockets API.
• Developed by the Computer Systems Research Group at the
University of California at Berkeley, the sockets API was first
released as part of the 4.1c BSD operating system in 1982.
• While there are longer-lived APIs—for example, those dealing
with Unix file I/O—it is quite impressive for an API to have
remained in use and largely unchanged for 27 years.
• The only major update to the sockets API has been the extension of
ancillary routines to accommodate the larger addresses used by
IPv6.2

• Read http://queue.acm.org/detail.cfm?id=1538949
Whither Sockets?

Computer Science IIB 2


Computer Science IIB 3
JAVA: Sockets

• The model of distributed programming that came to be most


popularized by the sockets API was the client/server model, in
which there is a server and a set of clients.
• The clients send messages to the server to ask it to do work on
their behalf, wait for the server to do the work requested, and at
some later point receive an answer.
• This model of computing is now so ubiquitous it is often the
only model with which many software engineers are familiar
• The sockets API made the client/server model easy to
implement because of the small number of extra system calls
that programmers would need to add to their non-networked
code so it could take advantage of other computing resources.

Computer Science IIB 4


JAVA: Sockets
• Data is transmitted across the Internet in packets of finite size
called datagrams.

• However, since datagrams have a finite length, it's often


necessary to split the data across multiple packets and
reassemble it at the destination.

• It's also possible that one or more packets may be lost or


corrupted in transit and need to be retransmitted or that packets
arrive out of order and need to be reordered.

Computer Science IIB 5


JAVA: Sockets
Keeping track of this –
• splitting the data into packets,
• generating headers,
• parsing the headers of incoming packets,
• keeping track of what packets have and haven't been received,
and so on –
is a lot of work and requires a lot of intricate code.

Computer Science IIB 6


Sockets

• Fortunately, you don't have to do the work yourself.

• Sockets allow the programmer to treat a network connection as


just another stream onto which bytes can be written and
from which bytes can be read.

• Sockets shield the programmer from low-level details of the


network, such as error detection, packet sizes, packet
retransmission, network addresses, and more.

Computer Science IIB 7


Sockets basics

A socket is a connection between two hosts. It can perform seven


basic operations:
1. Connect to a remote machine
2. Send data CLIENT
3. Receive data
4. Close a connection

1. Bind to a port
2. Listen for incoming data
3. Accept connections from remote machines SERVER
on the bound port

Computer Science IIB 8


Ports
• Addresses would be all you needed if each computer did no
more than one thing at a time. However, modern computers do
many different things at once.

• Email needs to be separated from FTP requests, which need to


be separated from web traffic.

• This is accomplished through ports.

• Each computer with an IP address has several thousand logical


ports (65,535 per transport layer protocol, to be precise).
These are purely abstractions in the computer's memory and do
not represent anything physical, like a serial or parallel port.

• Each port is identified by a number between 1 and 65,535.


• Each
Computer portIIBcan be allocated to a particular service.
Science 9
Ports

• Port numbers between 1 and 1,023 are reserved for well-


known services like finger, FTP, HTTP, and IMAP.

• Port assignments are not absolutely guaranteed; in particular,


web servers often run on ports other than 80, either because
multiple servers need to run on the same machine or because
the person who installed the server doesn't have the root
privileges needed to run it on port 80.

Computer Science IIB 10


Some well-known ports

Protocol Port Protocol Purpose


echo 7 TCP/UDP echo is a test protocol used to verify that two machines are able to connect by having one echo
back the other's input.

daytime 13 TCP/UDP Provides an ASCII representation of the current time on the server.

FTP data 20 TCP FTP uses two well-known ports. This port is used to transfer files.

FTP 21 TCP This port is used to send FTP commands like put and get.

SSH 22 TCP Used for encrypted, remote logins.

telnet 23 TCP Used for interactive, remote command-line sessions.

SMTP 25 TCP The Simple Mail Transfer Protocol is used to send email between machines.

time 37 TCP/UDP A time server returns the number of seconds that have elapsed on the server since midnight,
January 1, 1900, as a four-byte, signed, big-endian integer.

whois 43 TCP A simple directory service for Internet network administrators.

finger 79 TCP A service that returns information about a user or users on the local system.

HTTP 80 TCP The underlying protocol of the World Wide Web.

POP3 110 TCP Post Office Protocol Version 3 is a protocol for the transfer of accumulated email from the host to
spo­radically connected clients.

NNTP 119 TCP Usenet news transfer; more formally known as the "Network News Transfer Protocol".

IMAP 143 TCP Internet Message Access Protocol is a protocol for accessing mailboxes stored on a server.

RMI Registry 1099 TCP The registry service for Java remote objects.
Computer Science IIB 11
Sockets basics

• Once the connection is established, the local and remote hosts


get input and output streams from the socket and use those
streams to send data to each other.

• This connection is full-duplex; both hosts can send and receive


data simultaneously.

• When the transmission of data is complete, one or both sides


close the connection.

Computer Science IIB 12


Investigating protocols with Telnet
• To get a feel for how a protocol operates, you can use Telnet to
connect to a server, type different commands to it, and watch
its responses.
• By default, Telnet attempts to connect to port 23.
• Telnet clients are available for all common operating systems
• You can use Telnet to simulate a client, see how the client and
the server interact, and thus learn what your Java program
needs to do.
• Windows:
telnet
open localhost 25
• This requests a connection to port 25, the SMTP port, on the
local machine
• SMTP is the protocol used to transfer email between servers or
between a mail client and a server
Computer Science IIB 13
The Socket Class

Computer Science IIB 14


The Socket Class

• The java.net.Socket class is Java's fundamental class for


performing client-side TCP operations.

• Other client-oriented classes that make TCP network


connections such as URL, URLConnection, Applet, and
JEditorPane all ultimately end up invoking the methods of
this class.

• The actual reading and writing of data over the socket is


accomplished via the familiar stream classes.

Computer Science IIB 15


The Constructors

• The nondeprecated public Socket constructors are simple.


• Each lets you specify the host and the port you want to connect
to.
• Hosts may be specified as an InetAddress or a String.
• Ports are always specified as int values from 0 to 65,535.
• Two of the constructors also specify the local address and local
port from which data will be sent.
• For example:
try {
Socket theSocket = new Socket(“www.abc.com", 80);
}
catch (UnknownHostException ex) {
System.err.println(ex);
}
catch (IOException ex) {
System.err.println(ex);
}
Computer Science IIB 16
The Constructors
• If the domain name server cannot resolve the hostname or is not
functioning, the constructor throws an
UnknownHostException.

• If the socket cannot be opened for some other reason, the


constructor throws an IOException.

• There are many reasons a connection attempt might fail:


– the host you're trying to reach may not be accepting
connections,
– a dialup Internet connection may be down,
– routing problems may be preventing your packets from
reaching their destination
Computer Science IIB 17
Getting information about a socket

• public InetAddress getlnetAddress( )


– Given a Socket object, the getInetAddress( ) method tells you which
remote host the Socket is connected to or, if the connection is now
closed, which host the Socket was connected to when it was connected.

• public int getPort( )


– The getPort( ) method tells you which port the Socket is connected to on
the remote host.

• public int getLocaIPort( )


– There are two ends to a connection: the remote host and the local host.
To find the port number for the local end of a connection, call
getLocalPort( )

Computer Science IIB 18


Finally

It’s good practice to close a connection when your have finished


with it.
try {
Socket connection = null;
connection = new Socket(“www.abc.com", 80);
}
catch (UnknownHostException ex) {
System.err.println(ex);
}
catch (IOException ex) {
System.err.println(ex);
}
finally {
try {
if (connection != null)
connection.close();
}
Computer Science IIB 19
Non-graphical characters
• Java provides escape sequences for several non-graphical characters.
• \n New line
• \t Tab
• \b Backspace
• \r Carriage return
• \f Formfeed
• \\ Backslash
• \' Single quotation mark
• \" Double quotation mark
• \d Octal
• \xd Hexadecimal
• \ud Unicode character
Computer Science IIB 20
Comment

• Three disparate areas of networking are not well served by the


sockets API:
– low-latency or realtime applications;
– high-bandwidth applications;
– and multihomed systems—that is, those with multiple network interfaces.
• Many people confuse increasing network bandwidth with higher
performance, but increasing bandwidth does not necessarily
reduce latency.
• The challenge for the sockets API is giving the application faster
access to network data.

• Latency, a synonym for delay, is an expression of how much time it takes for
a packet of data to get from one designated point to another.

Computer Science IIB 21

You might also like