Client/Server Computing & Socket Programming: CPSC 826 Application-Layer Protocols
Client/Server Computing & Socket Programming: CPSC 826 Application-Layer Protocols
application
Internetworking Overview transport
network
Application-layer protocols define: link
physical
Client/Server Computing & » The types of messages exchanged
» The syntax and semantics of
Socket Programming messages
local ISP
» The rules for when and how
messages are sent
Michele Weigle
Department of Computer Science Public protocols (defined in RFCs) regional ISP
Clemson University » HTTP, FTP, SMTP, POP, IMAP,
DNS
[email protected]
September 1, 2004 Proprietary protocols
» RealAudio, RealVideo
» IP telephony
http://www.cs.clemson.edu/~mweigle/courses/cpsc826 company
» … network
1 2
9 10
A TCP socket provides a reliable bi-directional Sockets are addressed using an IP address and port
number
communications channel from one process to another
» A “pair of pipes” abstraction
process process
socket
Local port numbers socket
(e.g., 6500)
Process Process TCP with TCP with
Internet buffers, buffers,
write bytes read variables variables
socket socket Internet addresses of hosts
read bytes write
End System (e.g., 130.127.48.4) End System
Host Host Internet domain
(end system) (end system) name of host DNS
e.g., access.cs.clemson.edu
17 18
read write
w ak
bytes
n
ay e
Client
process
Client creates a local TCP When the client creates a socket, “welcoming”
process ake socket socket
socket specifying the host and the client’s TCP establishes ay handsh
TCP 3-w
port number of server process connection to server’s TCP client bytes
» Java resolves host names to IP When contacted by a client, socket socket connection
socket socket
addresses using DNS server creates a new socket for host or bytes
Client contacts server server process to communicate server Internet
» Server process must be running with client Server
» Server must have created socket
» This allows the server to talk with Client
that “welcomes” client’s contact
multiple clients
19 20
Socket-programming using TCP Socket programming with TCP Example
Simple client-server example Client structure
inFromServer
Client reads from standard input
outToServer
(inFromUser stream), writes to
Client Server server via a socket (outToServer
stdin welcoming stream)
client socket socket
socket socket Server reads line from a socket
connection
stdout socket socket
Server converts line to uppercase and
writes back to client client socket
The client reads a line of text from standard input and
sends the text to the server via a socket Client reads from socket, (2) (3)
The server receives the line of text from the client and (inFromServer stream) prints
converts the line of characters to all uppercase modified line to standard output Standard Client
output Process
(4)
The server sends the converted line back to the client
The client receives the converted text and writes it to Standard
inFromUser
input
standard output 21
(1)
22
Socket programming with TCP Example Socket programming with TCP Example
Client/server TCP socket interaction in Java Java client
import java.io.*;
Server (running on torpedo1.cs.clemson.edu) import java.net.*;
class TCPClient {
create socket for incoming public static void main(String argv[]) throws Exception
request (port=6789)
{
welcomeSocket = new ServerSocket(...)
Client (running on shadow1.cs...) String sentence;
String modifiedSentence;
wait for incoming TCP create socket,
connection request connection setup connect to torpedo1.cs.clemson.edu,
port=6789 // Create (buffered) input stream using standard input
connectionSocket = clientSocket = new Socket(...)
welcomeSocket.accept() BufferedReader inFromUser = new BufferedReader(
new InputStreamReader(System.in));
write request using
read request from
connectionSocket clientSocket System.out.println("Client ready for input");
write reply to read reply from // Create client socket with connection to server at port 6789
connectionSocket program flow
clientSocket
create address
(torpedo1.cs.clemson.edu, port = 9876)
read request from and send datagram using
serverSocket clientSocket
Process Internet Process
write bytes read read reply from
socket socket write reply to
read bytes write serverSocket
clientSocket
specifying client IP address program flow
and port number
close
Host Host clientSocket data flow
(end system) (end system)
29 30