Kiran-SocketProgramming

Sockaddr we dont need to deal with sockaddr

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: don’t need to deal with sockaddr structures structures since we will only deal with a real protocol family. • We can use sockaddr_in structures. sockaddr_in structures. TCP/IP TCP/IP Addresses 30 sa_data ECEN 602 Socket Programming sin_len AF_INET sa_len sa_family sin_zero sin_addr sin_port sockaddr_in sockaddr 31 ECEN 602 Socket Programming • bind returns 0 if successful or -1 on error. int bind( int sockfd, const const struct sockaddr *myaddr, int int addrlen); const! bind() • The bind() system call is used to assign an address to an existing socket. Assigning Assigning an address to a socket 32 ECEN 602 Socket Programming bind( mysock, (struct sockaddr*) &myaddr, sizeof(myaddr) ); • calling bind() assigns the address bind() specified by the sockaddr structure to sockaddr the socket descriptor. • You can give bind() a sockaddr_in bind() sockaddr_in structure: bind() bind() 33 ECEN 602 Socket Programming err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr)); mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( portnum ); myaddr.sin_addr = htonl( ipaddress); int mysock,err; struct sockaddr_in myaddr; bind() bind() Example 34 ECEN 602 Socket Programming – Client can ask the O.S. to assign any any available port number. – Client can bind to a specific port. – Server would like to bind to a well known address address (port number). bind() • There are a number of uses for bind(): Uses Uses for bind() bind() 35 ECEN 602 Socket Programming myaddr.port = htons(0); • Clients typically don’t care what port they are assigned. • When you call bind you can tell it to assign assign you any available port: Client Client Port 36 ECEN 602 Socket Programming • specify the IP address as: INADDR_ANY, INADDR_ANY this this tells the OS to take care of things. • Th...
View Full Document

This document was uploaded on 01/29/2014.

Ask a homework question - tutors are online