控制台TCP聊天程序

来源:互联网 发布:淘宝秒杀软件 编辑:程序博客网 时间:2024/05/16 19:11

The WSAStartup function initiates use of Ws2_32.dll by a process.

int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
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.
1)加载套接字库
2)进行套接字版本协商

Example

The following code fragment demonstrates how an application that supports only version 2.2 of Windows 
Sockets makes a WSAStartup call:
 

WSADATA

The members of the WSADATA structure are:
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; 

Members

wVersion
Version of the Windows Sockets specification that the Ws2_32.dll expects the caller to use.
wHighVersion
Highest version of the Windows Sockets specification that this .dll can support (also encoded as above).
Normally this is the same as wVersion.
szDescription
Null-terminated ASCII string into which the Ws2_32.dll copies a description of the Windows Sockets implementation.
The text (up to 256 characters in length) can contain any characters except control and formatting characters:
the most likely use that an application can put this to is to display it (possibly truncated) in a status message.
szSystemStatus
Null-terminated ASCII string into which the WSs2_32.dll copies relevant status or configuration information.
The Ws2_32.dll should use this parameter only if the information might be useful to the user or support staff:
it should not be considered as an extension of the szDescription parameter.
iMaxSockets
Retained for backward compatibility, but should be ignored for Windows Sockets version 2 and later,
as no single value can be appropriate for all underlying service providers.
iMaxUdpDg
Ignored for Windows Sockets version 2 and onward. iMaxUdpDg is retained for compatibility with Windows Sockets specification 1.1,
but should not be used when developing new applications. For the actual maximum message size specific to a particular
Windows Sockets service provider and socket type, applications should use getsockopt to retrieve the
value of option SO_MAX_MSG_SIZE after a socket has been created.
lpVendorInfo
厂商预留

 

 

socket

The socket 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. 对于TCP/IP协议的套接字,它只能是AF_INET.
type
[in] Type specification for the new socket.

The following are the only two type specifications supported for Windows Sockets 1.1:

TypeExplanationSOCK_STREAMProvides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. Uses TCP for the Internet address family.SOCK_DGRAM

Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family.

protocol
[in] Protocol to be used with the socket that is specific to the indicated address family.

 

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

returned, and a specific error code can be retrieved by calling WSAGetLastError.

 

 

bind

The bind function associates a local address with a socket.

int bind(  SOCKET s,                            const struct sockaddr FAR *name,     int namelen                        );

Parameters

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

Return Values

If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

 

htonl

The htonl function converts a u_long from host to TCP/IP network byte order (which is big-endian).

u_long htonl(  u_long hostlong  );

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  );

listen

The listen function places a socket a state where it is listening for an incoming connection.
int listen(  SOCKET s,      int backlog  );
backlog
[in] Maximum length of the queue of pending connections.
If set to SOMAXCONN, the underlying service provider responsible for socket s
will set the backlog to a maximum reasonable value.
There is no standard provision to obtain the actual backlog value.

accept

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

send

The send function sends data on a connected socket.

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

recv

The recv function receives data from a connected or bound socket.

int recv(  SOCKET s,         char FAR *buf,    int len,          int flags       );

closesocket

The closesocket function closes an existing socket.

int closesocket(  SOCKET s  );

connect

The connect function establishes a connection to a specified socket.

int connect(  SOCKET s,                            const struct sockaddr FAR *name,    int namelen                        );