Elementary TCP Sockets

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with TCP – I
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
1 Elementary TCP Sockets socket function connect function bind function listen function accept function fork and exec functions Concurrent servers close.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Computer Networks Sockets.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Sockets IMGD Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Tutorial 8 Socket Programming
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
UNIX Sockets COS 461 Precept 1.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Assignment 3 A Client/Server Application: Chatroom.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
Elementary TCP Sockets
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Chapter 2 Applications and Layered Architectures Sockets.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Remote Shell CS230 Project #4 Assigned : Due date :
Distributed Computing A Programmer’s Perspective.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 K. Salah Application Layer Module K. Salah Network layer duties.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
Chapter4 Elementary TCP Socket
Socket Programming in C
Network Programming CSC- 341
Network Programming CSC- 341
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Presentation transcript:

Elementary TCP Sockets Unix Network Programming Ch # 4

Elementary Socket functions

Socket Function To perform network I/O, first thing a process must do is call the socket function #include <sys/socket.h> int socket(int family, int type, int protocol); - returns: non-negative descriptor if ok, -1 on error

Connect funciton The connect function is used by a TCP client to establish a connection with a TCP server: #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Returns: 0 if ok, -1 on error Sockfd is a socket descriptor returned by the socket function 2nd & 3rd args are the socket address structures, must contain the address of the server to communicate with The client does not have to call bind The kernel chooses both an ephemeral port and the source IP address if necessary.

Bind function The bind funtion assigns a local protocol address to a socket. With IP, combination of 32-bit (IPv4 or 128-bit for IPv6) address, along with a 16-bit TCP or UDP port number. #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Servers bind to their well-known port when they start A process can bind a specific IP address to its socket Normally, however, a client does not bind an IP address, so that client can then respond on any interface available on the host

Listen function The listen function is called only by a TCP server and it performs 2 actions Converts an unconnected (active) socket into a passive socket (indicates kernel should accept incoming connect requests directed to this socket 2nd argument specifies the maximum number of connections kernel should queue for this socket #include <sys/socket.h> int listen(int sockfd, int backlog);

Listen function Normally called after both the socket and bind function, only by the server of course Backlog - for a given listening socket, the kernel maintains 2 queues: An incomplete connection queue, which contains an entry for each SYN that has arrived from a client for which server is awaiting completion of the TCP 3-way handshake A completed connection queue, entry for each client with whom 3-way handshake has completed. Figure 4.7, pg. 105

Accept function Accept is called by a TCP server to return the next completed connection from the front of the completed connection queue. If completed queue is empty, the process is put to sleep. #include <sys/socket.h> int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); Returns: non-negative descriptor if OK, -1 on error The cliaddr and addrlen args are used to return the protocol address of the connect peer process (the client).

Fork and exec functions We will look at building a concurrent server Need to create a new child process to handle each incomming client request/transaction fork function is the only way in Unix to create a new process: #include <unistd.h> pid_t fork(void); Returns: 0 in child, process ID of child in parent, -1 on error Called once but returns TWICE Once in the parent process (returns child process id), and once in the child process (return of 0)

More Forking All descriptors open in the parent before the call to fork() are shared with the child after fork returns. Including the connected socket file description returned by accept

Exec function Only way in which an executable program file on disk can be executed in Unix is for an existing process to call one of the 6 exec functions

Concurrent Servers When a client request can take some time to service, don't want to take away time for handling connections to service a single client Handle the communication with multiple clients at the same time Simplest way to write a concurrent server under Unix is to fork a child process to handle each client.

Concurrent Servers

Close function Close() function used to close a socket and terminate a TCP connection #include <unistd.h> int close(int sockfd); Returns: 0 if ok, -1 on error Default action of close with a TCP socket description is to mark the socket as closed and return tot he process immediately. Socket descriptor is no longer usable to the app process at this point But TCP will try to send any data that is already queued, and once flushed begin the normal TCP termination sequence.