Sunteți pe pagina 1din 25

BASICS FUNCTION USED IN SOCKETS PROGRAMMING:

1. WSADATA:
The WSADATA structure contains information about the Windows 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, and Windows 95. Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server. Header: Declared in Winsock2.h.

2. WSAStartup:
The WSAStartup function initiates use of WS2_32.DLL by a process. int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData );

Parameters:
wVersionRequested [in] Highest version of Windows Sockets support that the caller can use. The high-order byte specifies the minor version (revision) number; the low-order byte specifies the major version number .

lpWSAData
[out] Pointer to the WSADATA data structure that is to receive details of the Windows Sockets implementation.

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

Remarks:
The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation. The application or DLL can only issue

further Windows Sockets functions after successfully calling WSAStartup.

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 2000 Server, and Windows NT Server. Header: Declared in Winsock2.h. Library: Use Ws2_32.lib.

3. WSACleanup:
The WSACleanup function terminates use of the WS2_32.DLL. int WSACleanup(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 successful WSAStartup call before it can use Windows Sockets

services. When it has completed the use of Windows Sockets, the application or DLL must call WSACleanup to deregister itself from a Windows Sockets implementation and allow the implementation to free any resources allocated on behalf of the application or DLL. There must be a call to WSACleanup for every successful call to WSAStartup made by a task. Only the final WSACleanup for that task does the actual cleanup; the preceding calls simply decrement 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, and Windows 95. Server: Included in Windows Server 2003, Windows 2000 Server, 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 specific service provider. SOCKET socket( int af, int type, int protocol );

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 specifications supported for Winsock 1.1. Type SOCK_STREAM Description Provides sequenced, reliable, two-way, connection-based byte streams with an out of band (OOB) data transmission mechanism. Uses TCP for the Internet address family. Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family. two type

SOCK_DGRAM

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

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

Return Values:
If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned.

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 2000 Server, 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. int bind( SOCKET s, const struct sockaddr* name, int namelen );

Parameters:
s [in] Descriptor identifying an unbound socket. name [in] Address to assign SOCKADDR structure. namelen [in] Length of the value in the name parameter, in bytes. to the socket from the

Return Values:
If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR.

Remarks:
The bind function is used on an unconnected socket before subsequent calls to the connect or listen functions. It is used to bind to either connection-oriented (stream) or connectionless (datagram) sockets. When a socket is created with a call to the socket function, it exists in a name space (address family), but it has no name assigned to it. Use the bind function to establish the local association of the socket by assigning a local name to an unnamed socket. A name consists of three parts when using the Internet address family:

The address family. A host address.

A port number that identifies the application.

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 2000 Server, 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 to which the connection should be established. namelen

[in] Length of the name ,in bytes.

Return Values:
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR.

Remarks:
The connect function is used to create a connection to the specified destination. If socket s is unbound, unique values are assigned to the local association by the system and the socket is marked as bound.

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 2000 Server, 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

);

Parameters:
s [in] Descriptor socket. backlog [in] Maximum length of the queue of pending connections. This is not the maximum number of connections that can be established at the given port at one time. It is the maximum number of connections or partial connections that can be queued waiting for the application to accept them. identifying a bound, unconnected

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 the socket function and bound to a local address with the bind function, a backlog for incoming connections is specified with listen, and the connections are accepted with the accept function. Sockets that are connection-oriented, those of type SOCK_STREAM for example, are used with listen. Socket s is put into passive mode where incoming connection requests are acknowledged and queued pending acceptance by the process. The listen function is typically used by servers that can have more than one connection request at a time. If a connection request arrives and the queue is full, the client

will receive an error WSAECONNREFUSED.

with

an

indication

of

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 2000 Server, and Windows NT Server. Header: Declared in Winsock2.h. Library: Use Ws2_32.lib.

8. Accept:
The accept function attempt on a socket. SOCKET accept( SOCKET s, struct sockaddr* addr, int* addrlen ); permits an incoming connection

Parameters:
s [in] Descriptor identifying a socket that has been placed in a listening state with the listen function. The connection is actually made with the socket that is returned by accept.

addr [out] Optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the SOCKADDR structure was created. Accept returns the address of the new connections peer in the SOCKADDR_IN structure pointed to by addr. addrlen [out] Optional pointer to an integer that contains the length of addr. We often donot care our peers address, and in this case we specify NULL for addr and addrlen.

Return Values:
If no error occurs, accept returns a value of type SOCKET that is a descriptor for the new socket. This returned value is a handle for the socket on which the actual connection is made. Otherwise, a value of INVALID_SOCKET is returned.

Remarks:
The accept function is used with connection-oriented socket types such as SOCK_STREAM. If addr and/or addrlen are equal 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

Windows 95. Server: Included in Windows Server 2003, Windows 2000 Server, 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 bound socket. int recv( SOCKET s, char* buf, int len, int flags );

Parameters:
s [in] Descriptor identifying a connected socket. buf [out] Buffer for the incoming data. len [in] Length of buf, in bytes flags [in] Flag specifying the way in which the call is made.

Return Values:
If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned.

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

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 2000 Server, 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.

int send( SOCKET s, const char* buf, int len, int flags );

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 bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned.

Remarks:
The send function is used to write outgoing data on a connected socket. For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying provider, which can be obtained by using getsockopt to retrieve the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned, and no data is transmitted. The successful completion of a send does not indicate that the data was successfully delivered.

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 2000 Server, 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_short htons( u_short hostshort );

Parameters:
hostshort [in] 16-bit number in host byte order.

Return Values:
The htons function returns the value in TCP/IP network byte order.

