Lecture 3: Introduction To Socket Programming
Lecture 3: Introduction To Socket Programming
1
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
What is a socket?
• Port vs. Socket
2
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
TCP/IP Stack
3
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Two essential types of sockets
• TCP Socket • UDP Socket
• reliable delivery • unreliable delivery
• in-order guaranteed • no order guarantees
• connection-oriented • no notion of “connection” – app
• bidirectional indicates dest. for each packet
• can send or receive
App
App D1
3 2
1
socket Dest.
3 2 1
socket D2
D3
4
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Applications
medellin.cs.columbia.edu
(128.59.21.14)
newworld.cs.umass.edu
(128.119.245.93)
cluster.cs.columbia.edu
(128.59.21.14, 128.59.16.7,
128.59.16.5, 128.59.16.4)
6
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Ports
• Each host has 65,536 ports
Port 0
• Some ports are reserved
for specific apps Port 1
• 20,21: FTP
• 23: Telnet Port 65535
• 80: HTTP
• see RFC 1700 (about 2000 A socket provides an interface
ports are reserved) to send data to/from the
network through a port
7
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Addresses, Ports and Sockets
9
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Connection setup
• Passive participant • Active participant
• step 1: listen (for incoming
requests)
• step 2: request & establish
• step 3: accept (a request)
connection
• step 4: data transfer
• The accepted connection is
• step 4: data transfer
on a new socket
Passive Participant
• The old socket continues to
listen for other active a-sock-1 l-sock a-sock-2
participants
socket socket
Active 1 Active 2
10
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Dealing with blocking
11
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Java Sockets Programming
import java.net.*;
12
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Classes
InetAddress
Socket
ServerSocket
DatagramSocket
DatagramPacket
13
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
InetAddress class
• Static methods you can use to create new InetAddress
objects.
• getByName(String host)
• getAllByName(String host)
• getLocalHost()
InetAddress x = InetAddress.getByName(
“cse.unr.edu”);
Throws UnknownHostException
14
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
try {
InetAddress a = InetAddress.getByName(hostname);
System.out.println(hostname + ":" +
a.getHostAddress());
} catch (UnknownHostException e) {
16
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
JAVA TCP Sockets
• java.net.Socket
• Implements client sockets (also called just “sockets”).
• An endpoint for communication between two
machines.
• Uses input/output streams to pass messages
• java.net.ServerSocket
• Implements server sockets.
• Waits for requests to come in over the network.
• Accepts the client connection requests
• Performs some operation based on each request
17
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Socket Constructors
• Constructor creates a TCP connection to a named TCP server.
• There are a number of constructors:
18
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Socket Methods
void close();
InetAddress getInetAddress();
InetAddress getLocalAddress();
InputStream getInputStream();
OutputStream getOutputStream();
19
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Socket I/O
• Socket I/O is based on the Java I/O support
• in the package java.io
20
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
InputStream Basics
21
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
OutputStream Basics
22
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
ServerSocket Class
(TCP Passive Socket)
• Constructors:
ServerSocket(int port);
23
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
ServerSocket Methods
Socket accept();
void close();
InetAddress getInetAddress();
int getLocalPort();
24
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Socket programming with TCP
keyboard monitor
inFromUser
input
input (inFromUser stream) , stream
inFromServer
outToServer
output input
from socket (inFromServer stream stream
stream)
client TCP
clientSocket
socket TCP
socket
25
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Client/server socket interaction: TCP
Server (running on hostid) Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
26
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
TCPClient.java
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
28
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
outToClient.writeBytes(capitalizedSentence);
}
}
}
30
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
UDP Sockets
• DatagramSocket class
31
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Socket Programming with UDP
• UDP
• Connectionless and unreliable service.
• There isn’t an initial handshaking phase.
• Doesn’t have a pipe.
• Transmitted data may be received out of order, or lost
32
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
JAVA UDP Sockets
• In Package java.net
• java.net.DatagramSocket
• A socket for sending and receiving datagram
packets.
• Constructor and Methods
• DatagramSocket(int port): Constructs a datagram socket
and binds it to the specified port on the local host
machine.
• void receive( DatagramPacket p)
• void send( DatagramPacket p)
• void close()
33
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
DatagramSocket Constructors
DatagramSocket();
DatagramSocket(int port);
34
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Datagram Methods
void close();
Lots more!
35
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Datagram Packet
36
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
DatagramPacket Constructors
For receiving:
For sending:
37
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
DatagramPacket methods
byte[] getData();
void setData(byte[] buf);
InetAddress getAddress();
int getPort();
38
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Example: Java client (UDP)
keyboard monitor
inFromUser
input
stream
Client
Process
Input: receives
process
packet (TCP
received “byte
Output: sends
stream”)
packet (TCP sent
receivePacket
sendPacket
“byte stream”) UDP UDP
packet packet
client UDP
clientSocket
socket UDP
socket
39
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Client/server socket interaction: UDP
Server (running on hostid) Client
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port umber close
clientSocket
40
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
UDPClient.java
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress =
InetAddress.getByName("hostname");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
UDPClient.java
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length,
IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
42
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
UDPServer.java
import java.io.*;
import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new
DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
UDPServer.java
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
44
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna
Summary
• Socket programming is the most fundamental form
of Client-Server distributed computing available for
app. developers
• Can be used to develop client-server distributed
applications (e.g. Messaging applications)
• However, most real-world distributed systems use
more high level distributed computing technologies
(E.g. Web services, EJBs)
• Yet the underlying communication mechanism of
these high level Dist. Computing frameworks is
socket communication
45
SE3020 | Distributed Systems | Socket Programming | Dharshana Kasthurirathna