ICT 2406
Internet Programming
Lecture 8
Socket Programming
Faculty of Applied Sciences Rajarata University of Sri Lanka
M.K.C.Surangika B.Sc.(RJT), M.Sc.(MRT)
Client/Server Communication
Network based systems consist of a
server client media for communication
ICT2406 Internet Programming
Client/Server Communication
A computer running a program that makes request for services is called client machine. A computer running a program that offers requested services from one or more clients is called server machine. The media for communication can be wired or wireless network.
ICT2406 Internet Programming
Socket programming
Goal: learn how to build client/server application that communicate using sockets
Socket API
introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API:
UDP TCP
socket
A application-created, OS-controlled interface (a door) into which application process can both send and receive messages to/from another application process
ICT2406 Internet Programming
Socket programming basics
Server must be running Socket is locally before client can send identified with a port anything to it. number Analogous to the door # in Server must have a a building socket (door) through Client needs to know which it receives and server IP address and sends segments socket port number. Similarly client needs a socket
ICT2406 Internet Programming
5
Socket-programming using TCP
TCP service: reliable transfer of bytes from one process to another
controlled by application developer controlled by operating system
process
socket TCP with buffers, variables
process
socket TCP with buffers, variables
controlled by application developer
controlled by operating system
internet
host or server
host or server
ICT2406 Internet Programming
6
Socket programming with TCP
Client must contact server server process must first be running server must have created socket (door) that welcomes clients contact Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk with multiple clients
application viewpoint TCP provides reliable, in-order transfer of bytes (pipe) between client and server
7
ICT2406 Internet Programming
Client/server socket interaction: TCP
Server (running on hostid)
create socket, port=x, for incoming request: welcomeSocket = ServerSocket() wait for incoming connection request connection setup connectionSocket = welcomeSocket.accept() read request from connectionSocket write reply to connectionSocket
Client
TCP
create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket
read reply from clientSocket close clientSocket ICT2406 Internet Programming
close connectionSocket
Stream jargon
A stream is a sequence of characters that flow into or out of a process. An input stream is attached to some input source for the process, e.g., keyboard or socket. An output stream is attached to an output source, e.g., monitor or socket.
keyboard monitor input stream
inFromUser
Client Process process
output stream
inFromServer
outToServer
input stream
client TCP clientSocket socket
to network
TCP socket
from network
9
ICT2406 Internet Programming
Socket programming with TCP
Example client-server app: 1) client reads line from standard input, sends to server via socket 2) The server reads line from its connection socket 3) The server converts line to uppercase, 4) The server sends modified line back to client 5) The client reads modified line from its socket, prints modified line
ICT2406 Internet Programming
10
Example: Java client (TCP)
import java.io.*; import java.net.*; class TCPClient {
public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; Create input stream Create client socket, connect to server BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
ICT2406 Internet Programming
11
Create output stream attached to socket
Example: Java client (TCP), cont.
Create input stream attached to socket
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); Send line to server Read line from server outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close(); }
}
ICT2406 Internet Programming
12
Example: Java server (TCP)
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);
Create welcoming socket at port 6789
Wait, on welcoming socket for contact by client Create input stream, attached to socket
while(true) {
Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
ICT2406 Internet Programming
13
Example: Java server (TCP), cont
Create output stream, attached to socket
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n';
Read in line from socket
Write out line to socket } }
outToClient.writeBytes(capitalizedSentence);
} End of while loop, loop back and wait for another client connection
ICT2406 Internet Programming
14
TCP observations & questions
Server has two types of sockets:
ServerSocket and Socket
When client knocks on serverSockets door, server creates connectionSocket and completes TCP conx. Dest IP and port are not explicitly attached to segment. Can multiple clients use the server?
ICT2406 Internet Programming
15
Sources
[1] Slides adapted from, Computer Networking: A Top Down Approach, 5th ed., Jim Kurose, Keith Ross, Addison-Wesley, April 2009, Lecture Slides Chapter 1
[2] Kurose, James F. & Ross, Keith W. (2005) Computer Networking A Top-Down Approach Featuring the Internet. 3rd ed., Pearson Education Asia
ICT2406 Internet Programming
16