Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 441 - Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default.

Similar presentations


Presentation on theme: "CPSC 441 - Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default."— Presentation transcript:

1 CPSC 441 - Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default HTTP server port) at cis.poly.edu. Anything typed in sent to port 80 at cis.poly.edu telnet cis.poly.edu 80 2. Type in a GET HTTP request: GET /~ross/ HTTP/1.1 Host: cis.poly.edu By typing this in (hit carriage return twice), you send this minimal (but complete) GET request to HTTP server 3. Look at response message sent by HTTP server!

2 CPSC 441 - Application Layer 2 Try SMTP interaction for yourself:  telnet servername 25 r see 220 reply from server r enter HELO, MAIL FROM, RCPT TO, DATA, QUIT commands above lets you send email without using email client (reader)

3 CPSC 441 - Application Layer 3 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API:  unreliable datagram  reliable, byte stream- oriented a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Goal: learn how to build client/server application that communicate using sockets

4 r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows to processes on remotely connected computers to talk to each other What is a socket?

5 r SOCK_STREAM  TCP  connection oriented, bidirectional  reliable, in-order delivery Two types of sockets SOCK_DGRAM  UDP  no connection  unreliable delivery, no guarantee on the order  can send/receive

6 CPSC 441 - Application Layer 6 Socket-programming using TCP Socket: a door between application process and end- end-transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one process to another process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet

7 CPSC 441 - Application Layer 7 Socket programming with TCP Client must contact server r server process must first be running r server must have created socket (door) that welcomes client’s contact Client contacts server by: r creating client-local TCP socket r specifying IP address, port number of server process r When client creates socket: client TCP establishes connection to server TCP r When contacted by client, server TCP creates new socket for server process to communicate with client  allows server to talk with multiple clients  source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint

8 Socket Programming in C CPSC 441 - Application Layer 8

9 Socket Programming - Flow socket() connect()‏ send() recv() close()... socket() bind() listen() accept() recv() send() close() wait for connection request from next client Client Server

10 r int s = socket(family, type,protocol);  family: AF_INET specifies Ipv4  type: SOCK_STREAM, SOCK_DGRAM  protocol: 0 (pseudo, IP ). See /etc/protocols socket() ‏

11 Transport Layer r Used to address processes on a host  0-1024 is usually reserved for known service Ports FTP Server Web Server 21 80 Network Layer DLL/Physical

12 r Bind(socket, localAdd, addLength)  Server specifies which port and address it will be listening to  socket: our socket descriptor  localAdd: socket address structure  addLength: length of the address bind()

13 struct sockaddr_in { u_char sin_len; // length of address u_char sin_family; // family of address u_short sin_port; // protocol port num struct in_addr sin_addr; // IP Addr char sin_zero[8]; // set to zero, used for padding }; Address Structure

14 r Declare address structure  struct sockaddr_in sockAdd; r Set family  sockAdd.sin_family = AF_INET; r Set IP address  sockAdd.sin_addr.s_addr = inet_pton(“127.0.0.1”) ‏ r Set port  sockAdd.sin_port = htons(9999); Address Structure

15  int status = listen(sock, queuelength);  status: -1 if error, 0 otherwise  sock: socket descriptor  queuelen: Number of clients that can “wait” for a connection  listen is non-blocking: returns immediately listen()

16  int s = accept(sock, &name, &namelen);  s: new socket for communication with client  sock: the listening socket  name: struct sockaddr, address of client  namelen: sizeof(name): size of client address structure  accept is blocking: waits for connection before returning accept()

17 r fork() is a C system call used to spawn child processes  Execution for both child and parent process continues at the next instruction  fork() returns 0 if this is the child process PID (>0) of the child process if this is the parent <0 if fork() fails r Used to keep listening on socket and talking on another socket System calls - fork()

18 r int send(int s_new, const void *buf, int len, int flags); s_new – socket descriptor buf – pointer to buffer len – size of buffer flags – can be safely set to 0 r int recv(int s_new, void *buf, int len, unsigned int flags); similar to send buf holds the data to be transferred Talking

19 Socket Programming in Java CPSC 441 - Application Layer 19

20 CPSC 441 - Application Layer 20 Client process client TCP socket Stream jargon r A stream is a sequence of characters that flow into or out of a process. r An input stream is attached to some input source for the process, e.g., keyboard or socket. r An output stream is attached to an output source, e.g., monitor or socket.

21 CPSC 441 - Application Layer 21 Example: Java client (TCP) 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

22 CPSC 441 - Application Layer 22 Example: Java client (TCP), cont. 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

23 CPSC 441 - Application Layer 23 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); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket

24 CPSC 441 - Application Layer 24 Example: Java server (TCP), cont 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

25 Demo A simple client – server example: Echo Server

26 r Socket Programming, Dan Rubinstein, http://www1.cs.columbia.edu/~danr/courses/6761/Fall00/intro/6761-1b- sockets.ppt http://www1.cs.columbia.edu/~danr/courses/6761/Fall00/intro/6761-1b- sockets.ppt r 15-441 Socket Programming, www.cs.cmu.edu/afs/cs/academic/class/15441- f01/www/lectures/lecture03.pptwww.cs.cmu.edu/afs/cs/academic/class/15441- f01/www/lectures/lecture03.ppt r Network Programming, Geoff Kuenning, www.cs.hmc.edu/~geoff/classes/hmc.cs105.200701/slides/class21_net2.ppt www.cs.hmc.edu/~geoff/classes/hmc.cs105.200701/slides/class21_net2.ppt r Socket Programming, Abhinav Jain, www.cs.purdue.edu/homes/jain8/cs422/pso3.ppt www.cs.purdue.edu/homes/jain8/cs422/pso3.ppt References


Download ppt "CPSC 441 - Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default."

Similar presentations


Ads by Google