Download presentation
Presentation is loading. Please wait.
Published byEugenia Ball Modified over 8 years ago
1
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS447 - Computer and Data Communication SockAddr_Structure.ppt/000
2
CS447 - Computer and Data Communication How to specify your destination in socket? Server Host Client Host X IP Address Server Process Port # Each destination for a socket connection is determined by SockAddr_Structure.ppt/001
3
CS447 - Computer and Data Communication struct sockaddr_in { u_char sin_len; /* Length of this structure */ u_char sin_family; /* Network protocol used*/ u_short sin_port; /* Port number */ struct in_addr sin_addr; /* Pointer to an IP address */ char sin_zero[8]; /* Extra information */ }; How to specify your destination in our socket program source code? struct in_addr { u_long s_addr; /* Actual IP address */ }; sockaddr_in structure is used to define your destination The sockaddr_in structure is defined in C/C++ struct The sockaddr_in structure is defined in windows.h header file
4
CS447 - Computer and Data Communication How to specify your destination in our socket program source code (part 2)? An instance of sockaddr_in structure in memory Structure length (in bytes) Network-layer protocol Port number Pointer to in_addr structure Extra information An instance of in_addr structure in memory IP address as a binary number (32 bits) SockAddr_Structure.ppt/004
5
CS447 - Computer and Data Communication How to specify your destination in our socket program source code (part 3)? An instance of sockaddr_in structure in memory 128 IP protocol 1050 Extra information An instance of in_addr structure in memory 146 163 147 59 SockAddr_Structure.ppt/005
6
CS447 - Computer and Data Communication struct sockaddr_in server_address; How to specify your destination in our socket program source code (part 4)? How can I set the IP address and the port number of my destination? STEP #1: Instantiate a sockaddr_in structure: STEP #2: Set your destination IP address: Case 1: by “32-bit IP address” server_address.sin_addr.s_addr = inet_addr(“146.163.147.59”); Case 2: by a host name server_address.sin_addr.s_addr = inet_aton(“cougar.siue.edu”); Case 3: by a system-defined parameter server_address.sin_addr.s_addr = htonl(INADDR_ANY); SockAddr_Structure.ppt/006
7
CS447 - Computer and Data Communication How to specify your destination in our socket program source code (part 4)? How can I set the IP address and the port number of my destination? STEP #3: Set your destination port number: server_address.sin_port = htons(80); You are connecting to port #80! SockAddr_Structure.ppt/007
8
CS447 - Computer and Data Communication Example of setting IP address and port number SockAddr_Structure.ppt/008 #define SERVER_IP "146.163.144.99“ /* Server IP address */ #define SERVER_PORT 8050 /* Server-side port # */ struct sockaddr_in server_addr; /* Server Internet address */ /* Set Server's IP Address --------------------------------------------- */ server_addr.sin_family = AF_INET; /* Address Family to be Used */ server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); /* IP address */ server_addr.sin_port = htons(int(SERVER_PORT));/* Port num to use */
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.