Socket Programming: Learn How To Build Client/server Applications That Communicate Using Sockets
Socket Programming: Learn How To Build Client/server Applications That Communicate Using Sockets
2: Application Layer 1
Socket-programming using TCP
Socket: a door between application process and end-end-
transport protocol (UDP or TCP)
TCP service: reliable transfer of bytes from one process
to another
controlled by
controlled by process application
application process
developer
developer socket socket
controlled by TCP with TCP with controlled by
buffers, operating
operating buffers, internet system
system variables variables
host or host or
server server
2: Application Layer 2
Socket programming with TCP
Client must contact server When client creates socket:
server process must first client TCP establishes
be running connection to server TCP
server must have created When contacted by client,
socket (door) that server TCP creates new
welcomes client’s contact socket for server process to
communicate with client
Client contacts server by:
allows server to talk with
creating client-local TCP
multiple clients
socket
specifying IP address, port
application viewpoint
number of server process
TCP provides reliable, in-order
transfer of bytes (“pipe”)
between client and server
2: Application Layer 3
Socket programming with TCP
iinFromServer
outToServer
server reads line from socket
server converts line to
uppercase, sends back to
client
inFromUser
client reads, prints modified
line from socket
(inFromServer stream) client socket
2: Application Layer 4
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
connectionSocket
close
connectionSocket close
clientSocket
2: Application Layer 5
Example: Java client (TCP)
import java.io.*;
import java.net.*;
class TCPClient {
sentence = inFromUser.readLine();
Send line
to server outToServer.writeBytes(sentence + '\n');
clientSocket.close();
}
}
2: Application Layer 7
Example: Java server (TCP)
import java.io.*;
import java.net.*;
class TCPServer {
2: Application Layer 8
Example: Java server (TCP), cont
Create output
stream, attached DataOutputStream outToClient =
to socket new DataOutputStream(connectionSocket.getOutputStream());
Read in line
from socket clientSentence = inFromClient.readLine();
2: Application Layer 9
Socket programming with UDP
2: Application Layer 10
Client/server socket interaction: UDP
Server (running on hostid) Client
create socket,
port=x, for create socket,
clientSocket =
incoming request: DatagramSocket()
serverSocket =
DatagramSocket()
Create, address (hostid, port=x),
send datagram request
using clientSocket
read request from
serverSocket
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port number close
clientSocket
2: Application Layer 11
Example: Java client (UDP)
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket DatagramSocket clientSocket = new DatagramSocket();
Translate
InetAddress IPAddress = InetAddress.getByName("hostname");
hostname to IP
address using DNS byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
2: Application Layer 13
Example: Java server (UDP)
import java.io.*;
import java.net.*;
class UDPServer {
public static void main(String args[]) throws Exception
Create {
datagram socket
DatagramSocket serverSocket = new DatagramSocket(9876);
at port 9876
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
Create space for
DatagramPacket receivePacket =
received datagram
new DatagramPacket(receiveData, receiveData.length);
Receive serverSocket.receive(receivePacket);
datagram
2: Application Layer 14
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
Get IP addr
InetAddress IPAddress = receivePacket.getAddress();
port #, of
sender int port = receivePacket.getPort();
sendData = capitalizedSentence.getBytes();
Create datagram
DatagramPacket sendPacket =
to send to client new DatagramPacket(sendData, sendData.length, IPAddress,
port);
Write out
datagram serverSocket.send(sendPacket);
to socket }
}
} End of while loop,
loop back and wait for
another client connection
2: Application Layer 15
Summary on Application Layer
Our study of network apps now complete!
application service
specific protocols:
requirements: http
reliability, bandwidth,
ftp
delay
smtp, pop3
client-server paradigm
dns
Internet transport
service model socket programming
connection-oriented, client/server
reliable: TCP implementation
unreliable, datagrams: UDP using tcp, udp sockets
2: Application Layer 16
Summary on Application Layer
Most importantly: learned about protocols
typical request/reply
control vs. data msgs
message exchange:
client requests info or
in-based, out-of-band
service centralized vs. decentralized
server responds with data, stateless vs. stateful
status code reliable vs. unreliable msg
message formats: transfer
headers: fields giving info “complexity at network
about data edge”
data: info being
security: authentication
communicated
2: Application Layer 17