tcp ip - C++ TCP/IP bind error and setsockopt erorr -
i have error since 3 days ago. tried knowledge, still didn't solved
first, setsockopt function has error. searched error can't find way of solution. think if setsockopt function solve, can solve problem.
all :
#define _crt_secure_no_warnings #include <iostream> #include <stdlib.h> #include <string.h> #include <conio.h> #include <winsock2.h> #include <winsock.h> #include <process.h> #pragma comment(lib, "ws2_32.lib") #define bufsize 1024 using namespace std; critical_section crit; void em(char* str) { cout << str << endl; getch(); exit(1); } typedef struct useraccount { int id; char name[bufsize]; }usraccnt; class rs { public: char ms[bufsize]; char name_t[bufsize] = {}; int caddrsize; timeval timeout; fd_set reads, temps; usraccnt* users; dword senthreadid; dword recthreadid; handle hsenthread; handle hrecthread; void runthread() { hsenthread = (handle)_beginthreadex(null, 0, __senthread, null, 0, (unsigned*)&senthreadid); hrecthread = (handle)_beginthreadex(null, 0, __recthread, null, 0, (unsigned*)&senthreadid); } protected: void (rs::*recthreadfunc)() = &rs::recthread; void (rs::*senthreadfunc)() = &rs::senthread; static unsigned int __stdcall __senthread(lpvoid arg) { rs sendata; (sendata.*sendata.senthreadfunc)(); return 0; } static unsigned int __stdcall __recthread(lpvoid arg) { rs recdata; (recdata.*recdata.recthreadfunc)(); return 0; } virtual void recthread() { cout << "call" << endl; } virtual void senthread() { cout << "call" << endl; } }; int main() { rs data; wsadata data; socket csock; socket ssock; sockaddr_in saddr; sockaddr_in caddr; linger ling; ling.l_onoff = 1; ling.l_linger = 0; void* bf = (int*)1; int stat; int result; if (wsastartup(makeword(2, 2), &data) != 0) { em("wsastartup error"); } if (ssock = socket(pf_inet, sock_stream, 0) == invalid_socket) { em("socket error"); } stat = setsockopt(ssock, sol_socket, so_reuseaddr, (const char*)&bf, sizeof(bf)); cout << stat << endl; //cout << (const char*)bf << endl; memset(&saddr, 0x00, sizeof(saddr)); saddr.sin_family = af_inet; saddr.sin_addr.s_un.s_addr = htonl(inaddr_any); saddr.sin_port = htons(atoi("25565")); if (bind(ssock, (sockaddr*)&saddr, sizeof(saddr)) == socket_error) { em("bind error"); } if (listen(ssock, 5) == socket_error) { em("listen error"); } cout << "listen..." << endl << endl; fd_zero(&data.reads); fd_set(0, &data.reads); while (true) { data.temps = data.reads; data.timeout.tv_usec = 0; data.timeout.tv_sec = 5; if ((result = select(0, &data.temps, 0, 0, &data.timeout)) == -1) { em("select error"); } else if (result == 0) { em("timeout."); } (int = 0; < data.reads.fd_count; i++) { if (fd_isset(data.reads.fd_array[i], &data.temps)) { if (data.reads.fd_array[i] == ssock) { data.caddrsize = sizeof(caddr); data.reads.fd_array[i] = accept(ssock, (sockaddr*)&caddr, &data.caddrsize); fd_set(data.reads.fd_array[i], &data.reads); cout << "client joined server. id :" << data.reads.fd_array[i] << endl << endl; } else { cout << "call" << endl; } } } } getch(); return 0; }
Comments
Post a Comment