Udp_client_server_using多线程

时间:2013-07-07 09:14:31

标签: c++ network-programming

我使用多线程实现了基本的udp客户端服务器应用程序,但实际上线程没有调用...任何正文都可以帮助我..

class uu_FrameWorkSocket 
{

protected:
struct sockaddr_in ModuleMySocketAddress;
uu_int32 ModuleHostPortK;
uu_char* ModuleHostNameK;
uu_int32 ModuleSockFlagK;
uu_int32* ModuleSockOptValueP;

public:
uu_void uu_ModuleInitSocket(uu_int32,uu_int32);
uu_void uu_ModuleSetSocketOptions(uu_int32);
uu_void SetHostName(uu_char* );
uu_void SetPortNumber(uu_int32 );
uu_void uu_ModuleCloseSocket(uu_int32 );
uu_FrameWorkSocket(){

        ModuleHostPortK = 3232;
        ModuleHostNameK = (uu_char*) malloc(20);
        strcpy(ModuleHostNameK,"127.0.0.1");
    }
};

uu_void uu_FrameWorkSocket :: SetHostName(uu_char *psHostName){
ModuleHostNameK = (uu_char*) malloc(20);
strcpy(ModuleHostNameK,psHostName);
}

uu_void uu_FrameWorkSocket :: SetPortNumber(uu_int32 iPortNumber){
ModuleHostPortK=iPortNumber;
}

 uu_void uu_FrameWorkSocket :: uu_ModuleCloseSocket(uu_int32 iSocketfd){
close(iSocketfd);
}

 uu_void uu_FrameWorkSocket :: uu_ModuleInitSocket(uu_int32 iTypeFamily,uu_int32 iTypeProtocol){
if(iTypeFamily == UU_PROJECT_MODULE_IPV4)
{
if(iTypeProtocol == UU_PROJECT_MODULE_TCP)
ModuleSockFlagK = socket(AF_INET, SOCK_STREAM, 0);
else
ModuleSockFlagK = socket(AF_INET, SOCK_DGRAM, 0);
}
else
{
if(iTypeProtocol == UU_PROJECT_MODULE_TCP)
ModuleSockFlagK = socket(AF_INET6, SOCK_STREAM, 0);
else
ModuleSockFlagK = socket(AF_INET6, SOCK_DGRAM, 0);
}
if (ModuleSockFlagK == -1){
            printf("Error while initializing socket.\n");
            exit(0);
        }
 }

uu_void uu_FrameWorkSocket :: uu_ModuleSetSocketOptions(uu_int32 iTypeFamily){
ModuleSockOptValueP = (uu_int32*)malloc(sizeof(uu_int32));
        *ModuleSockOptValueP = 1;

        if( (setsockopt(ModuleSockFlagK, SOL_SOCKET, SO_REUSEADDR,    (uu_char*)ModuleSockOptValueP, sizeof(uu_int32)) == -1 ) ){
            printf("Error while setting socket options.\n");
            free(ModuleSockOptValueP);
            exit(0);
        }
        free(ModuleSockOptValueP);
        if(iTypeFamily == UU_PROJECT_MODULE_IPV4)
        ModuleMySocketAddress.sin_family = AF_INET ;
        else
        ModuleMySocketAddress.sin_family = AF_INET6 ;           
        ModuleMySocketAddress.sin_port = htons(ModuleHostPortK);
        memset(&(ModuleMySocketAddress.sin_zero), 0, 8);            
        ModuleMySocketAddress.sin_addr.s_addr = inet_addr(ModuleHostNameK);

}


class uu_ProjectModuleUdp : public uu_FrameWorkSocket
{
protected:
    socklen_t ModuleMySockAddrLength;
    uu_int32* ModuleSockConnFlag;
    sockaddr_in ModuleIpSockAddr;
    pthread_t ModuleThreadId;

public:

struct Request{
sockaddr_in ModuleClientSockAddr;
uu_int32 ModuleClientSocket;
};

uu_void ModuleUdpBind(){
        if( bind( ModuleSockFlagK, (sockaddr*)&ModuleMySocketAddress,    sizeof(ModuleMySocketAddress)) == -1 ){
            fprintf(stderr,"Error while binding to socket:%d\n",errno);
            exit(0);
        }
    }


uu_void ModuleUdpAccept(){
        ModuleMySockAddrLength = sizeof(sockaddr_in);
        uu_char Msg[100];
        uu_int32 N;
    struct Request ModuleRequestStructure;
        while(true){
                if(( N= recvfrom( ModuleSockFlagK,Msg,100,0, (sockaddr*)&ModuleIpSockAddr, &ModuleMySockAddrLength))!= -1){
                printf("==\nReceived connection from  %s\n",inet_ntoa(ModuleIpSockAddr.sin_addr));
                printf("Received message: %s \n",Msg);
                sendto( ModuleSockFlagK,Msg,N,0, (sockaddr*)&ModuleIpSockAddr, ModuleMySockAddrLength);
                 ModuleRequestStructure.ModuleClientSockAddr=ModuleIpSockAddr;
                 ModuleRequestStructure.ModuleClientSocket=ModuleSockFlagK;
                pthread_create(&ModuleThreadId,0,&ModuleUdpSocketHandler,  (uu_void*)&ModuleRequestStructure );
                pthread_detach(ModuleThreadId);
            }
            else{
                fprintf(stderr, "Error accepting... %d\n", errno);
            }
        }
    }

static uu_void* ModuleUdpSocketHandler(uu_void* ModuleRequestStruct){

        Request *ModuleRequest=(Request *)ModuleRequestStruct;
        uu_int32 iClientSocket=ModuleRequest->ModuleClientSocket;
        sockaddr_in ClientSockaddr=ModuleRequest->ModuleClientSockAddr;
    socklen_t ModuleMySockAddrLength = sizeof(sockaddr_in);
        uu_char acBuffer[1024];
        uu_int32 iBufferLen = 1024;
        uu_int32 iByteCount;

        while(true){
            memset(acBuffer, '\0', iBufferLen);
            if((iByteCount = recvfrom(iClientSocket,acBuffer,iBufferLen,0,(sockaddr*)&ClientSockaddr,&ModuleMySockAddrLength))== -1){
                fprintf(stderr, "Error receiving data %d\n", errno);
                exit(0);
            }
            printf("Received bytes: %d\nReceived string: %s \n", iByteCount, acBuffer);

            if((iByteCount = sendto(iClientSocket, acBuffer, strlen(acBuffer), 0,(sockaddr*)&ClientSockaddr,ModuleMySockAddrLength))== -1){
                fprintf(stderr, "Error sending data %d\n", errno);
                exit(0); 
            }

            printf("Sent bytes %d\n", iByteCount);
        }
    }


uu_void ModuleUdpAfterConn(){
        uu_char acBuffer[1024];
        uu_int32 iByteCount;
        uu_int32 iBufferLen=0;
        ModuleMySockAddrLength = sizeof(sockaddr_in);
        iBufferLen = 1024;

        while(true){
            memset(acBuffer, '\0', iBufferLen);

            printf("Enter some text to send to the server (press enter):\n");
            fgets(acBuffer, 1024, stdin);
            acBuffer[strlen(acBuffer)-1]='\0';

            if( (iByteCount=sendto(ModuleSockFlagK, acBuffer, strlen(acBuffer),0,(sockaddr*)&ModuleMySocketAddress,ModuleMySockAddrLength))== -1){
                fprintf(stderr, "Error sending data %d\n", errno);
                exit(0);
            }
            printf("Sent bytes %d\n", iByteCount);

            if((iByteCount = recvfrom(ModuleSockFlagK, acBuffer, iBufferLen, 0,NULL,NULL))== -1){
                fprintf(stderr, "Error receiving data %d\n", errno);
                exit(0);
            }
            printf("Recieved bytes: %d\nReceived string: \"%s\"\n", iByteCount, acBuffer);
        }
        close(ModuleSockFlagK);
    }

};

int main(int argc, char **argv){

if(strcmp(argv[1],"udp")==0){
if(strcmp(argv[2],"server")==0){
    printf("This is UDP Server");
    uu_ProjectModuleUdp server;
    server.SetPortNumber(6745);
    server.uu_ModuleInitSocket(UU_PROJECT_MODULE_IPV4,UU_PROJECT_MODULE_UDP);
    server.uu_ModuleSetSocketOptions(UU_PROJECT_MODULE_IPV4);
    server.ModuleUdpBind();
    server.ModuleUdpAccept();
 }
 else if(strcmp(argv[2],"client")==0){
    printf("This is UDP Client");
    uu_ProjectModuleUdp client;
    client.SetPortNumber(6745);
    client.uu_ModuleInitSocket(UU_PROJECT_MODULE_IPV4,UU_PROJECT_MODULE_UDP);
    client.uu_ModuleSetSocketOptions(UU_PROJECT_MODULE_IPV4);
    client.ModuleUdpAfterConn(); 
}
} 
else
    printf("Wrong/Missing Arguments\n");
return 0;
}

这里针对每个客户端必须调用一个线程,但它没有调用..

1 个答案:

答案 0 :(得分:0)

可能发生的事情是你的线程崩溃了。当您尝试访问ModuleRequest指针时,原因是未定义的行为,因为它指向ModuleUdpAccept内的局部变量。

ModuleUdpAccept返回时,该指针不再指向有效内存,并且您得到未定义的行为,这可能(或可能不会)导致线程崩溃。