+ All Categories
Home > Documents > Basics Function Used in Socket Programming

Basics Function Used in Socket Programming

Date post: 05-Apr-2018
Category:
Upload: sai61
View: 223 times
Download: 0 times
Share this document with a friend

of 25

Transcript
  • 8/2/2019 Basics Function Used in Socket Programming

    1/25

    BASICS FUNCTION USED IN SOCKETS

    PROGRAMMING:

    1. WSADATA:

    The WSADATA structure contains information about theWindows Sockets implementation.

    It is a typedef structure.

    typedef struct WSAData {WORD wVersion;WORD wHighVersion;

    char szDescription[WSADESCRIPTION_LEN+1];char szSystemStatus[WSASYS_STATUS_LEN+1];unsigned short iMaxSockets;unsigned short iMaxUdpDg;char FAR* lpVendorInfo;

    } WSADATA, *LPWSADATA;

    Details of members are not required.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.

  • 8/2/2019 Basics Function Used in Socket Programming

    2/25

    2. WSAStartup:

    The WSAStartup function initiates use of WS2_32.DLL by aprocess.

    intWSAStartup(WORDwVersionRequested,

    LPWSADATAlpWSAData

    );

    Parameters:

    wVersionRequested

    [in] Highest version of Windows Sockets support thatthe caller can use. The high-order byte specifies theminor version (revision) number; the low-order bytespecifies the major version number.

    lpWSAData

    [out] Pointer to the WSADATA data structure that isto receive details of the Windows Sockets

    implementation.

    Return Values:

    The WSAStartup function returns zero if successful.Otherwise, it returns one of the error codes.

    Remarks:

    The WSAStartup function must be the first WindowsSockets function called by an application or DLL. It allows anapplication or DLL to specify the version of Windows Socketsrequired and retrieve details of the specific Windows Socketsimplementation. The application or DLL can only issue

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/wsadata_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/wsadata_2.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    3/25

    further Windows Sockets functions after successfully callingWSAStartup.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    3. WSACleanup:

    The WSACleanup function terminates use of theWS2_32.DLL.

    intWSACleanup(void);

    Parameters:

    This function has no parameters.

    Return Values:

    The return value is zero if the operation was successful.

    Otherwise, the value SOCKET_ERROR is returned

    Remarks:

    An application or DLL is required to perform a successfulWSAStartup call before it can use Windows Sockets

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/wsastartup_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/wsastartup_2.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    4/25

    services. When it has completed the use of WindowsSockets, the application or DLL must call WSACleanup toderegister itself from a Windows Sockets implementationand allow the implementation to free any resources allocated

    on behalf of the application or DLL. There must be a call toWSACleanup for every successful call to WSAStartupmade by a task. Only the final WSACleanup for that taskdoes the actual cleanup; the preceding calls simplydecrement an internal reference count in the WS2_32.DLL.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    4. SOCKET:This function creates a socket that is bound to a specificservice provider.

    SOCKET socket(

    int af,

    int type,

    int protocol

    );

  • 8/2/2019 Basics Function Used in Socket Programming

    5/25

    Parameters:

    af

    [in] Address family specification.

    AF_INET for internet family .AF_UNIX for UNIX .

    type

    [in] Type specification for the new socket.

    The following table shows the only two typespecifications supported for Winsock 1.1.

    Type Description

    SOCK_STREAM Provides sequenced, reliable, two-way,connection-based byte streams with an outof band (OOB) data transmission

    mechanism. Uses TCP for the Internetaddress family.

    SOCK_DGRAM Supports datagrams, which areconnectionless, unreliable buffers of a fixed(typically small) maximum length. UsesUDP for the Internet address family.

    protocol

    [in] Protocol to be used with the socket that is specificto the indicated address family.The protocol field indicates which protocol should beused with the socket. With TCP/IP this is normally

  • 8/2/2019 Basics Function Used in Socket Programming

    6/25

    specified implicitly by the socket type, and theparameter is set to zero.Protocol is the transport protocol to use. Its best topass 0 (zero), which lets the system decide.

    Return Values:

    If no error occurs, socket returns a descriptor referencingthe new socket. Otherwise, a value of INVALID_SOCKET isreturned.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.

    Library: Use Ws2_32.lib

    5. BIND:

    The bind function associates a local address with a socket.

    intbind(

    SOCKETs,

    const struct sockaddr*name,

    intnamelen

    );

  • 8/2/2019 Basics Function Used in Socket Programming

    7/25

    Parameters:

    s[in] Descriptor identifying an unbound socket.

    name

    [in] Address to assign to the socket from theSOCKADDRstructure.

    namelen

    [in] Length of the value in the name parameter, inbytes.

    Return Values:

    If no error occurs, bind returns zero. Otherwise, itreturns SOCKET_ERROR.

    Remarks:

    The bind function is used on an unconnected socket beforesubsequent calls to the connect or listen functions. It isused to bind to either connection-oriented (stream) orconnectionless (datagram) sockets. When a socket iscreated with a call to the socket function, it exists in aname space (address family), but it has no name assignedto it. Use the bind function to establish the local associationof the socket by assigning a local name to an unnamed

    socket.A name consists of three parts when using the Internetaddress family:

    The address family.

    A host address.

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/sockaddr_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/connect_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/listen_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/sockaddr_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/connect_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/listen_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_2.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    8/25

    A port number that identifies the application.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    6. CONNECT:

    This function establishes a connection to a specified socket.

    int connect(

    SOCKET s,

    const struct SOCK_ADDR *name,

    int namelen

    );

    Parameters:

    s[in] Descriptor identifying an unconnected socket.

    name

    [in] Name of the socket in the SOCKADDR structure towhich the connection should be established.

    namelen

  • 8/2/2019 Basics Function Used in Socket Programming

    9/25

    [in] Length of the name ,in bytes.

    Return Values:

    If no error occurs, connect returns zero. Otherwise, itreturns SOCKET_ERROR.

    Remarks:

    The connect function is used to create a connection to the

    specified destination. If socket s is unbound, unique valuesare assigned to the local association by the system and thesocket is marked as bound.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.

    Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    7. LISTEN:

    This function places a socket at a state where it is listening

    for an incoming connection.

    int listen(

    SOCKET s,

    int backlog

  • 8/2/2019 Basics Function Used in Socket Programming

    10/25

    );

    Parameters:

    s[in] Descriptor identifying a bound, unconnectedsocket.

    backlog

    [in] Maximum length of the queue of pendingconnections. This is not the maximum number ofconnections that can be established at the given port atone time. It is the maximum number of connections orpartial connections that can be queued waiting for theapplication to accept them.

    Return Values:

    If no error occurs, listen returns zero. Otherwise, a value of

    SOCKET_ERROR is returned.

    Remarks:

    To accept connections, a socket is first created with thesocket function and bound to a local address with the bindfunction, a backlog for incoming connections is specified withlisten, and the connections are accepted with the acceptfunction. Sockets that are connection-oriented, those of typeSOCK_STREAM for example, are used with listen. Socket s

    is put into passive mode where incoming connectionrequests are acknowledged and queued pending acceptanceby the process.

    The listen function is typically used by servers that canhave more than one connection request at a time. If aconnection request arrives and the queue is full, the client

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/wcewinsk/htm/_wcesdk_socket.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/wcewinsk/htm/_wcesdk_accept_function.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/wcewinsk/htm/_wcesdk_socket.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/wcewinsk/htm/_wcesdk_accept_function.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    11/25

    will receive an error with an indication of WSAECONNREFUSED.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.

    Library: Use Ws2_32.lib.

    8. Accept:

    The accept function permits an incoming connectionattempt on a socket.

    SOCKETaccept(

    SOCKETs,

    struct sockaddr*addr,

    int*addrlen

    );

    Parameters:

    s[in] Descriptor identifying a socket that has beenplaced in a listening state with the listen function. Theconnection is actually made with the socket that isreturned by accept.

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/listen_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/listen_2.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    12/25

    addr[out] Optional pointer to a buffer that receives theaddress of the connecting entity, as known to thecommunications layer. The exact format of the addr

    parameter is determined by the address family thatwas established when the socket from the SOCKADDRstructure was created. Accept returns the address ofthe new connections peer in the SOCKADDR_INstructure pointed to by addr.

    addrlen

    [out] Optional pointer to an integer that contains the

    length ofaddr.

    We often donot care our peers address, and in thiscase we specify NULL for addr and addrlen.

    Return Values:

    If no error occurs, accept returns a value of type SOCKETthat is a descriptor for the new socket. This returned value is

    a handle for the socket on which the actual connection ismade.

    Otherwise, a value of INVALID_SOCKET is returned.

    Remarks:

    The accept function is used with connection-oriented sockettypes such as SOCK_STREAM. If addr and/or addrlen areequal to NULL, then no information about the remote

    address of the accepted socket is returned.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, and

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/sockaddr_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/sockaddr_2.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    13/25

    Windows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.

    Library: Use Ws2_32.lib.

    9. Recv:

    The recv function receives data from a connected or boundsocket.

    intrecv(

    SOCKETs,

    char*buf,

    intlen,

    intflags

    );

    Parameters:

    s[in] Descriptor identifying a connected socket.

    buf[out] Buffer for the incoming data.

    len[in] Length ofbuf, in bytes

    flags[in] Flag specifying the way in which the call is made.

  • 8/2/2019 Basics Function Used in Socket Programming

    14/25

    Return Values:

    If no error occurs, recv returns the number of bytesreceived. If the connection has been gracefully closed,the return value is zero. Otherwise, a value ofSOCKET_ERROR is returned.

    Remarks:

    The recv function is used to read incoming data onconnection-oriented sockets, or connectionless sockets.When using a connection-oriented protocol, the socketsmust be connected before calling recv. When using aconnectionless protocol, the sockets must be bound beforecalling recv.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    10. SEND:

    The send function sends data on a connected socket.

  • 8/2/2019 Basics Function Used in Socket Programming

    15/25

    intsend(

    SOCKETs,

    const char*buf,

    intlen, intflags

    );

    Parameters:

    s [in] Descriptor identifying a connected socket.

    buf[in] Buffer containing the data to be transmitted.

    len[in] Length of the data in buf, in bytes

    flags[in] Indicator specifying the way in which the call is

    made.

    Return Values:

    If no error occurs, send returns the total number of bytessent, which can be less than the number indicated by len.Otherwise, a value of SOCKET_ERROR is returned.

  • 8/2/2019 Basics Function Used in Socket Programming

    16/25

    Remarks:

    The send function is used to write outgoing data on a

    connected socket. For message-oriented sockets, care mustbe taken not to exceed the maximum packet size of theunderlying provider, which can be obtained by usinggetsockopt to retrieve the value of socket optionSO_MAX_MSG_SIZE. If the data is too long to passatomically through the underlying protocol, the errorWSAEMSGSIZE is returned, and no data is transmitted.

    The successful completion of a send does not indicate thatthe data was successfully delivered.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    11. htons:

    The htons function converts a u_short from host to TCP/IP

    network byte order (which is big-endian).

    u_shorthtons(

    u_shorthostshort

    );

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/getsockopt_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/windows_sockets_error_codes_2.htm#winsock.wsaemsgsize_2http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/getsockopt_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/windows_sockets_error_codes_2.htm#winsock.wsaemsgsize_2
  • 8/2/2019 Basics Function Used in Socket Programming

    17/25

    Parameters:

    hostshort

    [in] 16-bit number in host byte order.

    Return Values:

    The htons function returns the value in TCP/IP network byteorder.

    Remarks:The htons function takes a 16-bit number in host byte orderand returns a 16-bit number in network byte order used inTCP/IP networks.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, and

    Windows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    12. inet_addr:

    The inet_addr function converts a string containing an(Ipv4) Internet Protocol dotted address into a properaddress for the IN_ADDRstructure.

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/in_addr_2.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/winsock/winsock/in_addr_2.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    18/25

    unsigned longinet_addr(

    const char*cp

    );

    Parameters:

    cp[in] Null-terminated character string representing anumber expressed in the Internet standard ".'' (dotted)notation.

    Return Values:

    If no error occurs, inet_addr returns an unsigned longvalue containing a suitable binary representation of theInternet address given. If the string in the cp parameterdoes not contain a legitimate Internet address, for exampleif a portion of an "a.b.c.d" address exceeds 255, theninet_addr returns the value INADDR_NONE.

    Remarks:

    The inet_addr function interprets the character stringspecified by the cp parameter. This string represents anumeric Internet address expressed in the Internet standard".'' notation. The value returned is a number suitable for useas an Internet address. All Internet addresses are returnedin IP's network order (bytes ordered from left to right). If

    you pass in " " (a space) to the inet_addr function,inet_addr returns zero.

    Internet Addresses:

    Values specified using the ".'' notation take one of thefollowing forms:

  • 8/2/2019 Basics Function Used in Socket Programming

    19/25

    a.b.c.d a.b.c a.b a

    When four parts are specified, each is interpreted as a byteof data and assigned, from left to right, to the 4 bytes of anInternet address. When an Internet address is viewed as a

    32-bit integer quantity on the Intel architecture, the bytesreferred to above appear as "d.c.b.a''. That is, the bytes onan Intel processor are ordered from right to left.

    The parts that make up an address in "." notation can bedecimal, octal or hexadecimal as specified in the C language.Numbers that start with "0x" or "0X" imply hexadecimal.Numbers that start with "0" imply octal. All other numbersare interpreted as decimal.

    Internet address value Meaning

    "4.3.2.16" Decimal

    "004.003.002.020" Octal

    "0x4.0x3.0x2.0x10" Hexadecimal

    "4.003.002.0x10" Mix

    Note The following notations are only used by Berkeley,and nowhere else on the Internet. For compatibility withtheir software, they are supported as specified.

    When a three-part address is specified, the last part isinterpreted as a 16-bit quantity and placed in the right-most2 bytes of the network address. This makes the three-part

    address format convenient for specifying Class B networkaddresses as "128.net.host''

    When a two-part address is specified, the last part isinterpreted as a 24-bit quantity and placed in the right-most3 bytes of the network address. This makes the two-part

  • 8/2/2019 Basics Function Used in Socket Programming

    20/25

    address format convenient for specifying Class A networkaddresses as "net.host''.

    When only one part is given, the value is stored directly inthe network address without any byte rearrangement.

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, andWindows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.Library: Use Ws2_32.lib.

    13. Sockaddr_in :

    In the Internet address family, this structure is used byWindows Sockets to specify a local or remote endpointaddress to which to connect a socket. This is the form of thesockaddr structure specific to the Internet address familyand can be cast to sockaddr.

    struct sockaddr_in{

    shortsin_family;

    unsigned shortsin_port;

    IN_ADDRsin_addr;

    charsin_zero[8];

    http://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/wcewinsk/htm/cerefsockaddr.htmhttp://ms-help//MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/wcewinsk/htm/cerefsockaddr.htm
  • 8/2/2019 Basics Function Used in Socket Programming

    21/25

    };

    Members :sin_family

    Address family; must be AF_INET.

    sin_portInternet Protocol (IP) port.

    sin_addrIP address in network byte order.

    sin_zero

    Padding to make structure the same size asSOCKADDR.

    Remarks:

    This is the form of the SOCKADDRstructure specific to theInternet address family and can be cast to SOCKADDR. TheIP address component of this structure is of type IN_ADDR.

    The IN_ADDR structure is defined in Windows Socketsheader file winsock2.H

    Requirements:

    Client: Included in Windows XP, Windows 2000 Professional,Windows NT Workstation, Windows Me, Windows 98, and

    Windows 95.Server: Included in Windows Server 2003, Windows 2000Server, and Windows NT Server.Header: Declared in Winsock2.h.

    .

  • 8/2/2019 Basics Function Used in Socket Programming

    22/25

    NOTE:-

    The TCP/IP network protocol suite is the standard methodfor intermachine communication. Though originally integralonly to the UNIX operating system, its usage spread to allOS types, and it is the basis of the entire internet.

    A TCP/IP application consists of a pair of programs, called aserver and a client. If for example you use a web browser toview www.yahoo.com, the browser is the client, and theWeb Server at yahoo headquarters is the server.

    Client always initiates request tends to simplify the protocolas well as the program themselves. Server initiates messageto the client. Server sends network message only in

    response to request coming in from the network. The otherside of a protocol is a client which always initiatescommunication with the server.

    Sockets:

    Sockets are one of the most important IPC mechanismson UNIX. Sockets are the only IPC mechanism that allowscommunication between processes running on differentmachines. Essentially, it is an end point of communicationwhich may be bound to a name.

    But enough with the bland introduction. A socket is just away to allow processes to talk to one another. It doesntmatter if they are running on the same machine, just aslong as one knows the others contact details. Sockets are a

    CLIENT SERVER

    http://www.yahoo.com/http://www.yahoo.com/
  • 8/2/2019 Basics Function Used in Socket Programming

    23/25

    lot like using a phone-to contact somebody else, you have tohave a phone and the other persons phone number. Theyalso have to have one installed and must be listening for anyincoming details.

    TCP and UDP:

    As you should remember from networking, on top ofIP, there are two major transport protocols on top of

    which all other protocols are built: TCP (TransmissionControl Protocol ) and UDP (User Datagram Protocol ).

    These act as transportation mechanism for other,

    higher level protocols.

    TCP is a reliable, connection-oriented protocol thattransmits data as a stream of bytes. UDP, on the

    other hand, is an unreliable ,connectionless protocolthat sends data in chunks called datagrams.

    PROTOCOL:

    A protocol is a common language that the SERVER and theCLIENT both understand. In other words protocols is a set ofrules whatever we have to follow.

    Whats the need of protocol:-

    TCP is fine for transporting data across a network, after allthats what its for. However, TCP has no idea what data itstransporting about.

    To best explain why you need a protocol, imagine if youcalled somebody in South Africa who didnt understand aword of Finnish and started talking down the line at them.Most likely, theyd think theres a lunatic on the other sideand would just hang up. The same thing happens in networkcommunications without a protocol, the machine involved

  • 8/2/2019 Basics Function Used in Socket Programming

    24/25

    wouldnt know each other was blathering on about andwould probably get confused.

    Binding the sockets to a well-known address:For clients to connect to a server, they need to know

    its address, but sockets are created without an address oftheir own and so must be assigned to one that the client willknow about. This is done by binding a socket to a wellknown address on the system. For instance, web-servers areusually bound to port 80 as this is the well known port forthe HTTP protocol.

    struct sockaddr

    This is a generic socket address structure provide to allowflexibility in passing socket addresses to functions.

    Struct sockaddr

    {

    Unsigned short sa_family; //Address family tag.

    Char sa_data[14]; //Padding

    };

    Struct sockaddr_in

    Struct sockaddr_in is a specialized version of struct sockaddrespecially for the AF_INET address family.

    Struct sockaddr_in

    {

    Unsigned short sin_family; //Set to AF_INET

    Unsigned short sin_port; //Port number to bind to.

  • 8/2/2019 Basics Function Used in Socket Programming

    25/25

    Struct in_addr sin_addr; //IP address.

    Char sin_zero[8]; //Padding

    };

    Struct in_addr

    Struct in_addr represents an IP address. Why this structureexists and wasnt just incorporated directly into structsockaddr_in is anybodys guess.

    Setting its one field, s_addr, to INADDR_ANY will leave it upto the server to choose an appropriate host IP address, andthis is usually the best thing to do.

    Struct in_addr

    {

    Unsigned long s_addr ; // IP address

    };


Recommended