Estos son los códigos que llevo hasta ahora:
Servidor:
Código
#ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include <windows.h> #include <WinSock2.h> #include <WS2tcpip.h> #include <IPHlpApi.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib, "Ws2_32.lib") #define DEFAULT_PORT "27016" #define DEFAULT_BUFLEN 516 int main() { WSADATA wsaData; int iResult; char recvbuf[DEFAULT_BUFLEN]; int recvbuflen = DEFAULT_BUFLEN; int recvBytes; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed with error: %d\n", iResult); return 1; } struct addrinfo *result = NULL, hints; ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); if (iResult != 0) { printf("getaddrinfo error: %d\n", iResult); WSACleanup(); return 1; } SOCKET ListenSocket = INVALID_SOCKET; ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); return 1; } iResult = bind(ListenSocket, result->ai_addr, result->ai_addrlen); if (iResult == SOCKET_ERROR) { printf("Error at bind: %d\n", iResult); freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); return 1; } freeaddrinfo(result); if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) { printf("Listen failed with: %ld\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return 1; } SOCKET ClientSocket = INVALID_SOCKET; ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { printf("accept failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); return 1; } printf("A CONNECTION HAS BEEN FOUND!!\n"); iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed with error: %d\n", iResult); closesocket(ClientSocket); WSACleanup(); return 1; } do { recvBytes = recv(ClientSocket, recvbuf, recvbuflen, 0); if (recvBytes > 0) { printf("Client >> "); for (int i = 0; i < recvBytes; ++i) { printf("%c", recvbuf[i]); } //printf("Bytes received: %d\n\n", recvBytes); } else if (recvBytes == 0) { printf("Connection closing...\n"); } else { printf("recv failed: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } } while (recvBytes > 0); closesocket(ClientSocket); WSACleanup(); system("pause>nul"); return 0; }
Cliente:
Código
#ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include <windows.h> #include <WinSock2.h> #include <WS2tcpip.h> #include <IPHlpApi.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib, "Ws2_32.lib") #define DEFAULT_PORT "27016" #define DEFAULT_BUFLEN 516 int main() { WSADATA wsaData; int iResult; char sendbuf[DEFAULT_BUFLEN]; int iSendResult; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed with error: %d\n", iResult); return 1; } 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; iResult = getaddrinfo("127.0.0.1", DEFAULT_PORT, &hints, &result); if (iResult != 0) { printf("getaddrinfo failed with error: %d/n", iResult); WSACleanup(); return 1; } SOCKET ConnectSocket = INVALID_SOCKET; ptr = result; ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (ConnectSocket == INVALID_SOCKET) { printf("Error at socket(): %ld\n", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); return 1; } iResult = connect(ConnectSocket, ptr->ai_addr, ptr->ai_addrlen); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; } freeaddrinfo(result); if (ConnectSocket == INVALID_SOCKET) { printf("Unable to connect to server!\n"); WSACleanup(); return 1; } do { printf("Send a message to the server: "); fgets(sendbuf, DEFAULT_BUFLEN, stdin); iSendResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0); if (iSendResult == SOCKET_ERROR) { printf("send failed: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } //printf("Bytes sent: %d\n\n", iSendResult); } while (iSendResult > 0); iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed: %d\n", iResult); closesocket(ConnectSocket); WSACleanup(); return 1; } closesocket(ConnectSocket); WSACleanup(); system("pause>nul"); return 0; }
Estoy trabajando en Visual Studio Community 2015.