// shutdown the connection since we're done iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { sprintf(str,"shutdown failed with error: %d\r\n", WSAGetLastError()); cout«str; closesocket(ClientSocket); WSACleanup(); return 1; } // cleanup closesocket(ClientSocket); WSACleanup(); cout«"\n"; } return 0; }
клиент #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h> #include <iostream> using namespace std; #pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Mswsock.lib") #pragma comment (lib, "AdvApi32.lib") #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "27015" #define DEFAULT_PORT1 "27016" int main () { setlocale(LC_ALL, "rus"); int a=-1; while (a != 0) { cout «"Выберите нужный пункт:\n"« "1) Получение информации с первого сервера" « endl « "2) Получение информации со второго сервера" « endl « "0) Выход" « endl ; cout « "Введите номер пункта: "; cin » a; switch (a) { case 0: { break; } case 1 : { WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; struct addrinfo *result = NULL, *ptr = NULL, hints; char *sendbuf = "this is a test"; char recvbuf[DEFAULT_BUFLEN]; int iResult; int recvbuflen = DEFAULT_BUFLEN; char str[100]; // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { sprintf(str,"WSAStartup failed with error: %d\r\n", iResult); cout«str; system ("pause"); return 1; } ZeroMemory( &hints, sizeof(hints) ); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; // Resolve the server address and port iResult = getaddrinfo("localhost", DEFAULT_PORT, &hints, &result); if ( iResult != 0 ) { sprintf(str,"getaddrinfo failed with error: %d\r\n", iResult); cout«str; WSACleanup(); system ("pause"); return 1; } // Attempt to connect to an address until one succeeds for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) { // Create a SOCKET for connecting to server ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (ConnectSocket == INVALID_SOCKET) { sprintf(str,"socket failed with error: %ld\r\n", WSAGetLastError()); cout«str; WSACleanup(); system ("pause"); return 1; } // Connect to server. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; } break; } freeaddrinfo(result); if (ConnectSocket == INVALID_SOCKET) { sprintf(str,"Unable to connect to server!\r\n"); cout«str; WSACleanup(); system ("pause"); return 1; } // Send an initial buffer iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iResult == SOCKET_ERROR) { sprintf(str,"send failed with error: %d\r\n", WSAGetLastError()); cout«str; closesocket(ConnectSocket); WSACleanup(); system ("pause"); return 1; } sprintf(str,"Bytes Sent: %ld\r\n", iResult); cout«str; // shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { sprintf(str,"shutdown failed with error: %d\r\n", WSAGetLastError()); cout«str; closesocket(ConnectSocket); WSACleanup(); system ("pause"); return 1; } // Receive until the peer closes the connection do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if ( iResult > 0 ){ sprintf(str,"Bytes received: %d\r\n", iResult); cout«str; cout«recvbuf; } else if ( iResult == 0 ) { sprintf(str,"Connection closed\r\n"); cout«str; } else { sprintf(str,"recv failed with error: %d\r\n", WSAGetLastError()); cout«str; } } while( iResult > 0 ); // cleanup closesocket(ConnectSocket); WSACleanup(); system ("pause");
break; } case 2 : { WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; struct addrinfo *result = NULL, *ptr = NULL, hints; char *sendbuf = "this is a test"; char recvbuf[DEFAULT_BUFLEN]; int iResult; int recvbuflen = DEFAULT_BUFLEN; char str[100]; // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { sprintf(str,"WSAStartup failed with error: %d\r\n", iResult); cout«str; system ("pause"); return 1; } ZeroMemory( &hints, sizeof(hints) ); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; // Resolve the server address and port iResult = getaddrinfo("localhost", DEFAULT_PORT1, &hints, &result); if ( iResult != 0 ) { sprintf(str,"getaddrinfo failed with error: %d\r\n", iResult); cout«str; WSACleanup(); system ("pause"); return 1; } // Attempt to connect to an address until one succeeds for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) { // Create a SOCKET for connecting to server ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (ConnectSocket == INVALID_SOCKET) { sprintf(str,"socket failed with error: %ld\r\n", WSAGetLastError()); cout«str; WSACleanup(); system ("pause"); return 1; } // Connect to server. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; } break; } freeaddrinfo(result); if (ConnectSocket == INVALID_SOCKET) { sprintf(str,"Unable to connect to server!\r\n"); cout«str; WSACleanup(); system ("pause"); return 1; } // Send an initial buffer iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iResult == SOCKET_ERROR) { sprintf(str,"send failed with error: %d\r\n", WSAGetLastError()); cout«str; closesocket(ConnectSocket); WSACleanup(); system ("pause"); return 1; } sprintf(str,"Bytes Sent: %ld\r\n", iResult); cout«str; // shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { sprintf(str,"shutdown failed with error: %d\r\n", WSAGetLastError()); cout«str; closesocket(ConnectSocket); WSACleanup(); system ("pause"); return 1; } // Receive until the peer closes the connection do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if ( iResult > 0 ){ sprintf(str,"Bytes received: %d\r\n", iResult); cout«str; cout«recvbuf; } else if ( iResult == 0 ) { sprintf(str,"Connection closed\r\n"); cout«str; } else { sprintf(str,"recv failed with error: %d\r\n", WSAGetLastError()); cout«str; } } while( iResult > 0 ); // cleanup closesocket(ConnectSocket); WSACleanup(); system ("pause"); break; } } } return 0; }