cabe resaltar que mi cliente- servidor ya hace mensajes grupales pero faltan los mensajes de un cliente a otro en especifico.
codigo Serever
Código:
#include "stdafx.h" #undef UNICODE #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string> using namespace std; stringstream sstm; #pragma comment (lib, "Ws2_32.lib") #define DEFAULT_PORT "27015" #define MAX 100 bool acept(SOCKET Misocket); bool NonBlock_Recv(SOCKET s, char* Buffer); SOCKET ClientSocket[MAX]; int iContador = 0; int _tmain(int argc, _TCHAR* argv[]) { char MyMessage[100] = ""; SOCKET ListenSocket = INVALID_SOCKET; WSADATA wsaData; int iResult; int iClient = 0; struct addrinfo *result = NULL; struct addrinfo hints; int iSendResult; char Mimensaje[128] = ""; iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { cout << "WSAStartup fallo con : " << iResult << endl; WSACleanup(); return -1; } cout << "WSAStartup.....ok" << endl; 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) { cout << "getAddrInfo fallo con: " << iResult << endl; WSACleanup(); return -1; } cout << "Get AdrrInfo....Ok" << endl; ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { cout << "Socket fallo con error" << WSAGetLastError() << endl; FreeAddrInfo(result); WSACleanup(); return -1; } cout << "Socket.....ok" << endl; iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { cout << " bind fallo con error:" << WSAGetLastError() << endl; freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); return -1; } freeaddrinfo(result); cout << "Bind....ok" << endl; iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { cout << "listen fallo con error ...." << WSAGetLastError() << endl; closesocket(ListenSocket); WSACleanup(); return -1; } cout << "Listen...ok" << endl; while (true) { int i = 0; if (acept(ListenSocket)) iContador++; while (i < iContador) { char Mensaje[100] = ""; if (NonBlock_Recv(ClientSocket[i], Mensaje)) { int len; cout << "Cliente " << i << ": " << Mensaje << endl; len = strlen(Mensaje); for (int cont=0; cont < iContador; cont++) { if (cont == i) { send(ClientSocket[cont], "I am:" , 100, 0); send(ClientSocket[cont], Mensaje, len, 0); }else { char message[128]=""; sprintf_s(message, "cliente %i dijo:", i); send(ClientSocket[cont], message, 128, 0); send(ClientSocket[cont], Mensaje, 100, 0); } } } i++; } } system("pause"); return 0; } bool acept(SOCKET Misocket) { fd_set object; FD_ZERO(&object); FD_SET(Misocket, &object); timeval time; time.tv_sec = 0; time.tv_usec = 0; static int iClient = 0; if (select(1, &object, NULL, NULL, &time)) { ClientSocket[iClient] = accept(Misocket, NULL, NULL); cout << "Cliente " << iClient <<":"<< endl; char message[128] = " "; sprintf_s(message, "Eres el cliente %i.", iClient); int Size = strlen(message); send(ClientSocket[iClient], message, Size, 0); iClient++; return true; } return false; } bool NonBlock_Recv(SOCKET s, char* Buffer) { fd_set readSet;//es un set FD_ZERO(&readSet);//limpia ek set FD_SET(s, &readSet); //agrega al set timeval time;//para ver cuanto tiempo debemos esperar time.tv_sec = 0;//segundos time.tv_usec = 0;//milisegundos if (select(1, &readSet, NULL, NULL, &time)) { recv(s, Buffer, 100, NULL); return true; } return false; }
codigo cliente
Código:
#include "stdafx.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h> #include<conio.h> #include<iostream> #include<string.h> #pragma comment(lib, "Ws2_32.lib") #pragma comment (lib, "Mswsock.lib") #pragma comment (lib, "AdvApi32.lib") #define DEFAULT_BUFLEN 128 #define DEFAULT_PORT "27015" #define ENTER 13 using namespace std; bool NonBlockGetLine(char* sz); bool NonBlock_Recv(SOCKET s, char* szBuffer); int _tmain(int argc, _TCHAR* argv[]) { WSADATA wsaData; int iResult; struct addrinfo *result = NULL, *ptr = NULL; struct addrinfo hints; int iSendResult=0; iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { cout << "WSAStartup fallo con : " << iResult << endl; WSACleanup(); return -1; } cout << "WSAStartup.....ok" << endl; 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) { cout << "getAddrInfo fallo con: " << iResult << endl; WSACleanup(); return -1; } cout << "Get AdrrInfo....Ok" << endl; SOCKET ConnectSocket = INVALID_SOCKET; ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ConnectSocket == INVALID_SOCKET) { WSACleanup(); return 1; } cout << "Socket.....ok" << endl; iResult = connect(ConnectSocket, result->ai_addr, result->ai_addrlen); if (iResult) cout << "Error" << endl; else cout << "Se conecto" << endl; char msj[100] = ""; int msjlen = 0; while (true) { if (NonBlockGetLine(msj)) { msjlen = strlen(msj); send(ConnectSocket, msj, msjlen, 0); } char MsjRecv1[100] = ""; char MsjRecv2[100] = ""; if (NonBlock_Recv(ConnectSocket, MsjRecv1)) { cout << endl << MsjRecv1 << endl; if (NonBlock_Recv(ConnectSocket, MsjRecv2)) { cout << MsjRecv2 << endl; } } } system("Pause"); return 0; } bool NonBlockGetLine(char* sz) { static int iCont = 0; if (_kbhit()) { sz[iCont] = _getche(); iCont++; if (sz[iCont - 1] == ENTER) { sz[iCont - 1] = 0; iCont = 0; return true; } } return false; } bool NonBlock_Recv(SOCKET s, char* szBuffer) { fd_set readSet; FD_ZERO(&readSet); FD_SET(s, &readSet); timeval time; time.tv_sec = 0; time.tv_usec = 0; if (select(1, &readSet, NULL, NULL, &time)) { recv(s, szBuffer, 128, 0); return true; } return false; }