赞
踩
初始化以后,SOCKET对象必须由客户端实例化
1.声明一个addrinfo对象,它包含一个sockaddr结构,然后初始化这些值。此应用程序,互联网地址族未指明,所以或者返回IPv6地址或者IPv4地址。应用程序要求socket类型为SOCK_STREAM(提供面向连接的稳定数据传输,即TCP协议)。
- struct addrinfo *result = NULL,
- *ptr = NULL,
- hints;
-
- ZeroMemory( &hints, sizeof(hints) );
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- #define DEFAULT_PORT "27015"
-
- // Resolve the server address and port
- iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
- if (iResult != 0) {
- printf("getaddrinfo failed: %d\n", iResult);
- WSACleanup();
- return 1;
- }
3.创建一个SOCKET 对象称为ConnectSocket。
SOCKET ConnectSocket = INVALID_SOCKET;
- // Attempt to connect to the first address returned by// the call to getaddrinfo
- ptr=result;
-
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
5.检查错误,保证
SOCKET
是有效的。
- if (ConnectSocket == INVALID_SOCKET) {
- printf("Error at socket(): %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- // Connect to server.
- iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- }
-
- // Should really try the next address returned by getaddrinfo// if the connect call failed// But for this simple example we just free the resources// returned by getaddrinfo and print an error message
-
- freeaddrinfo(result);
-
- if (ConnectSocket == INVALID_SOCKET) {
- printf("Unable to connect to server!\n");
- WSACleanup();
- return 1;
- }
- #define DEFAULT_BUFLEN 512
-
- int recvbuflen = DEFAULT_BUFLEN;
-
- char *sendbuf = "this is a test";
- char recvbuf[DEFAULT_BUFLEN];
-
- int iResult;
-
- // Send an initial buffer
- iResult = send(ConnectSocket, sendbuf, (int) strlen(sendbuf), 0);
- if (iResult == SOCKET_ERROR) {
- printf("send failed: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
-
- printf("Bytes Sent: %ld\n", iResult);
-
- // shutdown the connection for sending since no more data will be sent// the client can still use the ConnectSocket for receiving data
- iResult = shutdown(ConnectSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- printf("shutdown failed: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
-
- // Receive data until the server closes the connectiondo {
- iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0)
- printf("Bytes received: %d\n", iResult);
- else if (iResult == 0)
- printf("Connection closed\n");
- else
- printf("recv failed: %d\n", WSAGetLastError());
- } while (iResult > 0);
- // shutdown the send half of the connection since no more data will be sent
- iResult = shutdown(ConnectSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- printf("shutdown failed: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
2.当客户端应用程序完成接收数据时,closesocket函数被调用来关闭socket。当客户端应用程序完成后使用WS2_32.dll,WSACleanup 函数被调用以释放资源。
- // cleanup
- closesocket(ConnectSocket);
- WSACleanup();
-
- return 0;
编辑于2016年8月3日 By Seefun_Zhu
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。