Unix Socket – IP Address Functions ”; Previous Next 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 valid 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: %sn”,ip); (…) Print Page Previous Next Advertisements ”;
Category: Computer Programming
Unix Socket – Core Functions
Unix Socket – Core Functions ”; Previous Next This chapter describes the core socket functions required to write a complete TCP client and server. The following diagram shows the complete Client and Server interaction − The socket Function To perform network I/O, the first thing a process must do is, call the socket function, specifying the type of communication protocol desired and protocol family, etc. #include <sys/types.h> #include <sys/socket.h> int socket (int family, int type, int protocol); This call returns a socket descriptor that you can use in later system calls or -1 on error. Parameters family − It specifies the protocol family and is one of the constants shown below − Family Description AF_INET IPv4 protocols AF_INET6 IPv6 protocols AF_LOCAL Unix domain protocols AF_ROUTE Routing Sockets AF_KEY Ket socket This chapter does not cover other protocols except IPv4. type − It specifies the kind of socket you want. It can take one of the following values − Type Description SOCK_STREAM Stream socket SOCK_DGRAM Datagram socket SOCK_SEQPACKET Sequenced packet socket SOCK_RAW Raw socket protocol − The argument should be set to the specific protocol type given below, or 0 to select the system”s default for the given combination of family and type − Protocol Description IPPROTO_TCP TCP transport protocol IPPROTO_UDP UDP transport protocol IPPROTO_SCTP SCTP transport protocol The connect Function The connect function is used by a TCP client to establish a connection with a TCP server. #include <sys/types.h> #include <sys/socket.h> int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); This call returns 0 if it successfully connects to the server, otherwise it returns -1 on error. Parameters sockfd − It is a socket descriptor returned by the socket function. serv_addr − It is a pointer to struct sockaddr that contains destination IP address and port. addrlen − Set it to sizeof(struct sockaddr). The bind Function The bind function assigns a local protocol address to a socket. With the Internet protocols, the protocol address is the combination of either a 32-bit IPv4 address or a 128-bit IPv6 address, along with a 16-bit TCP or UDP port number. This function is called by TCP server only. #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct sockaddr *my_addr,int addrlen); This call returns 0 if it successfully binds to the address, otherwise it returns -1 on error. Parameters sockfd − It is a socket descriptor returned by the socket function. my_addr − It is a pointer to struct sockaddr that contains the local IP address and port. addrlen − Set it to sizeof(struct sockaddr). You can put your IP address and your port automatically A 0 value for port number means that the system will choose a random port, and INADDR_ANY value for IP address means the server”s IP address will be assigned automatically. server.sin_port = 0; server.sin_addr.s_addr = INADDR_ANY; NOTE − All ports below 1024 are reserved. You can set a port above 1024 and below 65535 unless they are the ones being used by other programs. The listen Function The listen function is called only by a TCP server and it performs two actions − The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket. The second argument to this function specifies the maximum number of connections the kernel should queue for this socket. #include <sys/types.h> #include <sys/socket.h> int listen(int sockfd,int backlog); This call returns 0 on success, otherwise it returns -1 on error. Parameters sockfd − It is a socket descriptor returned by the socket function. backlog − It is the number of allowed connections. The accept Function The accept function is called by a TCP server to return the next completed connection from the front of the completed connection queue. The signature of the call is as follows − #include <sys/types.h> #include <sys/socket.h> int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); This call returns a non-negative descriptor on success, otherwise it returns -1 on error. The returned descriptor is assumed to be a client socket descriptor and all read-write operations will be done on this descriptor to communicate with the client. Parameters sockfd − It is a socket descriptor returned by the socket function. cliaddr − It is a pointer to struct sockaddr that contains client IP address and port. addrlen − Set it to sizeof(struct sockaddr). The send Function The send function is used to send data over stream sockets or CONNECTED datagram sockets. If you want to send data over UNCONNECTED datagram sockets, you must use sendto() function. You can use write() system call to send data. Its signature is as follows − int send(int sockfd, const void *msg, int len, int flags); This call returns the number of bytes sent out, otherwise it will return -1 on error. Parameters sockfd − It is a socket descriptor returned by the socket function. msg − It is a pointer to the data you want to send. len − It is the length of the data you want to send (in bytes). flags − It is set to 0. The recv Function The recv function is used to receive data over stream sockets or CONNECTED datagram sockets. If you want to receive data over UNCONNECTED datagram sockets you must use recvfrom(). You can use read() system call to read the data. This call is explained in helper functions chapter. int recv(int sockfd, void *buf, int len, unsigned int flags); This call returns the number of bytes read into the buffer, otherwise it will return -1 on error. Parameters sockfd − It is a socket descriptor returned by the socket function. buf − It is the buffer to read the information into. len − It is the maximum length of the buffer. flags − It is set to 0. The sendto Function The sendto function is used to send data over UNCONNECTED datagram sockets. Its signature is as follows − int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct
Unix Socket – Summary
Unix Socket – Summary ”; Previous Next Here is a list of all the functions related to socket programming. Port and Service Functions Unix provides the following functions to fetch service name from the /etc/services file. struct servent *getservbyname(char *name, char *proto) − This call takes a service name and a protocol name and returns the corresponding port number for that service. struct servent *getservbyport(int port, char *proto) − This call takes a port number and a protocol name and returns the corresponding service name. Byte Ordering Functions unsigned short htons (unsigned short hostshort) − This function converts 16-bit (2-byte) quantities from host byte order to network byte order. unsigned long htonl (unsigned long hostlong) − This function converts 32-bit (4-byte) quantities from host byte order to network byte order. unsigned short ntohs (unsigned short netshort) − This function converts 16-bit (2-byte) quantities from network byte order to host byte order. unsigned long ntohl (unsigned long netlong) − This function converts 32-bit quantities from network byte order to host byte order. IP Address Functions 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 is valid and 0 on error. 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. 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. Socket Core Functions int socket (int family, int type, int protocol) − This call returns a socket descriptor that you can use in later system calls or it gives you -1 on error. int connect (int sockfd, struct sockaddr *serv_addr, int addrlen) − The connect function is used by a TCP client to establish a connection with a TCP server. This call returns 0 if it successfully connects to the server, otherwise it returns -1. int bind(int sockfd, struct sockaddr *my_addr,int addrlen) − The bind function assigns a local protocol address to a socket. This call returns 0 if it successfully binds to the address, otherwise it returns -1. int listen(int sockfd, int backlog) − The listen function is called only by a TCP server to listen for the client request. This call returns 0 on success, otherwise it returns -1. int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) − The accept function is called by a TCP server to accept client requests and to establish actual connection. This call returns a non-negative descriptor on success, otherwise it returns -1. int send(int sockfd, const void *msg, int len, int flags) − The send function is used to send data over stream sockets or CONNECTED datagram sockets. This call returns the number of bytes sent out, otherwise it returns -1. int recv (int sockfd, void *buf, int len, unsigned int flags) − The recv function is used to receive data over stream sockets or CONNECTED datagram sockets. This call returns the number of bytes read into the buffer, otherwise it returns -1 on error. int sendto (int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen) − The sendto function is used to send data over UNCONNECTED datagram sockets. This call returns the number of bytes sent, otherwise it returns -1 on error. int recvfrom (int sockfd, void *buf, int len, unsigned int flags struct sockaddr *from, int *fromlen) − The recvfrom function is used to receive data from UNCONNECTED datagram sockets. This call returns the number of bytes read into the buffer, otherwise it returns -1 on error. int close (int sockfd) − The close function is used to close a communication between the client and the server. This call returns 0 on success, otherwise it returns -1. int shutdown (int sockfd, int how) − The shutdown function is used to gracefully close a communication between the client and the server. This function gives more control in comparison to close function. It returns 0 on success, -1 otherwise. int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) − This function is used to read or write multiple sockets. Socket Helper Functions int write (int fildes, const void *buf, int nbyte) − The write function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes. Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned. int read (int fildes, const void *buf, int nbyte) − The read function attempts to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf. Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned. int fork (void) − The fork function creates a new process. The new process, called the child process, will be an exact copy of the calling process (parent process). void bzero (void *s, int nbyte) − The bzero function places nbyte null bytes in the string s. This function will be used to set all the socket structures with null values. int bcmp (const void *s1, const void *s2, int nbyte) − The bcmp function compares the byte string s1 against the byte string s2. Both the strings are assumed to be nbyte bytes long. void bcopy (const void *s1,
Unix Socket – Home
Unix Socket Tutorial PDF Version Quick Guide Resources Job Search Discussion Sockets are communication points on the same or different computers to exchange data. Sockets are supported by Unix, Windows, Mac, and many other operating systems. The tutorial provides a strong foundation by covering basic topics such as network addresses, host names, architecture, ports and services before moving into network address functions and explaining how to write client/server codes using sockets. Audience This tutorial has been designed for everyone interested in learning the data exchange features of Unix Sockets. Prerequisites Learning Sockets is not at all a difficult task. We assume that you are well versed with the basic concepts of C programming. Print Page Previous Next Advertisements ”;
Unix Socket – Client Example
Unix Socket – Client Examples ”; Previous Next To make a process a TCP client, you need to follow the steps given below &minus ; Create a socket with the socket() system call. Connect the socket to the address of the server using the connect() system call. Send and receive data. There are a number of ways to do this, but the simplest way is to use the read() and write() system calls. Now let us put these steps in the form of source code. Put this code into the file client.c and compile it with gcc compiler. Run this program and pass hostname and port number of the server, to connect to the server, which you already must have run in another Unix window. #include <stdio.h> #include <stdlib.h> #include <netdb.h> #include <netinet/in.h> #include <string.h> int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,”usage %s hostname portn”, argv[0]); exit(0); } portno = atoi(argv[2]); /* Create a socket point */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror(“ERROR opening socket”); exit(1); } server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,”ERROR, no such hostn”); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); /* Now connect to the server */ if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { perror(“ERROR connecting”); exit(1); } /* Now ask for a message from the user, this message * will be read by server */ printf(“Please enter the message: “); bzero(buffer,256); fgets(buffer,255,stdin); /* Send message to the server */ n = write(sockfd, buffer, strlen(buffer)); if (n < 0) { perror(“ERROR writing to socket”); exit(1); } /* Now read server response */ bzero(buffer,256); n = read(sockfd, buffer, 255); if (n < 0) { perror(“ERROR reading from socket”); exit(1); } printf(“%sn”,buffer); return 0; } Print Page Previous Next Advertisements ”;
Unix Socket – Quick Guide
Unix Socket – Quick Guide ”; Previous Next What is a Socket? Sockets allow communication between two different processes on the same or different machines. To be more precise, it”s a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else. To a programmer, a socket looks and behaves much like a low-level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes. Sockets were first introduced in 2.1BSD and subsequently refined into their current form with 4.2BSD. The sockets feature is now available with most current UNIX system releases. Where is Socket Used? A Unix Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connection between client and server and then for exchanging data. Socket Types There are four types of sockets available to the users. The first two are most commonly used and the last two are rarely used. Processes are presumed to communicate only between sockets of the same type but there is no restriction that prevents communication between sockets of different types. Stream Sockets − Delivery in a networked environment is guaranteed. If you send through the stream socket three items “A, B, C”, they will arrive in the same order − “A, B, C”. These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries. Datagram Sockets − Delivery in a networked environment is not guaranteed. They”re connectionless because you don”t need to have an open connection as in Stream Sockets − you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol). Raw Sockets − These provide users access to the underlying communication protocols, which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more cryptic facilities of an existing protocol. Sequenced Packet Sockets − They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as a part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets, either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming packets. What is Next? The next few chapters are meant to strengthen your basics and prepare a foundation before you can write Server and Client programs using socket. If you directly want to jump to see how to write a client and server program, then you can do so but it is not recommended. It is strongly recommended that you go step by step and complete these initial few chapters to make your base before moving on to do programming. Unix Socket – Network Addresses Before we proceed with the actual stuff, let us discuss a bit about the Network Addresses − the IP Address. The IP host address, or more commonly just IP address, is used to identify hosts connected to the Internet. IP stands for Internet Protocol and refers to the Internet Layer of the overall network architecture of the Internet. An IP address is a 32-bit quantity interpreted as four 8-bit numbers or octets. Each IP address uniquely identifies the participating user network, the host on the network, and the class of the user network. An IP address is usually written in a dotted-decimal notation of the form N1.N2.N3.N4, where each Ni is a decimal number between 0 and 255 decimal (00 through FF hexadecimal). Address Classes IP addresses are managed and created by the Internet Assigned Numbers Authority (IANA). There are five different address classes. You can determine which class an IP address is in by examining the first four bits of the IP address. Class A addresses begin with 0xxx, or 1 to 126 decimal. Class B addresses begin with 10xx, or 128 to 191 decimal. Class C addresses begin with 110x, or 192 to 223 decimal. Class D addresses begin with 1110, or 224 to 239 decimal. Class E addresses begin with 1111, or 240 to 254 decimal. Addresses beginning with 01111111, or 127 decimal, are reserved for loopback and for internal testing on a local machine [You can test this: you should always be able to ping 127.0.0.1, which points to yourself]; Class D addresses are reserved for multicasting; Class E addresses are reserved for future use. They should not be used for host addresses. Example Class Leftmost bits Start address Finish address A 0xxx 0.0.0.0 127.255.255.255 B 10xx 128.0.0.0 191.255.255.255 C 110x 192.0.0.0 223.255.255.255 D 1110 224.0.0.0 239.255.255.255 E 1111 240.0.0.0 255.255.255.255 Subnetting Subnetting or subnetworking basically means to branch off a network. It can be done for a variety of reasons like network in an organization, use of different physical media (such as Ethernet, FDDI, WAN, etc.), preservation of address space, and security. The most common reason is to control network traffic. The basic idea in subnetting is to partition the host identifier portion of the IP address into two parts − A
Unix Socket – Network Addresses ”; Previous Next Before we proceed with the actual stuff, let us discuss a bit about the Network Addresses − the IP Address. The IP host address, or more commonly just IP address, is used to identify hosts connected to the Internet. IP stands for Internet Protocol and refers to the Internet Layer of the overall network architecture of the Internet. An IP address is a 32-bit quantity interpreted as four 8-bit numbers or octets. Each IP address uniquely identifies the participating user network, the host on the network, and the class of the user network. An IP address is usually written in a dotted-decimal notation of the form N1.N2.N3.N4, where each Ni is a decimal number between 0 and 255 decimal (00 through FF hexadecimal). Address Classes IP addresses are managed and created by the Internet Assigned Numbers Authority (IANA). There are five different address classes. You can determine which class an IP address is in by examining the first four bits of the IP address. Class A addresses begin with 0xxx, or 1 to 126 decimal. Class B addresses begin with 10xx, or 128 to 191 decimal. Class C addresses begin with 110x, or 192 to 223 decimal. Class D addresses begin with 1110, or 224 to 239 decimal. Class E addresses begin with 1111, or 240 to 254 decimal. Addresses beginning with 01111111, or 127 decimal, are reserved for loopback and for internal testing on a local machine [You can test this: you should always be able to ping 127.0.0.1, which points to yourself]; Class D addresses are reserved for multicasting; Class E addresses are reserved for future use. They should not be used for host addresses. Example Class Leftmost bits Start address Finish address A 0xxx 0.0.0.0 127.255.255.255 B 10xx 128.0.0.0 191.255.255.255 C 110x 192.0.0.0 223.255.255.255 D 1110 224.0.0.0 239.255.255.255 E 1111 240.0.0.0 255.255.255.255 Subnetting Subnetting or subnetworking basically means to branch off a network. It can be done for a variety of reasons like network in an organization, use of different physical media (such as Ethernet, FDDI, WAN, etc.), preservation of address space, and security. The most common reason is to control network traffic. The basic idea in subnetting is to partition the host identifier portion of the IP address into two parts − A subnet address within the network address itself; and A host address on the subnet. For example, a common Class B address format is N1.N2.S.H, where N1.N2 identifies the Class B network, the 8-bit S field identifies the subnet, and the 8-bit H field identifies the host on the subnet. Print Page Previous Next Advertisements ”;
Unix Socket – Network Host Names ”; Previous Next Host names in terms of numbers are difficult to remember and hence they are termed by ordinary names such as Takshila or Nalanda. We write software applications to find out the dotted IP address corresponding to a given name. The process of finding out dotted IP address based on the given alphanumeric host name is known as hostname resolution. A hostname resolution is done by special software residing on high-capacity systems. These systems are called Domain Name Systems (DNS), which keep the mapping of IP addresses and the corresponding ordinary names. The /etc/hosts File The correspondence between host names and IP addresses is maintained in a file called hosts. On most of the systems, this file is found in /etc directory. Entries in this file look like the following − # This represents a comments in /etc/hosts file. 127.0.0.1 localhost 192.217.44.207 nalanda metro 153.110.31.18 netserve 153.110.31.19 mainserver centeral 153.110.31.20 samsonite 64.202.167.10 ns3.secureserver.net 64.202.167.97 ns4.secureserver.net 66.249.89.104 www.google.com 68.178.157.132 services.amrood.com Note that more than one name may be associated with a given IP address. This file is used while converting from IP address to host name and vice versa. You would not have access to edit this file, so if you want to put any host name along with IP address, then you would need to have root permission. Print Page Previous Next Advertisements ”;
Unix Socket – Structures
Unix Socket – Structures ”; Previous Next Various structures are used in Unix Socket Programming to hold information about the address and port, and other information. Most socket functions require a pointer to a socket address structure as an argument. Structures defined in this chapter are related to Internet Protocol Family. sockaddr The first structure is sockaddr that holds the socket information − struct sockaddr { unsigned short sa_family; char sa_data[14]; }; This is a generic socket address structure, which will be passed in most of the socket function calls. The following table provides a description of the member fields − Attribute Values Description sa_family AF_INET AF_UNIX AF_NS AF_IMPLINK It represents an address family. In most of the Internet-based applications, we use AF_INET. sa_data Protocol-specific Address The content of the 14 bytes of protocol specific address are interpreted according to the type of address. For the Internet family, we will use port number IP address, which is represented by sockaddr_in structure defined below. sockaddr in The second structure that helps you to reference to the socket”s elements is as follows − struct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; }; Here is the description of the member fields − Attribute Values Description sa_family AF_INET AF_UNIX AF_NS AF_IMPLINK It represents an address family. In most of the Internet-based applications, we use AF_INET. sin_port Service Port A 16-bit port number in Network Byte Order. sin_addr IP Address A 32-bit IP address in Network Byte Order. sin_zero Not Used You just set this value to NULL as this is not being used. in addr This structure is used only in the above structure as a structure field and holds 32 bit netid/hostid. struct in_addr { unsigned long s_addr; }; Here is the description of the member fields − Attribute Values Description s_addr service port A 32-bit IP address in Network Byte Order. hostent This structure is used to keep information related to host. struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list #define h_addr h_addr_list[0] }; Here is the description of the member fields − Attribute Values Description h_name ti.com etc. It is the official name of the host. For example, tutorialspoint.com, google.com, etc. h_aliases TI It holds a list of host name aliases. h_addrtype AF_INET It contains the address family and in case of Internet based application, it will always be AF_INET. h_length 4 It holds the length of the IP address, which is 4 for Internet Address. h_addr_list in_addr For Internet addresses, the array of pointers h_addr_list[0], h_addr_list[1], and so on, are points to structure in_addr. NOTE − h_addr is defined as h_addr_list[0] to keep backward compatibility. servent This particular structure is used to keep information related to service and associated ports. struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; }; Here is the description of the member fields − Attribute Values Description s_name http This is the official name of the service. For example, SMTP, FTP POP3, etc. s_aliases ALIAS It holds the list of service aliases. Most of the time this will be set to NULL. s_port 80 It will have associated port number. For example, for HTTP, this will be 80. s_proto TCP UDP It is set to the protocol used. Internet services are provided using either TCP or UDP. Tips on Socket Structures Socket address structures are an integral part of every network program. We allocate them, fill them in, and pass pointers to them to various socket functions. Sometimes we pass a pointer to one of these structures to a socket function and it fills in the contents. We always pass these structures by reference (i.e., we pass a pointer to the structure, not the structure itself), and we always pass the size of the structure as another argument. When a socket function fills in a structure, the length is also passed by reference, so that its value can be updated by the function. We call these value-result arguments. Always, set the structure variables to NULL (i.e., ””) by using memset() for bzero() functions, otherwise it may get unexpected junk values in your structure. Print Page Previous Next Advertisements ”;
Unix Socket – Useful Resources ”; Previous Next The following resources contain additional information on Unix Socket. Please use them to get more in-depth knowledge on this. Useful Video Courses Socket Programming in C from Scratch 11 Lectures 1 hours Eduonix Learning Solutions More Detail Network Socket programming in C Practical Way 8 Lectures 1.5 hours Musab Zayadneh More Detail Network sockets and streaming (C# based) 17 Lectures 1.5 hours Taurius Litvinavicius More Detail TCP/IP Socket Programming HandsOn-Windows & Linux in C & C++ 29 Lectures 9 hours Sonali Shrivastava More Detail Creating a Web Crawler in Python using Socket Programming 17 Lectures 2 hours Mgh Gh More Detail Create a 3D multi-player game using THREE.js and Socket.IO 35 Lectures 2.5 hours Nicholas Lever More Detail Print Page Previous Next Advertisements ”;