Salve,
ho sviluppato un'applicazione client server in C++ che deve funzionare nel seguente modo:l'applicazione server accetta
due client e dopo un certo numero di tempo chiude la connessione con un dato client. Il problema consiste nella chiusura della connessione, perchè se utilizzo la funzione closesocket sul socket temporaneo che creo per consentire al socket di accettare altre richieste, sia se effettuo la chiusura del socket temporaneo, che lo shutdown di tale socket il server termina l'esecuzione, mentre voglio che il server
deve rimanere sempre attivo in attesa di richiesta di altri
client.
Invio il codice delle due applicazioni, e vi chiedo gentilmente un aiuto e se potevo inviarmi qualche altro link di spiegazioni ed esempi che parlano dei socket su come chiudere la connessione con il client senza terminare l'esecuzione soprattutto se il server è multithread.
Applicazione client.
#include "stdafx.h"
#include <winsock2.h>
#include "process.h"
#using <mscorlib.dll>
using namespace System;
SOCKET sock=NULL;
SOCKADDR addr;
void MsgProc();
int _tmain()
{
WSADATA dat;
WORD ver=MAKEWORD(1,1);
int res;
SOCKADDR_IN addr_in;
addr_in.sin_family=AF_INET;
addr_in.sin_port=100;
addr_in.sin_addr.S_un.S_un_b.s_b1=127;
addr_in.sin_addr.S_un.S_un_b.s_b2=0;
addr_in.sin_addr.S_un.S_un_b.s_b3=0;
addr_in.sin_addr.S_un.S_un_b.s_b4=1;
memcpy(&addr,&addr_in,sizeof(SOCKADDR_IN));
//auto-start server
//system("start ..\\socketserver\\debug\\socketserver.exe");
printf("Remember, 0s are good\n");
printf("WSAStartup: %d. Version %d\n",(res=WSAStartup(ver,&dat)),ver);
if(res!=0)
return 0;
sock=socket(PF_INET,SOCK_STREAM,0);
printf("Connect: %d\n",(res=connect(sock,&addr,sizeof(addr))));
if(sock==NULL||res!=0)
return 0;
Console::WriteLine("Avvio connessione");
MsgProc();
WSACleanup();
return 0;
}
void MsgProc()
{
char str[100]="";
int res;
while(stricmp(str,"exit")!=0){
//printf("String to send: ");
//gets(str);
send(sock,str,100,0);
for(int i=0;(res=recv(sock,str,100,0))==SOCKET_ERROR&&i<50 ;i++)
Sleep(100);
if(res==0||res==WSAECONNRESET||res==WSAECONNABORTE D)
{
printf("Server terminated connection\n");
break;
}
}
closesocket(sock);
}
Applicazione server
#include "stdafx.h"
#include <winsock2.h>
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
SOCKET sock;
SOCKADDR addr;
public __gc class ThreadExample
{
public:
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
static void ThreadProc()
{
/*for (int i = 0; i < 30; i++)
{
Console::Write("ThreadProc: ");
Console::WriteLine(i);
// Yield the rest of the time slice.
Thread::Sleep(10000);
}
closesocket(sock);
*/
}
};
void MsgProc();
int _tmain()
{
SOCKADDR_IN addr_in;
addr_in.sin_family=AF_INET;
addr_in.sin_port=100;
addr_in.sin_addr.S_un.S_un_b.s_b1=127;
addr_in.sin_addr.S_un.S_un_b.s_b2=0;
addr_in.sin_addr.S_un.S_un_b.s_b3=0;
addr_in.sin_addr.S_un.S_un_b.s_b4=1;
memcpy(&addr,&addr_in,sizeof(SOCKADDR_IN));
SOCKET temp=NULL;
WSADATA dat;
WORD ver=MAKEWORD(1,1);
int res;
printf("WSAStartup: %d Version %d\n",(res=WSAStartup(ver,&dat)),ver);
if(res!=0)
return 0;
sock=socket(PF_INET,SOCK_STREAM,0);
printf("Binding socket: %d\n",bind(sock,&addr,sizeof(addr)));
printf("Listen mode: %d\n",listen(sock,2));
while((temp=accept(sock,NULL,NULL))==SOCKET_ERROR) ;
Console::WriteLine("Benvenuto sul server");
//sock=temp;
Thread *oThread = new Thread(new ThreadStart(0, &ThreadExample::ThreadProc));
// Start the thread. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
oThread->Start();
//Thread::Sleep(0);
for (int i=0;i<30;i++)
{ printf("%d\n",i);
oThread->Sleep(10000);
}
Console::WriteLine("Il server sta per chiudere la connessione");
closesocket(temp);
if(sock)
MsgProc();
closesocket(sock);
WSACleanup();
}
void MsgProc()
{
char buf[100];
int res=1;
while((res=recv(sock,buf,100,0))!=SOCKET_ERROR)
{
if(res==0||res==WSAECONNRESET||res==WSAECONNABORTE D)
{
printf("Client terminated connection\n");
break;
}
_flushall();//probably not needed
//printf("Recieved string: %s\n",buf);
send(sock,buf,100,0);
}
closesocket(sock);
}
In attesa di un gentile riscontro, porgo distinti saluti.