c++ - How can I Always open server? -
i have code (my c++ socket server) don't know how can open. server close. when send client close itself. want wait others client , never closed. how can ? oh use multi-threaded too.
please,help me.
and here code
int main(void) { handle hthread[3]; dword dwid[3]; dword dwretval = 0; hthread[0] = createthread(null,0,(lpthread_start_routine)threadtwo,null,0,&dwid[0]); dwretval = waitformultipleobjects(3, hthread, true, infinite); closehandle(hthread[0]); return 0; } long winapi threadtwo(long lparam) { wsadata wsadata; socket listensocket = invalid_socket, clientsocket = invalid_socket; struct addrinfo *result = null, hints; char recvbuf[default_buflen]; int iresult, isendresult; int recvbuflen = default_buflen; iresult = wsastartup(makeword(2,2), &wsadata); if (iresult != 0) { printf("wsastartup failed error: %d\n", iresult); return 1; } 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 failed error: %d\n", iresult); wsacleanup(); return 1; } listensocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (listensocket == invalid_socket) { printf("socket failed error: %ld\n", wsagetlasterror()); freeaddrinfo(result); wsacleanup(); return 1; } iresult = bind( listensocket, result->ai_addr, (int)result->ai_addrlen); if (iresult == socket_error) { printf("bind failed error: %d\n", wsagetlasterror()); freeaddrinfo(result); closesocket(listensocket); wsacleanup(); return 1; } freeaddrinfo(result); iresult = listen(listensocket, somaxconn); if (iresult == socket_error) { printf("listen failed error: %d\n", wsagetlasterror()); closesocket(listensocket); wsacleanup(); return 1; } printf("waiting client\n"); clientsocket = accept(listensocket, null, null); if (clientsocket == invalid_socket) { printf("accept failed error: %d\n", wsagetlasterror()); closesocket(listensocket); wsacleanup(); return 1; } printf("client acctped.\n"); closesocket(listensocket); { iresult = recv(clientsocket, recvbuf, recvbuflen, 0); if (iresult > 0) { printf("bytes received: %d\n", iresult); recvbuf[iresult] = '\0'; printf(recvbuf); char* testsend = "222 333\n444 555\n666 777" ; isendresult = send( clientsocket, testsend , strlen(testsend)+1 , 0 ); if (isendresult == socket_error) { printf("send failed error: %d\n", wsagetlasterror()); closesocket(clientsocket); wsacleanup(); return 1; } printf("bytes sent: %d\n", isendresult); } else if (iresult == 0) printf("connection closing...\n"); else { printf("recv failed error: %d\n", wsagetlasterror()); closesocket(clientsocket); wsacleanup(); return 1; } } while (iresult > 0); iresult = shutdown(clientsocket, sd_send); if (iresult == socket_error) { printf("shutdown failed error: %d\n", wsagetlasterror()); closesocket(clientsocket); wsacleanup(); return 1; } system("pause"); closesocket(clientsocket); wsacleanup(); return 0; }
thank again.
you have use loop :)
basically, server main code must in loop, go forever ... while not stop it.
you should have :
// that's close pseudo code :) while(notstoppedbymaster) { // [...] clientsocket = accept(listensocket, null, null); handlerequest(clientsocket); } // close listen socket here
the handlerequest
should handle request in thread. be attentive synchronization :)
my2c
Comments
Post a Comment