Salve,
stavo cercando di creare un server multiclient per studio ma sono incappato in un problema
Quando connetto anche un solo client al server,questo comincia a stampare "byte recv -1" all'infinito.
Incollo il source in c++/cli
void main()
{
printf("############################## SERVER #####################################\n\n");
temp = WSAStartup(MAKEWORD(2,2),&data);
if(temp != NO_ERROR)
{
printf("Error in WSAStartup");
system("PAUSE");
}
ZeroMemory(&hints,sizeof(hints));
hints.ai_family= AF_INET;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_socktype = SOCK_STREAM;
sockAdd = &hints;
ZeroMemory(&collection_sock,sizeof(collection_sock));
for (int i = 0; i < 30; i++)
{
collection_sock[i] = 0;
}
temp = getaddrinfo(NULL,DEFAULT_PORT,&hints,&sockAdd);
if (temp < 0 )
{
printf("Getaddrinfo error");
system("PAUSE");
}
master_sock = socket(sockAdd->ai_family,sockAdd->ai_socktype,sockAdd->ai_protocol);
if (master_sock == INVALID_SOCKET)
{
printf("Error in creating socket");
system("PAUSE");
}
if (setsockopt (master_sock,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val)) == SOCKET_ERROR)
{
printf("error with setsockopt func");
system("PAUSE");
}
temp = bind(master_sock,sockAdd->ai_addr,sockAdd->ai_addrlen);
if (temp == SOCKET_ERROR)
{
printf("Error in bind %d\n",WSAGetLastError());
system("PAUSE");
}
temp = listen(master_sock,3);
if (temp < 0)
{
printf("Error with listen func");
system("PAUSE");
}
SOCKET client;
int activity = NULL;
while (true)
{
FD_ZERO(&fds);
FD_SET(master_sock,&fds);
for(int i = 0; i < 30; i++)
{
if(collection_sock[i] > 0)
FD_SET(collection_sock[i],&fds);
}
activity = select(master_sock + 1,&fds,NULL,NULL,0);
if (FD_ISSET(master_sock,&fds))
{
client = accept(master_sock,(sockaddr *)&addInfo,(int *)sizeof(addInfo));
FD_SET(client,&fds);
do
{
sendResult = recv(client,recvBuffer,sizeof(recvBuffer),NULL);
printf("byte recv %d\n",sendResult);
}while(sendResult > 0);
for(int i = 0; i < 30; i++)
{
if(collection_sock[i] = 0)
collection_sock[i] = client;
break;
}
}
}
}
Credo di aver fatto un casino con le varie macro di select()....
Ho già chiesto ad altre persone ma nessuno mi ha saputo o avuto tempo di darmi una risposta esaustiva...
Grazie in anticipo a tutti quanti risponderanno.