控制台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.
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:
Type Explanation SOCK_STREAMProvides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. Uses TCP for the Internet address family.SOCK_DGRAMSupports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet 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 );
- 控制台TCP聊天程序
- 控制台聊天程序实例
- 控制台线程聊天程序
- 控制台线程聊天程序
- 【转】控制台聊天程序
- Linux 控制台聊天程序
- tcp简易聊天程序
- udp网络聊天控制台程序
- JavaSE_基于TCP协议的Socket实现的控制台聊天程序
- TCP聊天程序的使用说明
- Delphi简单TCP聊天程序
- TCP聊天程序学习笔记
- SOCKET控制台双线程聊天程序
- [Java] Socket/ServerSocket 控制台简单聊天程序
- 基于UDP的控制台聊天程序
- socket聊天程序(多线程,控制台)
- java控制台实现QQ聊天程序
- C#简易聊天机器人(控制台程序)
- alfresco中节点之间的关系
- JavaScript滚动新闻类 【转】
- 有用的话(选了别人帖子后43条比较喜欢)
- 开机显示windows/system32/config/system损坏解决办法
- 无废话C#设计模式之二:Singleton
- 控制台TCP聊天程序
- java打印的源代码(示例)
- C/C++中,GB2312转Unicode
- 50个Windows自带软件的免费替代品
- 杰卡斯葡萄酒(Jacob's Creek)
- 利用 WAS 6.1 WebService 功能部件包开发 JAX-WS 2.0 Web services
- VB 系统热键实现(模块)
- Dojo语法精讲
- ASUS P5K SE 开启 AHCI