Kiran-SocketProgramming

Sockaddr we dont need to deal with sockaddr

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern