Client Accepting strange problem. C++ Linux -
i'm making multi-threaded server , ran strange problem. when first client connects server (after run application), accept command returns ip address @ "0.0.0.0", next 1 , others after him addresses valid. im missing or doing wrong? im using gcc , code::blocks
pic if dont understand me (http://i53.tinypic.com/16az1vp.jpg)
libs are:
#include <sys/timeb.h> #include <sys/select.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <fcntl.h> #include <arpa/inet.h>
acceptation function , class header
void tcpthread::executelistner() { int clifd, cid; socklen_t laddr_len; char ip[inet_addrstrlen]; printf("[tcpthread] listner thread executed!\n"); while (1) { clifd = accept(listenfd, (struct sockaddr *) &cli, &laddr_len); // akceptujemy polaczenie nonblockingmode(clifd); // ustawiamy tryb nieblokujacy inet_ntop(af_inet, &(cli.sin_addr), ip, inet_addrstrlen); // pobieranie adresu ip printf("[tcpthread] new connection: %s\n", ip); pthread_mutex_lock(&new_connection_mutex); // blokujemy dostep danych cid = clients->add(); // dodajemy nowego klienta wstawiamy podstawowe dane clients->client[cid].sock = clifd; clients->client[cid].ip.assign(ip); pthread_mutex_unlock(&new_connection_mutex); // inne thready moga juz korzystac z danych } } void tcpthread::nonblockingmode(int sockfd) { int opts; opts = fcntl(sockfd, f_getfl); if(opts < 0) { perror("fcntl(f_getfl)\n"); exit(1); } opts = (opts | o_nonblock); if(fcntl(sockfd, f_setfl, opts) < 0) { perror("fcntl(f_setfl)\n"); exit(1); } }
header file:
#ifndef tcpparser_h_included #define tcpparser_h_included #include "includes.h" #include "clientclass.h" #include "netmsg.h" class tcpthread { public: tcpthread(); int startserver(); clientclass *clients; protected: int runlistner(); int runparser(); static void * entrypointlistner(void*); static void * entrypointparser(void*); virtual void executelistner(); virtual void executeparser(); void parsepacket(int clientid, pchar buffer[max_client_buffer], int length); void nonblockingmode(int sockfd); private: pthread_t threadid; int listenfd; struct sockaddr_in srv, cli; pthread_mutex_t new_connection_mutex; }; #endif // tcpparser_h_included
i tried
(char *)inet_ntoa(cli.sin_addr)
but not make diffrience.
Comments
Post a Comment