1 / 16

Ports

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 ) Standard ports for well-known applications. Telnet (23), ftp(21), http (80).

mavis
Télécharger la présentation

Ports

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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) Standard ports for well-known applications. Telnet (23), ftp(21), http (80). See /etc/services file on any UNIX machine for listing of services on reserved ports. • Ephemeral ports (1024-65535) For ordinary user-developed programs.

  2. Associations • A socket address is the triple: {protocol, local-IP, local-port} For example, {tcp, 130.245.1.44, 23} • An association is the 5-tuple that completely specifies the two end-points that comprise a connection: {protocol, local-IP, local-port, remote-IP, remote-port} For example: {tcp, 130.245.1.44, 23, 130.245.1.45, 1024}

  3. Associations…. • A half-association is {protocol, local-IP, local-port} or {protocol, remote-IP, remote-port} which specify each half of the connection. • The half-association is also called a socket or a transport address.

  4. UDP Daytime Client and Server • Server waits for requests on a known port. • Client sends a UDP request to server. • Server responds with daytime info. • No connection establishment! • No reliability!

  5. UDP Client

  6. #include "unp.h" int main(int argc, char **argv) { int sockfd, n, servlen; char req[10], recvline[MAXLINE + 1]; struct sockaddr_in servaddr; if( argc != 2 )err_quit(“usage : gettime <IP address>”); /* Create a UDP socket */ sockfd = Socket(AF_INET, SOCK_DGRAM, 0); /* Specify server’s IP address and port */ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server port */ Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

  7. /* Send message to server requesting date/time */ strcpy(req, “GET_TIME”); Sendto(sockfd, req, strlen(req), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); /* Read date/time from the socket */ servlen = sizeof(servaddr); n= Recvfrom(sockfd, recvline, MAXLINE, 0, (struct sockaddr *)&servaddr, &servlen); recvlen[n] = 0; printf(“%s”, recvlen); close(sockfd); }

  8. UDP Server

  9. #include "unp.h" #include <time.h> int main(int argc, char **argv) { int sockfd, clilen; struct sockaddr_in servaddr, cliaddr; char buff[MAXLINE], req[REQ_LEN]; time_t ticks; /* Create a socket */ sockfd = Socket(AF_INET, SOCK_DGRAM, 0); /* Initialize server’s address and well-known port */ bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); /* daytime server */ /* Bind server’s address and port to the socket */ Bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

  10. for ( ; ; ) { /* Wait for client request */ len = sizeof(cliaddr); n = Recvfrom( sockfd, req, REQ_LEN, 0, &cliaddr, &clilen); /* Retrieve the system time */ ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); /* Send to client*/ Sendto(sockfd, buff, strlen(buff), 0, &cliaddr, clilen); } }

  11. TCP Connection Sequence Server socket() Client bind() socket() listen() 3-way handshake connect() accept() write() data read() data write() read() EOF close() read() close()

  12. UDP Connection Sequence Server Client socket() socket() bind() sendto() data recvfrom() data sendto() recvfrom() close()

  13. Socket API summary • int socket( int family, int type, int protocol) Creates a network plug point that enables the client/server to communicate • int connect( int sockfd, const struct sockaddr *servaddr, socklen_t addrlen) Enables a client to connect to a server. • int bind( int sockfd, const struct sockaddr *myaddr, socklen_t addrlen) Allows a server to specify the IP address/port_number associated with a socket

  14. Socket API summary… • int listen(int sockfd, int backlog) Allows the server to specify that a socket can be used to accept connections. • int accept(int sockfd, struct sockaddr *client_addr, socklen_t *addrlen) Allows a server to wait till a new connection request arrives. • int close(int sockfd) Terminates any connection associated with a socket and releases the socket descriptor.

  15. Socket API summary… • int read(int sockfd, void *buf, int count); Read data from a TCP connection. • int write(int sockfd, void *buf, int count) Write data to a TCP connection. • int sendto(int sockfd, void *msg, … ) Send a UDP message on a socket to specified destination • int recvfrom(int sockfd, void *buf, … ) Recv a UDP message on a socket along with address of sending source. • Similarly check man pages for sendmsg(), recvmsg().

  16. Converting Between Address formats • From ASCII to numeric • “130.245.1.44”  32-bit network byte ordered value • inet_aton(…) with IPv4 • inet_pton(…) with IPv4 and IPv6 • From numeric to ASCII • 32-bit value  “130.245.1.44” • inet_ntoa(…) with IPv4 • inet_ntop(…) with IPv4 and IPv6 • Note – inet_addr(…) obsolete • cannot handle broadcast address “255.255.255.255” (0xFFFFFFFF)

More Related