You are on page 1of 4

An Internet socket or network socket is an endpoint of a bidirectional interprocess communication flow across an Internet Protocol-, such as the Internet.

The term Internet sockets is also used as a name for an application programming interface (API) for the TCP/IP protocol stack, usually provided by the operating system.

An application programming interface (API) is a particular set of rules ('code') and specifications that software programs can follow to communicate with each other. It serves as an interface between different software programs and facilitates their interaction, similar to the way the user interface facilitates interaction between humans and computers.

Internet sockets constitute a mechanism for delivering incoming data packets to the appropriate application process or thread, based on a combination of local and remote IP addresses and port numbers. Each socket is mapped by the operating system to a communicating application process or thread.

A socket address is the combination of an IP address (the location of the computer) and a port (which is mapped to the application program process) into a single identity, much like one end of a telephone connection is the combination of a phone number and a particular extension.

Socket types
There are several Internet socket types available: Datagram sockets, A Datagram socket is a type of connectionless Internet socket, which is the sending or receiving point for packet delivery services.[1] Each packet sent or received on a Datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may arrive in any order and might not arrive at the receiving computer. UDP broadcasts sends are always enabled on a Datagram Socket. In order to receive broadcast packets a Datagram Socket should be bound to the wildcard address. Broadcasted packets may also be received when a Datagram Socket is bound to a more specific address. Stream sockets, In computer networking, a stream socket is a type of internet socket which provides a connection-oriented, sequenced, and unduplicated flow of data without record boundaries, with well-defined mechanisms for creating and destroying connections and for detecting errors.

This internet socket type transmits data on a reliable basis, in order, and with out-ofband capabilities. Traditionally, stream sockets are implemented on top of TCP so that applications can run across any networks using TCP/IP protocol. SCTP can also be used for stream sockets.

The Berkeley sockets application programming interface (API) comprises a library for developing applications in the C programming language that perform inter-process communication, most commonly for communications across a computer network.

Header files
The Berkeley socket interface is defined in several header files. The names and content of these files differ slightly between implementations. In general, they include: <sys/socket.h> Core BSD socket functions and data structures. <netinet/in.h> AF_INET and AF_INET6 address families and their corresponding protocol families PF_INET and PF_INET6. Widely used on the Internet, these include IP addresses and TCP and UDP port numbers. <sys/un.h> PF_UNIX/PF_LOCAL address family. Used for local communication between programs running on the same computer. Not used on networks. <arpa/inet.h> Functions for manipulating numeric IP addresses. <netdb.h> Functions for translating protocol names and host names into numeric addresses. Searches local data as well as DNS. [edit]Socket

API functions

This list is a summary of functions or methods provided by the Berkeley sockets API library:

socket() creates a new socket of a certain socket type, identified by an integer number,
and allocates system resources to it.

bind() is typically used on the server side, and associates a socket with a socket
address structure, i.e. a specified local port number and IP address.

listen() is used on the server side, and causes a bound TCP socket to enter listening
state.

connect() is used on the client side, and assigns a free local port number to a socket.
In case of a TCP socket, it causes an attempt to establish a new TCP connection.

accept() is used on the server side. It accepts a received incoming attempt to create a
new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection.

send() and recv(), or write() and read(), or sendto() and recvfrom(), are
used for sending and receiving data to/from a remote socket.

close() causes the system to release resources allocated to a socket. In case of TCP,
the connection is terminated.

gethostbyname() and gethostbyaddr() are used to resolve host names and


addresses. IPv4 only.

select() is used to prune a provided list of sockets for those that are ready to read,
ready to write, or that have errors.

poll() is used to check on the state of a socket in a set of sockets. The set can be
tested to see if any socket can be written to, read from or if an error occurred.

getsockopt() is used to retrieve the current value of a particular socket option for the
specified socket.

setsockopt() is used to set a particular socket option for the specified socket

socket() simply returns to you a socket descriptor that you can use in later system

calls, or -1 on error. The global variable errno is set to the error's value (see the perror() man page.)

if(-1 == SocketFD) { perror("can not create socket"); exit(EXIT_FAILURE); } memset(&stSockAddr, 0, sizeof(stSockAddr));

stSockAddr.sin_family = AF_INET; stSockAddr.sin_port = htons(1100); stSockAddr.sin_addr.s_addr = INADDR_ANY; if(-1 == bind(SocketFD,(struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) { perror("error bind failed"); close(SocketFD); exit(EXIT_FAILURE); } if(-1 == listen(SocketFD, 10)) { perror("error listen failed"); close(SocketFD); exit(EXIT_FAILURE); }

21: File Transfer Protocol (FTP) 22: Secure Shell (SSH) 23: Telnet remote login service 25: Simple Mail Transfer Protocol (SMTP) 53: Domain Name System (DNS) service 80: Hypertext Transfer Protocol (HTTP) used in the World Wide Web 110: Post Office Protocol (POP) 119: Network News Transfer Protocol (NNTP) 143: Internet Message Access Protocol (IMAP) 161: Simple Network Management Protocol (SNMP) 443: HTTP Secure (HTTPS)

You might also like