int CcsServer_recvRequest(CcsImplHeader *hdr,void **reqData) { char ip_str[200]; skt_ip_t ip; unsigned int port,ret=1; SOCKET fd; skt_abortFn old=skt_set_abort(req_abortFn); CCSDBG(("CCS Receiving connection...\n")); fd=skt_accept(ccs_server_fd,&ip,&port); CCSDBG(("CCS Connected to IP=%s, port=%d...\n",skt_print_ip(ip_str,ip),port)); hdr->attr.ip=ip; hdr->attr.port=ChMessageInt_new(port); if (0==CcsServer_recvRequestData(fd,hdr,reqData)) { fprintf(stdout,"During CCS Client IP:port (%s:%d) processing.\n", skt_print_ip(ip_str,ip), port); skt_close(fd); ret=0; } CCSDBG(("CCS Got all %d data bytes for request.\n",reqBytes)); skt_set_abort(old); return ret; }
void on_connect(int fd, void *arg) { int afd; uint32_t ip; uint16_t port; struct rpcd *rpcd = (struct rpcd *)arg; afd = skt_accept(fd, &ip, &port); if (afd == -1) { loge("skt_accept failed: %d\n", errno); return; } rpc_connect_create(rpcd, afd, ip, port); }
// Server loop to handle incoming connections void TCPServer::loop(){ std::unique_lock<std::mutex> connectionLock(_mutex, std::defer_lock); while(!_dead){ skt_ip_t client_ip; // IP & port of other end of connection unsigned int client_port; int cSocket = skt_accept(_socket, &client_ip, &client_port); sockaddr_in address = skt_build_addr(client_ip, client_port); sockaddr client_addr = *((sockaddr *)(&address)); connectionLock.lock(); std::shared_ptr<TCPConnection> newCon = makeConnection(cSocket, client_addr); newCon -> start(); _connections[to_string(client_addr)] = newCon; connectionLock.unlock(); } }
int main() { unsigned int port=1234; SERVER_SOCKET serv=skt_server(&port); while (1) { /* be a server */ std::cout<<"Listening on port "<<port<<std::endl; skt_ip_t client_ip; unsigned int client_port=0; SOCKET s=skt_accept(serv,&client_ip,&client_port); std::cout<<"Client connected from "<<skt_print_ip(client_ip)<<":"<<client_port<<std::endl; bool intranet=false; if (client_ip.data[0]==127 || client_ip.data[0]==10) intranet=true; try { handle_client(s,intranet); } catch (std::runtime_error &e) { std::cout<<"Client error: "<<e.what()<<"\n"; } } }