Remarks:
The htons function takes a 16-bit number in host byte order and returns a 16-bit number in network byte order used in TCP/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 2000 Server, 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 proper address for the IN_ADDR structure.

unsigned long inet_addr( const char* cp );

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

Return Values:
If no error occurs, inet_addr returns an unsigned long value containing a suitable binary representation of the Internet address given. If the string in the cp parameter does not contain a legitimate Internet address, for example if a portion of an "a.b.c.d" address exceeds 255, then inet_addr returns the value INADDR_NONE.

Remarks:
The inet_addr function interprets the character string specified by the cp parameter. This string represents a numeric Internet address expressed in the Internet standard ".'' notation. The value returned is a number suitable for use as an Internet address. All Internet addresses are returned in 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 the following forms:

a.b.c.d a.b.c a.b a When four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the 4 bytes of an Internet address. When an Internet address is viewed as a 32-bit integer quantity on the Intel architecture, the bytes referred to above appear as "d.c.b.a''. That is, the bytes on an Intel processor are ordered from right to left. The parts that make up an address in "." notation can be decimal, 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 numbers are interpreted as decimal. Internet address value "4.3.2.16" "004.003.002.020" "0x4.0x3.0x2.0x10" "4.003.002.0x10" Meaning Decimal Octal Hexadecimal Mix

Note The following notations are only used by Berkeley, and nowhere else on the Internet. For compatibility with their software, they are supported as specified. When a three-part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right-most 2 bytes of the network address. This makes the three-part address format convenient for specifying Class B network addresses as "128.net.host'' When a two-part address is specified, the last part is interpreted as a 24-bit quantity and placed in the right-most 3 bytes of the network address. This makes the two-part

address format convenient for specifying Class A network addresses as "net.host''. When only one part is given, the value is stored directly in the network address without any byte rearrangement.

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 2000 Server, 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 by Windows Sockets to specify a local or remote endpoint address to which to connect a socket. This is the form of the sockaddr structure specific to the Internet address family and can be cast to sockaddr. struct sockaddr_in{ short sin_family; unsigned short sin_port; IN_ADDR sin_addr; char sin_zero[8];

};

Members :
sin_family Address family; must be AF_INET. sin_port Internet Protocol (IP) port. sin_addr IP address in network byte order. sin_zero Padding to make SOCKADDR. structure the same size as

Remarks:
This is the form of the SOCKADDR structure specific to the Internet address family and can be cast to SOCKADDR. The IP address component of this structure is of type IN_ADDR. The IN_ADDR structure is defined in Windows Sockets header 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 2000 Server, and Windows NT Server. Header: Declared in Winsock2.h.
.

CLIENT

SERVER

NOTE:The TCP/IP network protocol suite is the standard method for intermachine communication. Though originally integral only to the UNIX operating system, its usage spread to all OS types, and it is the basis of the entire internet. A TCP/IP application consists of a pair of programs, called a server and a client. If for example you use a web browser to view www.yahoo.com, the browser is the client, and the Web Server at yahoo headquarters is the server. Client always initiates request tends to simplify the protocol as well as the program themselves. Server initiates message to the client. Server sends network message only in response to request coming in from the network. The other side of a protocol is a client which always initiates communication with the server.

Sockets:
Sockets are one of the most important IPC mechanisms on UNIX. Sockets are the only IPC mechanism that allows communication between processes running on different machines. Essentially, it is an end point of communication which may be bound to a name. But enough with the bland introduction. A socket is just a way to allow processes to talk to one another. It doesnt matter if they are running on the same machine, just as long as one knows the others contact details. Sockets are a

lot like using a phone-to contact somebody else, you have to have a phone and the other persons phone number. They also have to have one installed and must be listening for any incoming details.

TCP and UDP: As you should remember from networking, on top of IP, there are two major transport protocols on top of which all other protocols are built: TCP (Transmission Control Protocol ) and UDP (User Datagram Protocol ). These act as transportation mechanism for other, higher level protocols. TCP is a reliable, connection-oriented protocol that transmits data as a stream of bytes. UDP, on the other hand, is an unreliable ,connectionless protocol that sends data in chunks called datagrams.

PROTOCOL:
A protocol is a common language that the SERVER and the CLIENT both understand. In other words protocols is a set of rules whatever we have to follow. Whats the need of protocol:TCP is fine for transporting data across a network, after all thats what its for. However, TCP has no idea what data its transporting about. To best explain why you need a protocol, imagine if you called somebody in South Africa who didnt understand a word of Finnish and started talking down the line at them. Most likely, theyd think theres a lunatic on the other side and would just hang up. The same thing happens in network communications without a protocol, the machine involved

wouldnt know each other was blathering on about and would 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 of their own and so must be assigned to one that the client will know about. This is done by binding a socket to a well known address on the system. For instance, web-servers are usually bound to port 80 as this is the well known port for the HTTP protocol.

struct sockaddr This is a generic socket address structure provide to allow flexibility in passing socket addresses to functions. Struct sockaddr { Unsigned short sa_family; Char }; Struct sockaddr_in Struct sockaddr_in is a specialized version of struct sockaddr especially for the AF_INET address family. Struct sockaddr_in { Unsigned short sin_family; Unsigned short sin_port; //Set to AF_INET //Port number to bind to. sa_data[14]; //Address family tag. //Padding

Struct in_addr sin_addr; Char }; Struct in_addr sin_zero[8];

//IP address. //Padding

Struct in_addr represents an IP address. Why this structure exists and wasnt just incorporated directly into struct sockaddr_in is anybodys guess. Setting its one field, s_addr, to INADDR_ANY will leave it up to the server to choose an appropriate host IP address, and this is usually the best thing to do. Struct in_addr { Unsigned long s_addr ; }; // IP address

S-ar putea să vă placă și