SOCKET PROGRAMMING
Introduction
The socket API is an Interprocessing Communication (IPC) programming
interface originally provided as part of the Berkeley UNIX operating
system.
It has been ported to all modern operating systems, including Sun
Solaris and Windows systems.
It is a de facto standard for programming IPC, and is the basis of
more sophisticated IPC interface such as remote procedure call and
remote method invocation.
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
2/78
The conceptual model of the socket API
P ro c ess A
P ro c ess B
a socket
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
3/78
The socket API
A socket API provides a programming construct termed a socket. A
process wishing to communicate with another process must create an
instance, or instantiate, such a construct
The two processes then issues operations provided by the API to send
and receive data.
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
4/78
Connection-oriented & connectionless datagram socket
A socket programming construct can make use of either the UDP or TCP protocol.
Sockets that use UDP for transport are known as datagram sockets, while sockets that use
TCP are termed stream sockets.
Datagram sockets can support both connectionless and connection-oriented
communication at the application layer. This is so because even though datagrams are sent or
received without the notion of connections at the transport layer, the runtime support of the
socket API can create and maintain logical connections for datagrams exchanged between two
processes, as you will see in the next section
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
5/78
Connection-oriented & connectionless datagram socket
sock et
A P I r u n t im e
suppo r t
P roce ss A
P roce ss B
sock et
A P I r u n t im e
suppo r t
tr a n s p o r t la y e r s o ftw a r e
tr a n s p o r t la y e r s o ftw a r e
c o n n e c t io n le s s d a ta g r a m s o c k e t
a d a ta g r a m
a lo g ic a l c o n n e c t io n c r e a t e d a n d m a in t a in e d
b y t h e r u n t im e s u p p o r t o f t h e d a t a g r a m
socket A P I
sock e t
A P I r u n t im e
su pp o r t
P rocess A
P roce ss B
sock e t
A P I r u n t im e
su p po r t
tr a n s p o r t la y e r s o ft w a r e
tr a n s p o r t la y e r s o ftw a r e
c o n n e c t io n - o r ie n t e d d a t a g r a m s o c k e t
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
6/78
Networking Basics
Computers running on the Internet communicate to each other using either the Transmission
Control Protocol (TCP) or the User Datagram Protocol (UDP)
TCP
When two applications want to communicate to each other reliably, they establish a
connection and send data back and forth over that connection
TCP provides a point-to-point channel for applications that require reliable
communications. The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and
Telnet are all examples of applications that require a reliable communication channel
UDP
The UDP protocol provides for communication that is not guaranteed between two
applications on the network. UDP is not connection-based like TCP. Rather, it sends
independent packets of data, called datagrams, from one application to another
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
7/78
Client-Server
Client - initiates connection
retrieves data,
displays data,
responds to user input,
requests more data
Examples:
Web Browser
Chat Program
PC accessing files
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
8/78
Client-Server
Server - responds to connection
receives request for data,
looks it up,
delivers it
Examples:
Web Server
Database Server
Domain Name Server
Stock Quote Server
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
9/78
Client-Server
Difference between client and server is semantic
It’s all just peers talking to each other
Protocol - roles, vocabulary, rules for communication (more later)
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
10/78
TCP/IP: The Internet Protocol
Application Layer (HTTP, FTP, SMTP)
Transport Layer (TCP, UDP)
Internet Layer (IP)
Physical Network
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
11/78
TCP/UDP/IP
IP
raw packets
the “Internet Layer”
TCP
data stream
reliable, ordered
the “Transport Layer”
UDP
user datagrams (packets)
unreliable, unordered
the “Transport Layer”
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
12/78
The Three ‘I’s
internet
any IP-based network
Internet
the big, famous, world-wide IP network
intranet
a corporate LAN-based IP network
extranet
accessing corporate data across the Internet
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
13/78
Sockets and Ports
Port: a meeting place on a host
one service per port
1-1023 = well-known services
1024+ = experimental services, temporary
Socket: a two-way connection
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
14/78
Well-Known Ports
20,21: FTP
23: telnet
25: SMTP
43: whois
80: HTTP
119: NNTP
1099: RMI
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
15/78
Sockets and Ports (Diagram)
port 13
Time Service
port 80
Web Service
Client
Socket
Socket
Server
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
16/78
Understanding Ports
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
17/78
Communication between Applications Using Ports
Local ports identify the application establishing a connection from other
programs, allowing multiple TCP applications to run on the same
machine.
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
18/78
Transmission Control Protocol
TCP establishes a virtual connection to transmit data
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
19/78
TCP Programmning in Java
ServerSocket server = new ServerSocket(port);
Socket serverSoc = server.accept();
Socket clientSoc = new Socket(serverAdd,port);
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
20/78
TCP Programmning in Java
OutputStream outClient =
clientSoc.getOutputStream();
InputStream inServer =
serverSoc.getInputStream();
stream
Server
Client
InputStream inClient =
clientSoc.getInputStream();
OutputStream outServer =
serverSoc.getOutputStream();
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
21/78
Socket Basics
A socket is a connection between two hosts. It can perform seven basic operations:
Connect to a remote machine
Send data
Receive data
Close a connection
Bind to a port
Listen for incoming data
Accept connections from remote machines on the bound port
Java's Socket class, which is used by both clients and servers, has methods that correspond to the
first four of these operations. The last three operations are needed only by servers, which wait for
clients to connect to them. They are implemented by the ServerSocket class
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
22/78
Program using client socket
1.
2.
3.
The program creates a new socket with a Socket( ) constructor.
The socket attempts to connect to the remote host.
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.
4.
When the transmission of data is complete, one or both sides close the
connection. Some protocols, such as HTTP 1.0, require the connection to be
closed after each request is serviced. Others, such as FTP, allow multiple
requests to be processed in a single connection.
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
23/78
Socket Basics - Constructors
public Socket(String host, int port) throws UnknownHostException,
IOException
This constructor creates a TCP socket to the specified port on the specified host and
attempts to connect to the remote host. For example:
try {
Socket toOReilly = new
Socket("www.oreilly.com",
80);
// send and receive data...
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
24/78
LowPortScanner Program
import java.net.*;
import java.io.*;
public class LowPortScanner {
public static void main(String[] args) {
String host = "localhost";
if (args.length > 0) host = args[0];
for (int i = 1; i < 1024; i++) {
try {
System.out.print("Scanning on port : "+i +" ; ");
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "+host);
}
catch (UnknownHostException e) {
System.out.println("The Server adress is unknown");
break;
}
catch (IOException e) {
System.out.println("The Server is not found");
}
}}}
Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007
25/78