Download presentation
Presentation is loading. Please wait.
Published byLenard Barnett Modified over 8 years ago
1
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger
2
TCP Sockets Programming Creating a passive mode (server) socket Establishing an application-level connection send/receive data Terminating a connection 2 CPE 401/601 Lecture 3 : TCP Socket Programming
3
TCP state diagram
4
Creating a TCP socket int socket(int family, int type, int proto); – family: AF_INET, AF_INET6, AF_LOCAL, … – type: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RAW – protocol: IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP int sock; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ } 4 CPE 401/601 Lecture 3 : TCP Socket Programming
5
Binding to well known address int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr; 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)); 5 CPE 401/601 Lecture 3 : TCP Socket Programming
6
Establishing a passive mode TCP socket Passive mode: – Address already determined Tell the kernel to accept incoming connection requests directed at the socket address – Handle 3-way handshake Tell the kernel to queue incoming connections for us 6 CPE 401/601 Lecture 3 : TCP Socket Programming
7
listen() int listen(int sockfd, int backlog); sockfd is the TCP socket – already bound to an address backlog is the max number of buffered incoming connections – incomplete and completed handshakes When app is ready to handle a new connection – we need to ask the O.S. for the next connection 7 CPE 401/601 Lecture 3 : TCP Socket Programming
8
accept() int accept( int sockfd, struct sockaddr* cliaddr, socklen_t *addrlen); sockfd is the passive mode TCP socket – initiated by socket(), bind() and listen() cliaddr is a pointer to allocated space addrlen is a value-result argument – must be set to the size of cliaddr – will be set to the number of used bytes in cliaddr 8 CPE 401/601 Lecture 3 : TCP Socket Programming
9
accept() return value accept() returns a new socket descriptor – small positive integer or -1 on error After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls read() and write() operate a little differently on sockets! – vs. file operation! 9 CPE 401/601 Lecture 3 : TCP Socket Programming
10
Terminating a TCP connection int close(int sockfd); Either end of the connection can call the close() system call What if there is data being sent? If the other end has closed the connection and there is no buffered data, – reading from a TCP socket returns 0 to indicate EOF 10 CPE 401/601 Lecture 3 : TCP Socket Programming
11
Client Code TCP clients can call connect() which: – takes care of establishing an endpoint address for the client socket – attempts to establish a connection to the specified server 3-way handshake no need to call bind first, the O.S. will take care of assigning the local endpoint address – TCP port number, IP address 11 CPE 401/601 Lecture 3 : TCP Socket Programming
12
connect() int connect( int sockfd, const struct sockaddr *server, socklen_t addrlen); sockfd is an already created TCP socket server contains the address of the server connect() returns 0 if OK, -1 on error – No response to SYN segment (3 trials) – RST signal – ICMP destination unreachable (3 trials) 12 CPE 401/601 Lecture 3 : TCP Socket Programming
13
Reading from a TCP socket int read(int fd, char *buf, int max); By default read() will block until data 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! 13 CPE 401/601 Lecture 3 : TCP Socket Programming
14
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 readn(), writen() and readline() functions 14 CPE 401/601 Lecture 3 : TCP Socket Programming
15
Metaphor for Good Relationships 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 15 bind() accept() connect() check for errors read might return 1 byte *Copyright Dr. Laura’s Network Programming Corp. CPE 401/601 Lecture 3 : TCP Socket Programming
16
UDP Sockets
17
UDP Sockets Programming Creating UDP sockets – Client – Server Sending data Receiving data Connected Mode 17 CPE 401/601 Lecture 3: UDP Socket Programming
18
Creating a UDP socket int socket(int family, int type, int proto); int sock; sock = socket(PF_INET, SOCK_DGRAM,0); if (sock<0) { /* ERROR */ } 18 CPE 401/601 Lecture 3: UDP Socket Programming
19
Binding to well known address typically done by server only int mysock; struct sockaddr_in myaddr; Mysock=socket(PF_INET,SOCK_DGRAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons(1234); myaddr.sin_addr = htonl(INADDR_ANY); bind(mysock, &myaddr, sizeof(myaddr)); 19 CPE 401/601 Lecture 3: UDP Socket Programming
20
Sending UDP Datagrams ssize_t sendto( int sockfd, void *buff, size_t nbytes, int flags, const struct sockaddr* to, socklen_t addrlen); sockfd is a UDP socket buff is the address of the data (nbytes long) to is the destination address Return value is the number of bytes sent, – or -1 on error. 20 CPE 401/601 Lecture 3: UDP Socket Programming
21
sendto() The return value of sendto() indicates how much data was accepted by the O.S. for sending as a datagram – not how much data made it to the destination There is no error condition that indicates that the destination did not get the data!!! You can send 0 bytes of data! 21 CPE 401/601 Lecture 3: UDP Socket Programming
22
Receiving UDP Datagrams ssize_t recvfrom( int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr* from, socklen_t *fromaddrlen); sockfd is a UDP socket buff is the address of a buffer (nbytes long) from is the address of a sockaddr Return value is the number of bytes received and put into buff, or -1 on error 22 CPE 401/601 Lecture 3: UDP Socket Programming
23
recvfrom() If buff is not large enough, any extra data is lost forever... You can receive 0 bytes of data! The sockaddr at from is filled in with the address of the sender 23 CPE 401/601 Lecture 3: UDP Socket Programming
24
More recvfrom() recvfrom doesn’t return until there is a datagram available, – unless you do something special You should set fromaddrlen before calling If from and fromaddrlen are NULL we don’t find out who sent the data 24 CPE 401/601 Lecture 3: UDP Socket Programming
25
Typical UDP client code Create UDP socket Create sockaddr with address of server Call sendto(), sending request to the server – No call to bind() is necessary! Possibly call recvfrom() – if we need a reply 25 CPE 401/601 Lecture 3: UDP Socket Programming
26
Typical UDP Server code Create UDP socket and bind to well known address Call recvfrom() to get a request – noting the address of the client Process request and send reply back with sendto() 26 CPE 401/601 Lecture 3: UDP Socket Programming
27
Typical UDP Communication 27 CPE 401/601 Lecture 3: UDP Socket Programming
28
UDP Echo Server int mysock; struct sockaddr_in myaddr, cliaddr; char msgbuf[MAXLEN]; socklen_t clilen; int msglen; mysock = socket(PF_INET,SOCK_DGRAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( S_PORT ); myaddr.sin_addr = htonl( INADDR_ANY ); bind(mysock, &myaddr, sizeof(myaddr)); while (1) { clilen=sizeof(cliaddr); msglen=recvfrom(mysock,msgbuf,MAXLEN,0, cliaddr,&clilen); sendto(mysock,msgbuf,msglen,0,cliaddr,clilen); } 28 NEED TO CHECK FOR ERRORS!!! CPE 401/601 Lecture 3: UDP Socket Programming
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.