Thread: Socket Overflow by sending / receiving ACK's

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    45

    Socket Overflow by sending / receiving ACK's

    Hello!

    Is it possible that my socket gets overflowed when he receives ACK's but not handles them?

    I send an ACK:

    Code:
    guint32 pos = htonl (count & 0xffffffff); 
    send(sockfd,(char*) &pos,4,0);
    Everytime I receive a part of a file. So this is like constant ACK & Receive file.

    It only happends when I do this with 2 of my self written clients.


    Kind regards

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Are these raw sockets, or tcp/ip sockets? If the later, are you sure doing this makes sense? The point of a C library tcp/ip socket is that all that is taken care of. You don't have to deal with the tcp or ip layers. That would be absurdly tedious.

    Ie, your "ACK" would be encapsulated, and hence not be received as an ACK at all. It would just be a tcp/ip packet with 4 bytes in it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by MK27 View Post
    Are these raw sockets, or tcp/ip sockets? If the later, are you sure doing this makes sense? The point of a C library tcp/ip socket is that all that is taken care of. You don't have to deal with the tcp or ip layers. That would be absurdly tedious.

    Ie, your "ACK" would be encapsulated, and hence not be received as an ACK at all. It would just be a tcp/ip packet with 4 bytes in it.
    Normally this is all handled in the application, if I look at the RFC of IRC it says:

    The recipient should acknowledge each packet by transmitting the total number of bytes received as an unsigned, 4 byte integer in network byte order.
    So that is what I am doing, it's the same code that is used in xchat.

    Yes mine are TCP/IP Sockets. Pff all is working fine with XCHat but now it doesn't work between 2 of my own applications

    Kind regards
    Last edited by Salem; 11-23-2011 at 12:37 PM. Reason: tags fixing

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    This line:
    Code:
    send(sockfd,(char*) &pos,4,0);
    Makes XChat file transfer work 100%

    But makes my own application crash.

    Why??

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you plan on sending your own ACKs, you need to open your socket in SOCK_RAW mode, not SOCK_STREAM (TCP) mode. Is that what you're doing? Are you sure that's what you want?

    It's a little hard for us to diagnose your problem with just 2 lines of code. There is nothing there that is obviously the problem. Could you post the full code, or at least all the relevant sections?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The ACK you're looking at is at your protocol level, not at the network level.

    You also need to post more information than "it crashes".
    Random lines of code doesn't work.

    Compile the whole program with the -g flag, then run both programs in gdb.

    When it crashes, start by typing in 'bt' and see exactly where it crashed.
    From there, use gdb to print variables, so you can see what they really contain.

    If you're still stuck, post your gdb transcript.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by Salem View Post
    The ACK you're looking at is at your protocol level, not at the network level.

    You also need to post more information than "it crashes".
    Random lines of code doesn't work.

    Compile the whole program with the -g flag, then run both programs in gdb.

    When it crashes, start by typing in 'bt' and see exactly where it crashed.
    From there, use gdb to print variables, so you can see what they really contain.

    If you're still stuck, post your gdb transcript.
    Is that possible in Visual Studio?

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    This is my receive code:

    Code:
        struct sockaddr_storage their_addr;
        struct addrinfo hints, *servinfo, *p;
        struct in_addr addr;
        socklen_t addr_size;
    
        int  sockfd ,bindfd , listenfd;
        char buffer[1024], filePath[50];
        char s[INET6_ADDRSTRLEN], *dotted_quad;
        int numbytes=0;
        unsigned long long int count =0;
        bool bolRecvFile=false;
    
        FILE *fp;
    
        //File Location
        filePath[0] = '\0';
        strcat(filePath,"C:\\");
        strcat(filePath,file);
        strcat(filePath,"\0");
    
        //Socket Stuff
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
            Log("WSAStartup","WSAStartup failed");
            closesocket(1);
        }
    
        //Socket Stuff
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever
        hints.ai_socktype = SOCK_STREAM; //TCP
        hints.ai_flags = AI_PASSIVE;     // fill in my IP for me
    
        //Convert Decimal Notation to Dotted Decimal Notation
        unsigned long numeric_ip = (unsigned) strtoul(ip,NULL,10);
        numeric_ip = htonl(numeric_ip);  // flip the byte order
        memcpy(&addr, &numeric_ip, sizeof(numeric_ip));
        dotted_quad = inet_ntoa(addr);
    
        getaddrinfo(dotted_quad, port, &hints, &servinfo);
    
        for(p = servinfo; p != NULL; p = p->ai_next) {
            if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
                //wprintf(L"### Socket failed with error: %ld\n\n", WSAGetLastError());
                Log("Socket","Sockted failed with error");
            }
    
            if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
                //wprintf(L"\n### Connect failed with error: %ld\n\n", WSAGetLastError());
                Log("Connect","Connect failed with error");
                closesocket(sockfd);
                continue;
            }
            break;
        }
    
        if (p == NULL) {
            printf("Failed to connect\n");
        }
    
        if((fp=fopen(filePath, "wb"))==NULL) {
            printf(":Cannot open file, please pick another.\n");     
            Log("DCC File Receive","Cannot open file, please pick another.");     
            return;
        }
        //here
    
        //printf("%d",filesize);
        int bytes_sent=0;
        do {
            //Receiving From Socket
            numbytes = recv(sockfd, buffer, sizeof(buffer), 0);
    
            if(numbytes>4)
            {
                //Counting Bytes Received
                count = count + numbytes;
                if (bolRecvFile == false)
                {
                    printf("\n:>Receiving File\n", file);
                    bolRecvFile = true;
                }
    
                //Writing to file
                fwrite(buffer, numbytes, 1, fp);
                //printf("\n\nCount: %d Filesize: %d",count,filesize);
    
                //Sending ACK's
                if(count > 4566695)
                {
                    guint32 pos = htonl (count & 0xffffffff); 
                    send(sockfd,(char*) &pos,4,0); 
                }
    
            }
            /*    else
            {
            break;
            printf("aj aj");
            }*/
            //printf("%d\n",bytes_sent);
    
        }while(count < filesize);

    This is my send code:
    Code:
    //DCC file sending;
    void listenSocket (void *arg,void *inputUser,void *inputFileName,void *inputFileLoc)
    {
        struct sockaddr_storage their_addr;
        struct addrinfo hints, *servinfo;
        struct in_addr addr;
        socklen_t addr_size;
    
        int len, bytes_sent, bytes_received, fdmax, new_fd, nbytes, j, i,myRandPort=0;
        int sockfdIRCSocket = (int)arg,    sockfd ,bindfd , listenfd;
        unsigned long long int countSentBytes=0;
    
        char *sendToUser = (char*)inputUser, *fileName = (char*)inputFileName, *fileLoc = (char*)inputFileLoc;
        char strSizeFile[50], buffer[5120], hostip[30], read[500], sendDCC[500], decResult[50], hostname[80], *pch;
        char buf[500], remoteIP[INET6_ADDRSTRLEN], MYPORT[6]="\0",bufferke[4];  
    
        long sizeFile;
    
        bool bolSendFile = false;
    
        fd_set master;    // master file descriptor list
        fd_set read_fds;
    
        strSizeFile[0] = '\0';
        decResult[0]= '\0';
        hostip[0]= '\0';
    
        srand(time(NULL));
        myRandPort=getRand(49152,65535);
        sprintf(MYPORT,"%d",myRandPort);
    
        //Socket Stuff
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
            Log("WSAStartup","WSAStartup failed");
            closesocket(1);
        }
    
        //File thats needs to be sended
        FILE *fp;
        //Read + Open File
        if((fp = fopen(fileLoc, "rb+"))==NULL) {
            printf(":Cannot open file, please pick another.\n");     
            Log("DCC File Send","Cannot open file, please pick another.");     
            return;
        }
    
        //Get Filesize
        fseek(fp, 0,SEEK_END);
        sizeFile = ftell(fp);
        ltoa(sizeFile,strSizeFile,10);
        fseek(fp, 0,SEEK_SET);
    
        //GET IP
        if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR) {
            Log("DCC Send Host","No Host Found");
        }
    
        struct hostent *phe = gethostbyname(hostname);
        memcpy(&addr, phe->h_addr_list[1], sizeof(struct in_addr));
        sprintf(hostip,"%s",inet_ntoa(addr));
    
        //Convert IP to Decimal notation
        sprintf(decResult,"%u", addr);
        sprintf(decResult,"%u", htonl(atoi(decResult)));
    
        // \001DCC SEND test.txt <decimal> 9999 70\001
        sendDCC[0] = '\0';     
        strcat(sendDCC,"PRIVMSG ");
        strcat(sendDCC, sendToUser);
        strcat(sendDCC," :\001DCC SEND ");
        strcat(sendDCC, fileName);
        strcat(sendDCC," ");
        strcat(sendDCC, decResult);
        strcat(sendDCC," ");
        strcat(sendDCC, MYPORT);
        strcat(sendDCC, " ");
        strcat(sendDCC, strSizeFile);
        strcat(sendDCC, "\001\n");
    
        //Socket Stuff
        memset(&hints, 0, sizeof hints);
        hints.ai_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever
        hints.ai_socktype = SOCK_STREAM; //TCP
        hints.ai_flags = AI_PASSIVE;     // fill in my IP for me
    
        //AddrInfo
        getaddrinfo(hostip,MYPORT, &hints, &servinfo);
    
        //Create a socket
        sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
        if(sockfd > 0)
        {
            //printf("\n:Socket(%i): OK\n\n",sockfd);
            Log("DCC Send Create Socket","Sockted created");
        }
        else
        {
            //wprintf(L"### Bind failed with error: %ld\n\n", WSAGetLastError());
            Log("DCC Send Create Socket","Sockted failed");
        }
    
        //Bind
        bindfd = bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);    
        if(bindfd == 0)
        {
            //printf(":Bind: OK\n\n",bindfd);
            Log("DCC Send Bind","Bind ok");
        }
        else
        {
            //wprintf(L"### Bind failed with error: %ld\n\n", WSAGetLastError());
            Log("DCC Send Bind","Bind failed");
        }
    
        //Listen
        for(;;){
    
            listenfd = listen(sockfd, 10);
            if(listenfd == 0)
            {
                //printf(":Listening on port %s...\n\n",MYPORT);
                Log("Port listening",MYPORT);
            }
            else
            {
                //wprintf(L"### Listen failed with error: %ld\n\n", WSAGetLastError());
                Log("DCC Send Listen","Listen Failed");
            }
    
            FD_ZERO(&master);    
            FD_ZERO(&read_fds);
            FD_SET(sockfd, &master);
            fdmax = sockfd; 
    
            //sendToServer("PRIVMSG bigvince :\001DCC SEND test.txt 127.0.0.1 9999 70\001\n",(void *)sockfdIRCSocket);
            sendToServer(sendDCC,(void *)sockfdIRCSocket);
            int bytes_rex = 0;
            // main loop
            for(;;) {
                read_fds = master; // copy it
                if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
                    //wprintf(L"### Select failed with error: %ld\n\n", WSAGetLastError());
                    Log("DCC Send Select","Select failed");
                    closesocket(4);
                }
    
                // run through the existing connections looking for data to read
                for(i = 0; i <= fdmax; i++) {
                    if (FD_ISSET(i, &read_fds)) { // we got one!!
                        if (i == sockfd) {
    
                            // handle new connections
                            addr_size = sizeof their_addr;
                            new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&addr_size);
                            //printf("accept");
    
                            if (new_fd == -1) {
                                //wprintf(L"### Accept failed with error: %ld\n\n", WSAGetLastError());
                                Log("DCC Send Accept","Accept failed");
                            } 
                            else 
                            {
                                Log("DCC Send Accept","Accept all good");
    
                                FD_SET(new_fd, &master); // add to master set
                                if (new_fd > fdmax) 
                                {    // keep track of the max
                                    fdmax = new_fd;
                                }
    
                                //printf(":New connection from %s on socket %d\n\n", inet_ntop(their_addr.ss_family,&their_addr,remoteIP, INET6_ADDRSTRLEN),new_fd);
                                Log("DCC Send New Connection", (char *)inet_ntop(their_addr.ss_family,&their_addr,remoteIP, INET6_ADDRSTRLEN));
    
                                if (atoi(strSizeFile)<sizeof(buffer))
                                {
                                    //Read file into buffer
                                    len = fread(&buffer,atoi(strSizeFile),1,fp);
    
                                    //Send buffer into network
                                    bytes_sent = send(new_fd, buffer, atoi(strSizeFile), 0);
                                    if (bytes_sent > 0)
                                    {
                                        printf("\n:>Sending file\n");
                                    }
                                    else if (bytes_sent == -1)
                                    {
                                        wprintf(L"### Send failed with error: %ld\n\n", WSAGetLastError());
                                        //Log("DCC Send Filebytes","Send failed");
                                        fclose(fp);
                                        return;
                                    }
                                }
                                else
                                { 
                                    do {
                                        //Read file into buffer
                                        fread(&buffer,sizeof(buffer),1,fp);
    
                                        //Send buffer into network
                                        bytes_sent = send(new_fd, buffer, sizeof(buffer), 0);
                                        if (bytes_sent > 0)
                                        {
                                        //    do
                                        //    {
                                        //    //Count totall send bytes
                                        //        bytes_rex = recv(new_fd, bufferke, sizeof(bufferke), 0);
                                        //    if(bytes_rex > 0)
                                        //    {
                                                countSentBytes = countSentBytes + bytes_sent;
                                                if (bolSendFile == false)
                                                {
                                                    printf("\n:>Sending file\n");
                                                    bolSendFile = true;
                                                }
                                                printf(".");
                                            /*    break;
                                            }
                                            }while(bytes_rex != 4);*/
                                        }
                                        else if (bytes_sent == -1)
                                        {
                                            wprintf(L"### Send failed with error: %ld\n\n", WSAGetLastError());
                                            //Log("DCC Send Filebytes","Send failed");
                                            fclose(fp);
                                            return;
                                        }
                                        else
                                        {
                                            printf("lala");
                                        }
                                         //
                                        //printf("%d",bytes_rex);
                                        
                                    }while(countSentBytes < atoi(strSizeFile));
    
                                 
                                        
                                     
                                }
                                if(countSentBytes = atoi(strSizeFile) )
                                {
                                    printf("\n:>File transfer complete \n\n");
                                    bolSendFile = false;
                                }
                                fclose(fp);
                            }
                        }
                    }
                }
            }
        }
    }



    The reason why I have that code :
    Code:
    send(sockfd,(char*) &pos,4,0);
    Is the following: If I comment that code my files transfer perfectly via XChat and via my Application but in XChat the Procent that the application is finished doesn't go up it stays at 0%.

    When I include the code above the % goes up and says done at 100% like this:

    Socket Overflow by sending / receiving ACK's-untitled-png


    When I include the code above my applications only send a party of the files to each other and then just stop all of a sudden, when I don't include the code that problem doesn't occure.


    Thanks for your help.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why do you insist on cross-posting?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    << snipped answer - can't be arsed with xposters >>
    Your loss.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by rags_to_riches View Post
    Why do you insist on cross-posting?
    What is everyone's problem with cross posting i'm just trying to get the problem solved as fast as possible...

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by MaSSaSLaYeR View Post
    What is everyone's problem with cross posting i'm just trying to get the problem solved as fast as possible...
    maybe you should learn some patience instead of just expecting things to come instantly.

    there is one context in which cross posting is acceptable:

    you notify the users of EVERY forum of the cross post, and you share the answer with EVERY forum once you receive it. otherwise, nobody knows that your question may have been answered elsewhere, and they waste their time answering it again.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What is everyone's problem with cross posting i'm just trying to get the problem solved as fast as possible...
    Then go to a professional help site and pay for as much priority as you can afford.

    At the moment, you're just abusing the FREE generosity of other people.
    Choose a forum
    and
    No, it isn't urgent

    Now imagine if every single noob behaved as you do - posting the same question multiple times across multiple forums. Some lucky noob would get the same answer lots of times in lots of places. But because lots of people WASTED THEIR TIME answering a question that had already been answered, other noobs get NO ANSWER at all in any place they posted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    45
    Quote Originally Posted by Salem View Post
    > What is everyone's problem with cross posting i'm just trying to get the problem solved as fast as possible...
    Then go to a professional help site and pay for as much priority as you can afford.

    At the moment, you're just abusing the FREE generosity of other people.
    Choose a forum
    and
    No, it isn't urgent

    Now imagine if every single noob behaved as you do - posting the same question multiple times across multiple forums. Some lucky noob would get the same answer lots of times in lots of places. But because lots of people WASTED THEIR TIME answering a question that had already been answered, other noobs get NO ANSWER at all in any place they posted.
    Excuse me! First off: Everytime I cross post I link to the forum where the answer was found to the problem.

    Second I only post things if I really can't find them after I for example:

    Debugged every thing in the function looked at wiresharks and tried to ask friends.

    Once it gets around 11 at night I get desperate and I turn to forums as these.

    I hope you as programmer understand how much effort something takes.

    Well I was out of effort for that day.

    Greetz.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending/Receiving packets over socket
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 10-22-2010, 10:01 AM
  2. Sending / Receiving Info Via TCP or UDP
    By bengreenwood in forum C Programming
    Replies: 0
    Last Post: 03-24-2009, 02:17 AM
  3. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  4. Sending/Receiving Messages
    By osal in forum C++ Programming
    Replies: 0
    Last Post: 02-19-2005, 09:11 PM
  5. single Socket for listening and sending/receiving, Simultaneously?
    By Aidman in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-01-2003, 11:17 PM