- Unix Socket - Summary
- Unix Socket - Client Example
- Unix Socket - Server Example
- Unix Socket - Helper Functions
- Unix Socket - Core Functions
- Unix Socket - IP Address Functions
- Unix Socket - Network Byte Orders
- Unix Socket - Ports and Services
- Unix Socket - Structures
- Unix Socket - Client Server Model
- Unix Socket - Network Host Names
- Unix Socket - Network Addresses
- Unix Socket - What is a Socket?
- Unix Socket - Home
Unix Socket Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Unix Socket - IP Address Functions
Unix provides various function calls to help you manipulate IP addresses. These functions convert Internet addresses between ASCII strings (what humans prefer to use) and network byte ordered binary values (values that are stored in socket address structures).
The following three function calls are used for IPv4 addressing −
int inet_aton(const char *strptr, struct in_addr *addrptr)
in_addr_t inet_addr(const char *strptr)
char *inet_ntoa(struct in_addr inaddr)
int inet_aton(const char *strptr, struct in_addr *addrptr)
This function call converts the specified string in the Internet standard dot notation to a network address, and stores the address in the structure provided. The converted address will be in Network Byte Order (bytes ordered from left to right). It returns 1 if the string was vapd and 0 on error.
Following is the usage example −
#include <arpa/inet.h> (...) int retval; struct in_addr addrptr memset(&addrptr, , sizeof(addrptr)); retval = inet_aton("68.178.157.132", &addrptr); (...)
in_addr_t inet_addr(const char *strptr)
This function call converts the specified string in the Internet standard dot notation to an integer value suitable for use as an Internet address. The converted address will be in Network Byte Order (bytes ordered from left to right). It returns a 32-bit binary network byte ordered IPv4 address and INADDR_NONE on error.
Following is the usage example −
#include <arpa/inet.h> (...) struct sockaddr_in dest; memset(&dest, , sizeof(dest)); dest.sin_addr.s_addr = inet_addr("68.178.157.132"); (...)
char *inet_ntoa(struct in_addr inaddr)
This function call converts the specified Internet host address to a string in the Internet standard dot notation.
Following is the usage example −
#include <arpa/inet.h> (...) char *ip; ip = inet_ntoa(dest.sin_addr); printf("IP Address is: %s ",ip); (...)Advertisements