Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.

Slides:



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

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.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
Lecture 16 Overview. Creating a TCP socket int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr;
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
Tutorial 8 Socket Programming
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Some slides are in courtesy of J. Kurose and K. Ross Review of Previous Lecture Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
Lecture 8 UDP Sockets & I/O Multiplexing
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
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.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Elementary TCP Sockets
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
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,
 Wind River Systems, Inc Chapter - 13 Network Programming.
Elementary TCP Sockets
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
Lecture 15 Overview.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
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.
Network Programming Berkeley Sockets (BSD)
回到第一頁 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 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Transport layer API: Socket Programming
Chapter 2 Transport Layer Protocols TCP UDP SCTP
Network Programming CSC- 341
Network Programming CSC- 341
UDP Sockets Programming
Socket Programming in C
Lecture 11 Overview.
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Lecture 2 Socket Programming
Server-side Programming CSE 333 Autumn 2018
Server-side Programming CSE 333 Summer 2018
Server-side Programming CSE 333 Winter 2019
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Today’s topic: Basic TCP API
Presentation transcript:

Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing an application-level connection. send/receive data.send/receive data. Terminating a connection.Terminating a connection.

Netprog: TCP Sockets2 Creating a TCP socket int socket(int family,int type,int proto); int sock; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ }

Netprog: TCP Sockets3 Binding to well known address int mysock; struct sockaddr_in myaddr; mysock = socket(PF_INET,SOCK_STREAM,0); mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( 80 ); myaddr.sin_addr = htonl( INADDR_ANY ); bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr)); bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));

Netprog: TCP Sockets4 Establishing a passive mode TCP socket Passive mode: –Address already determined. –Tell the kernel to accept incoming connection requests directed at the socket address. 3-way handshake3-way handshake –Tell the kernel to queue incoming connections for us.

Netprog: TCP Sockets5 listen() int listen( int sockfd, int backlog); sockfd is the TCP socket (already bound to an address) backlog is the number of incoming connections the kernel should be able to keep track of (queue for us). listen() returns -1 on error (otherwise 0).

Netprog: TCP Sockets6 Accepting an incoming connection. Once we call listen(), the O.S. will queue incoming connectionsOnce we call listen(), the O.S. will queue incoming connections –Handles the 3-way handshake –Queues up multiple connections. When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.

Netprog: TCP Sockets7 accept() int accept( int sockfd, struct sockaddr* cliaddr, struct sockaddr* cliaddr, socklen_t *addrlen); socklen_t *addrlen); sockfd is the passive mode TCP socket. cliaddr is a pointer to allocated space. addrlen is a value-result argument –must be set to the size of cliaddr –on return, will be set to be the number of used bytes in cliaddr.

Netprog: TCP Sockets8 accept() return value accept() returns a new socket descriptor (small positive integer) or -1 on error. After returns a new socket descriptor, I/O can be done using the and system calls. After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls. and operate a little differently on sockets (vs. file operation)! read() and write() operate a little differently on sockets (vs. file operation)!

Netprog: TCP Sockets9 Terminating a TCP connection Either end of the connection can call the system call.Either end of the connection can call the close() system call. If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF.If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF.

Netprog: TCP Sockets10 Client Code TCP clients can call which:TCP clients can call connect() which: –takes care of establishing an endpoint address for the client socket. don’t need to call bind first, the O.S. will take care of assigning the local endpoint address (TCP port number, IP address). don’t need to call bind first, the O.S. will take care of assigning the local endpoint address (TCP port number, IP address). –Attempts to establish a connection to the specified server. 3-way handshake3-way handshake

Netprog: TCP Sockets11 connect() int connect( int sockfd, const struct sockaddr *server, const struct sockaddr *server, socklen_t addrlen); socklen_t addrlen); sockfd is an already created TCP socket. server contains the address of the server (IP Address and TCP port number) returns 0 if OK, -1 on error connect() returns 0 if OK, -1 on error

Netprog: TCP Sockets12 Reading from a TCP socket int read( int fd, char *buf, int max); By default will block until data is available.By default read() will block until data is available. reading from a TCP socket may return less than max bytes (whatever is available).reading from a TCP socket may return less than max bytes (whatever is available). You must be prepared to read data 1 byte at a time!You must be prepared to read data 1 byte at a time!

Netprog: TCP Sockets13 Writing to a TCP socket int write( int fd, char *buf, int num); write might not be able to write all num bytes (on a nonblocking socket).write might not be able to write all num bytes (on a nonblocking socket). The book includes readn(), writen() and readline() function definitions.The book includes readn(), writen() and readline() function definitions.

Netprog: TCP Sockets14 Metaphor for Good Relationships Copyright Dr. Laura’s Network Programming Corp. To succeed in relationships: –you need to establish your own identity. –you need to be open & accepting. –you need to establish contacts. –you need to take things as they come, not as you expect them. –you need to handle problems as they arise. bind() accept() connect() check for errors read might return 1 byte