cliente
Código C++:
Ver original
//cliente #include <stdio.h> #include <windows.h> #include <winsock.h> /* El chequeo de errores ha sido removido por claridad */ void main(void) { WSADATA WsaData; WORD wVersionRequerida = MAKEWORD (2, 2); /* Inicializar WinSock */ WSAStartup (wVersionRequerida, &WsaData); SOCKET Sock; Sock = socket (AF_INET, SOCK_STREAM, 0); SOCKADDR_IN DireccionServer; DireccionServer.sin_family = AF_INET; DireccionServer.sin_addr.S_un.S_un_b.s_b1 = 127; DireccionServer.sin_addr.S_un.S_un_b.s_b2 = 0; DireccionServer.sin_addr.S_un.S_un_b.s_b3 = 0; DireccionServer.sin_addr.S_un.S_un_b.s_b4 = 1; DireccionServer.sin_port = 5000; int iResult = connect (Sock, (SOCKADDR*) &DireccionServer, \ sizeof(DireccionServer)); if (iResult) { return; } unsigned Dato = 2; send (Sock, (char *)&Dato, sizeof (Dato), 0); unsigned Rta; recv (Sock, (char *)&Rta, sizeof (Rta), 0); closesocket (Sock); WSACleanup(); }
servidor
Código C++:
Ver original
//servidor #include <stdio.h> #include <windows.h> #include <winsock.h> /* El chequeo de errores ha sido removido por claridad */ void main(void) { /* Inicializar WinSock */ WSADATA WsaData; WORD wVersionRequerida = MAKEWORD (2, 2); WSAStartup (wVersionRequerida, &WsaData); SOCKET SockEscucha; SockEscucha = socket (AF_INET, SOCK_STREAM, 0); SOCKADDR_IN DireccionLocal; DireccionLocal.sin_family = AF_INET; DireccionLocal.sin_port = 5000; bind (SockEscucha, (SOCKADDR*)&DireccionLocal, sizeof (DireccionLocal)); int iResult = listen (SockEscucha, 5); SOCKET SockConexion = accept (SockEscucha, NULL, NULL); closesocket (SockEscucha); unsigned Dato; recv (SockConexion, (char *)&Dato, sizeof (Dato), 0); unsigned Rta = Dato * 2; send (SockConexion, (char *)&Rta, sizeof (Rta), 0); closesocket (SockConexion); WSACleanup(); }