Problems with a chat application

Discussion in 'C' started by Light86, Mar 13, 2009.

  1. Light86

    Light86 New Member

    Joined:
    Mar 13, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi everybody!I'm trying to make a simple p2p chat using winsock and winthreads but I can't get it to work:it seems to block on receive,receiving bytes even when socket is empty leading to a receive error!I list you my project files:

    class.h
    Code:
    [B][B]#include <winsock2.h>
    #include <iostream>
    
    using namespace std;
    
    class chatsock{
          public:
                 chatsock();
                 chatsock(int port1);
                 ~chatsock();
                 void connection(char address[],int port1);
                 void chat();
                 void listening();
                 void receiving();
                 SOCKET getsock();
                 
          private:
                SOCKET sock,guestsock;
                WORD p1;
                WSADATA data1;
                sockaddr_in internal_address;
                sockaddr_in destination_address;
    [/B][/B][B][B][B][B]            sockaddr_in guest_address;[/B][/B][/B][/B]
    
    [B][B]            char buf[300];
                char recbuf[300];
                int n;
    };
    [/B][/B]
    class.cpp

    Code:
    #include "class.h"
    chatsock::chatsock(){
                         p1=MAKEWORD(2,2);
                                   if(WSAStartup(p1,&data1)!=0){
                                   cerr<<"WSA failed\n";
                                   exit(1);
                                   }
                                   
                 if((sock=socket(PF_INET,SOCK_STREAM,0))<0){
                 cerr<<"Failed creating socket\n";
                 exit(1);
                 }
                 
                 internal_address.sin_family=AF_INET;
                 internal_address.sin_port=htons((u_short)1234);
                 internal_address.sin_addr.s_addr=htonl(INADDR_ANY);
        
                 if(bind(sock,(struct sockaddr*)&internal_address,sizeof(internal_address))<0){
                 cerr<<"Bind failed\n";
                 exit(1);
                 }
    }
                         
    chatsock::chatsock(int port1){
                              
                                   p1=MAKEWORD(2,2);
                                   if(WSAStartup(p1,&data1)!=0){
                                   cerr<<"WSA failed\n";
                                   exit(1);
                                   }
                                   
                 if((sock=socket(PF_INET,SOCK_STREAM,0))<0){
                 cerr<<"Failed creating socket\n";
                 exit(1);
                 }
                 
                 
                 internal_address.sin_family=AF_INET;
                 internal_address.sin_port=htons((u_short)port1);
                 internal_address.sin_addr.s_addr=htonl(INADDR_ANY);
        
                 if(bind(sock,(struct sockaddr*)&indirizzo_interno,sizeof(indirizzo_interno))<0){
                 cerr<<"Bind failed\n";
                 exit(1);
                 }
    }
    
    void chatsock::connection(char address[],int port1){
        destination_address.sin_family=AF_INET;
        struct hostent* punthost;
        punthost=gethostbyname(address);
        destination_address.sin_addr.s_addr=*((unsigned long*)punthost->h_addr); 
        destination_address.sin_port=htons((u_short)port1);
        
        if(connect(sock,(struct sockaddr*)&destination_address,sizeof(destination_address))<0){
         cerr<<"Connect failed\n";
         exit(1);
        }
    }
    
    void chatsock::chat(){
         do{
        cout<<"Type>";
        gets(buf);
        send(sock,buf,sizeof(buf),0);
        }while(strcmp(buf,"quit")!=0);
         
    }
    
    void chatsock::listening(){
         if(listen(sock,1)<0){
         cerr<<"Listen failed\n";
         exit(1);
         }
         
         cout<<"Listening...\n";
         
        int alen=sizeof(guest_address);
        if((guestsock=accept(sock,(struct sockaddr*)&guest_address,&alen))<0){
         cerr<<"Accept fallito\n";
         exit(1);
         }
        cout<<"Someone has just connected\n";
        
    }
    
    void chatsock::receiving(){
         
         while(1){
                 if(n=recv(sock,recbuf,sizeof(recbuf),0)<0){
                 cerr<<"Receiving error\n";
                 exit(1);
                 }
                 
                 if(strcmp(recbuf,"quit")==0){
                 cout<<"Client closed connection\n";
                 break;
                 }
                 else
                 cout<<"Received: "<<recbuf<<endl;
                 }
    }
    
    SOCKET chatsock::getsock(){
           return sock;
    }
    
    chatsock::~chatsock(){
                          shutdown(sock,0);
                          closesocket(sock);
                          WSACleanup();
                          }
    main

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <process.h>
    #include "class.h"
    
    using namespace std;
    
    DWORD WINAPI recthread(LPVOID lpparam);
    
    int main(int argc, char *argv[])
    {
        HANDLE threads;
        int choose;
        char IP[20];
        int port;
        chatsock* c1;
        
        if(argc>1)
        c1=new chatsock(atoi(argv[1]));
        else
        c1=new chatsock();
        
        cout<<"Connect(1) or listen(2)?\n";
        do{
        cout<<">";
        cin>>choose;
        switch(choose){
                       case 1:
                            cout<<"Type destination IP \n>";
                            cin>>IP;
                            cout<<"\nType port\n>";
                            cin>>port;
                            c1->connessione(IP,port);
                            threads=CreateThread(NULL,0,recthread,(void*)(c1),0,NULL);
                            c1->chat();
                            break;
                       case 2:
                            c1->listening();
                            threads=CreateThread(NULL,0,recthread,(void*)(c1),0,NULL);
             
                            c1->chat();
                            break;
                       default:
                            cout<<"Command not accepted\n";
                       }
        }while((scelta!=1)&&(scelta!=2));
        
        
        
        
        delete c1;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    DWORD WINAPI recthread(LPVOID lpparam){
          chatsock* ricezione=(chatsock*)lpparam;
          ricezione->ricezione();
          
          return 0;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Why do both case 1 and case 2 call c1->chat()?
     
  3. Light86

    Light86 New Member

    Joined:
    Mar 13, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,because in both cases it's necessary to send bytes too.
    The difference is that in case 1 the client starts the connectione,while in 2 the client waits for a connection.
     
  4. Light86

    Light86 New Member

    Joined:
    Mar 13, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Anybody??T_T
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice