
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of socket programming at the application layer, focusing on tcp and udp. Topics include principles of network applications, web and http, ftp, electronic mail, dns, p2p file sharing, and building a web server. Learn about socket apis, client-server paradigm, and how to create java client and server applications using both tcp and udp.
Typology: Slides
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















SOCKET PROGRAMMING
Socket API
socket
Goal: learn how to build client/server application that communicate using sockets
Docsity.com
process
TCP with buffers, variables
socket
controlled by application developer controlled by operating system
process
TCP with buffers, variables
socket
controlled by application developer
controlled by operating system
Docsity.com
2: Application Layer (^7)
STREAM JARGON
2: Application Layer (^8)
to network from network
inFromServer
inFromUser
keyboard monitor
clientSocket
input stream
input stream
output stream
TCP socket
client TCP socket
2: Application Layer 10
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));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
Create input stream
Create client socket, connect to server Create output stream attached to socket
2: Application Layer 11
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();
} }
Create input stream attached to socket
Send line to server
Read line from server
2: Application Layer 13
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence); } } }
Read in line from socket
Create output stream, attached to socket
Write out line to socket
End of while loop, loop back and wait for another client connection
2: Application Layer (^14)
CHAPTER 2: APPLICATION LAYER
»2.1 Principles of network
applications
»2.2 Web and HTTP
»2.3 FTP
»2.4 Electronic Mail
»2.5 DNS
»2.6 P2P file sharing
»2.7 Socket programming with TCP
»2.8 Socket programming with UDP
»2.9 Building a Web server
2: Application Layer 16
CLIENT/SERVER SOCKET INTERACTION: UDP
close clientSocket
Server (running on hostid )
read reply from clientSocket
create socket, clientSocket = DatagramSocket()
Client
Create, address ( hostid, port=x, send datagram request using clientSocket
create socket, port= x , for incoming request: serverSocket = DatagramSocket()
read request from serverSocket
write reply to serverSocket specifying client host address, port number
2: Application Layer 17
sendPacket
to network from network
receivePacket
inFromUser
keyboard monitor
clientSocket
UDP packet
input stream
UDP packet
UDP socket
Output: sends packet (TCP sent “byte stream”)
Input: receives packet (TCP received “byte stream”)
client UDP socket
2: Application Layer 19
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(); } }
Create datagram with data-to-send, length, IP addr, port
Send datagram to server
Read datagram from server
2: Application Layer 20
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);
Create datagram socket at port 9876
Create space for received datagram
Receive datagram