Kiran-SocketProgramming

There is no realistic way for you to know the right

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ere is no realistic way for you to know the right right IP address to give bind() - what if the computer computer has multiple network interfaces? • How can you find out what your IP address is so you can tell bind() ? bind() Accepting Accepting connections 37 ECEN 602 Socket Programming Convert network byte ordered value to ASCII dotted-decimal (a string). dotted- char *inet_ntoa(struct in_addr); Convert ASCII dotted-decimal IP address to dottednetwork byte order 32 bit value. Returns 1 on success, 0 on failure. int inet_aton( char *, struct in_addr *); IPv4 IPv4 Address Conversion 38 Client / Server Session open_clientfd EOF close read write read close read accept listen write connect Connection request socket socket bind Server Client Overview Overview open_listenfd 39 // from back a couple slides // listen for incoming connections clen=sizeof(caddr) if((isock=accept(sockfd, (struct sockaddr *) &caddr, &clen)) < 0) { printf(“Error accepting\n”); ... } if(listen(sockfd, 5) < 0) { printf(“Error listening\n”); ... } if((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // bind! printf(“Error binding\n”); ... } // accept one memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind to any local address saddr.sin_port = htons(port); // specify port to listen on if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) { printf(“Error creating socket\n”); ... } struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; Lets Lets put the server together... // from back a couple slides if((connfd=connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // connect! printf(“Cannot connect\n”); ... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call memcpy((char *) &saddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); // copy the address saddr.sin_port = htons(port); // specify port to connect to if((h=gethostbyname(“www.slashdot.org”)) == NULL) { // Lookup the hostname printf(“Unknown host\n”); ... } if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) { printf(“Error creating socket\n”); ... } struct sockaddr_in saddr; struct hostent *h; int sockfd, connfd; unsigned short port = 80; Piecing Piecing the Client Together ECEN 602 Socket Programming http://www.ece.tamu.edu/~reddy/ee602_07.html Please refer to the course website for more information Thank Thank you! 42...
View Full Document

Ask a homework question - tutors are online