c++ - (WinSocket) Spam on Disconnect -
i'm trying write server client/server chat program. problem if 1 time client disconnects, server starts spamming lastly message server. if reconnect, keeps spamming..i've tried googling, can't find anything. here's server code:
#include "masterserver.h" using namespace std; sockaddr_in server; int addrlen = sizeof(server); socket slisten; socket connect; socket* connections; int port = 444; int concounter = 0; char* recv = new char[256]; int serverthread(int id) { zeromemory(recv, sizeof(recv)); for(;;) { if(recv(connections[id],recv ,256 ,null)) { cout << recv << endl; } } } int initwinsock() { int retval = 0; wsadata wsa; word version = makeword(2,1); retval = wsastartup(version, &wsa); homecoming retval; } int main() { int retval = 0; retval = initwinsock(); if(retval != 0) { cout << "winsock start failure"; } connections = (socket*)calloc(64, sizeof(socket)); slisten = socket(af_inet, sock_stream, null); connect = socket(af_inet, sock_stream, null); cout << "\t\t----====modernbacon server====----"<< endl; cout << "what port run on [default 444]: "; cin >> port; system("cls"); server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = af_inet; server.sin_port = htons(port); bind(slisten, (sockaddr*)&server, sizeof(server)); //listening listen(slisten, 64); cout << "listening connections ip:"<<inet_ntoa(server.sin_addr);"port:"<<port<< endl; for(;;) { if(connect = accept(slisten, (sockaddr*)&server, &addrlen)) { connections[concounter] = connect; char* id = new char[64]; zeromemory(id, sizeof(id)); itoa(concounter, id, 10); send(connections[concounter], id, sizeof(id),null); concounter = concounter + 1; createthread(null,null, (lpthread_start_routine) serverthread, (lpvoid)(concounter -1), null, null); cout << "\t\tclient connected!\tid: " << concounter << endl; } } return(0); }
i have no clue whats going on...
there quite few problems code. seek more instead:
#include "masterserver.h" #include <string> using namespace std; static const int max_clients = 64; static const int max_recv = 256; struct clientinfo { int id; socket connection; char recv[max_recv]; }; clientinfo clients[max_clients]; dword winapi serverthread(lpvoid lpparam) { clientinfo *client = (clientinfo*) lpparam; char szid[64] = {0}; itoa(client->id, szid, 10); int retval = send(client->connection, szid, sizeof(szid), null); if (retval != sizeof(szid)) { cout << "client " << client->id << " send failure. error " << wsagetlasterror() << endl; } else { { retval = recv(client->connection, client->recv, max_recv, null); if (retval == socket_error) { cout << "client " << client->id << " recv failure. error " << wsagetlasterror() << endl; break; } if (retval == 0) { cout << "client " << client->id << " disconnected" << endl; break; } cout << "client " << client->id << ": " << string(client->recv, retval) << endl; } while (true); } closesocket(client->connection); client->connection = invalid_socket; homecoming 0; } int initwinsock() { wsadata wsa; homecoming wsastartup(makeword(2,1), &wsa); } int main() { sockaddr_in addr; int addrlen; socket slisten; socket sclient; int port = 444; int index; cout << "\t\t----====modernbacon server====----"<< endl; (int = 0; < max_clients; ++i) connections[i] = invalid_socket; int retval = initwinsock(); if (retval != 0) { cout << "winsock start failure. error " << retval << endl; homecoming 0; } slisten = socket(af_inet, sock_stream, null); if (slisten == invalid_socket) { cout << "winsock socket failure. error " << wsagetlasterror() << endl; homecoming 0; } cout << "what port run on [default 444]: "; cin >> port; system("cls"); zeromemory(&addr, sizeof(addr)); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_family = af_inet; addr.sin_port = htons(port); retval = bind(slisten, (sockaddr*)&addr, sizeof(addr)); if (retval != 0) { cout << "winsock bind failure. error " << wsagetlasterror() << endl; homecoming 0; } //listening retval = listen(slisten, max_clients); if (retval != 0) { cout << "winsock hear failure. error " << wsagetlasterror() << endl; homecoming 0; } cout << "listening connections. ip: " << inet_ntoa(addr.sin_addr) << ", port: " << port << endl; { addrlen = sizeof(addr); sclient = accept(slisten, (sockaddr*)&addr, &addrlen); if (sclient == invalid_socket) { cout << "winsock take failure. error " << wsagetlasterror() << endl; homecoming 0; } index = -1; (int = 0; < max_clients; ++i) { if (clients[i].connection == invalid_socket) { index = i; break; } } if (index == -1) { closesocket(sclient); cout << "\t\tclient connected. max clients exceeded!" << endl; continue; } clients[index].id = index + 1; clients[index].connection = sclient; createthread(null, null, &serverthread, &clients[index], null, null); cout << "\t\tclient connected!\tid: " << clients[index].id << endl; } while (true); homecoming 0; }
c++ sockets chat
No comments:
Post a Comment