控制台TCP聊天程序

本文介绍了Windows套接字编程的基础知识,包括WSAStartup函数的使用方法、套接字创建与配置,以及数据发送接收等核心操作。通过示例代码展示了如何初始化套接字库并确认版本兼容性。

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:

TypeExplanation
SOCK_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                        
);
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值