Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. fail to envoke shutdown socket function.

fail to envoke shutdown socket function.

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitranjanmishra
    wrote on last edited by
    #1

    i fail to envoke shutdown function. i m trying to write the data recd in the socket buffer to a fiel. though i manage it but it happens only i stop the application and not before. i tried many alternatives but feel i m still short of something. Seeing the given code can i be suggested why???? // THe following is a client thread UINT ClientThread(LPVOID pParam) { char *buff; CString cmd; CString params; int n; int flag=0; BOOL auth=false; buff=(char *)malloc(1); SOCKET client=(SOCKET)pParam; recv(server,buff,sizeof(buff),0); // ftxt=fopen("d:\\amit\\COMMAND.txt","a+t"); while(true) {ftxt=fopen("d:\\amit\\COMMAND.txt","a+t"); while(n=recv(client,buff,sizeof(char),0)) { flag=1; // n=recv(client,buff,sizeof(char),0); fwrite(buff,sizeof(char),1,ftxt); if(n==SOCKET_ERROR ) break; buff[n]=0; cout< amit mishra

    J 1 Reply Last reply
    0
    • A amitranjanmishra

      i fail to envoke shutdown function. i m trying to write the data recd in the socket buffer to a fiel. though i manage it but it happens only i stop the application and not before. i tried many alternatives but feel i m still short of something. Seeing the given code can i be suggested why???? // THe following is a client thread UINT ClientThread(LPVOID pParam) { char *buff; CString cmd; CString params; int n; int flag=0; BOOL auth=false; buff=(char *)malloc(1); SOCKET client=(SOCKET)pParam; recv(server,buff,sizeof(buff),0); // ftxt=fopen("d:\\amit\\COMMAND.txt","a+t"); while(true) {ftxt=fopen("d:\\amit\\COMMAND.txt","a+t"); while(n=recv(client,buff,sizeof(char),0)) { flag=1; // n=recv(client,buff,sizeof(char),0); fwrite(buff,sizeof(char),1,ftxt); if(n==SOCKET_ERROR ) break; buff[n]=0; cout< amit mishra

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      I do not know about your actual problem, but you have other problems. char *buff; buff=(char *)malloc(1); recv(server,buff,sizeof(buff),0); recv(client,buff,sizeof(char),0); buff[n]=0; 1) Allocating a 1 character buffer makes no since and you never free the memory allocated. Just declare a single character variable or a small buffer: char buff; or char buff[1]; 2) These two statements are not the same thing: recv(server,buff,sizeof(buff),0); recv(client,buff,sizeof(char),0); sizeof(buff) == sizeof(char*) NOT sizeof(char); If you had declared the buff variable as buff[1] or buff then sizeof(buff) would equal sizeof(char). 3) buff[n]=0; if n is anything other than 0, then your thread may (should) crash. buff[1] = 0; // ERROR 4) Unless you have a good reason, constantly opening and closing the file in the outer loop is probably a bad idea too! INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

      A 1 Reply Last reply
      0
      • J John R Shaw

        I do not know about your actual problem, but you have other problems. char *buff; buff=(char *)malloc(1); recv(server,buff,sizeof(buff),0); recv(client,buff,sizeof(char),0); buff[n]=0; 1) Allocating a 1 character buffer makes no since and you never free the memory allocated. Just declare a single character variable or a small buffer: char buff; or char buff[1]; 2) These two statements are not the same thing: recv(server,buff,sizeof(buff),0); recv(client,buff,sizeof(char),0); sizeof(buff) == sizeof(char*) NOT sizeof(char); If you had declared the buff variable as buff[1] or buff then sizeof(buff) would equal sizeof(char). 3) buff[n]=0; if n is anything other than 0, then your thread may (should) crash. buff[1] = 0; // ERROR 4) Unless you have a good reason, constantly opening and closing the file in the outer loop is probably a bad idea too! INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

        A Offline
        A Offline
        amitranjanmishra
        wrote on last edited by
        #3

        Taking char * buff has a strong reason behind as i m trying to recv a string eg. SYNTAX like Operator play file abc.wav yeah a typing err n i apologize fr recv(server,....)it was n shud be recv(client,....). Constantly opening and closing file is reqd as the client may keep sending data. eg. 1.

        open channel ch_id,play file filename,record fiel fielrec

        , etc... i want to envoke shutdown in the outer loop n yes i ddnt free memory was my mistake..for once the data is written to the fiel the memory shud have been freed n file closed. amit mishra

        J 1 Reply Last reply
        0
        • A amitranjanmishra

          Taking char * buff has a strong reason behind as i m trying to recv a string eg. SYNTAX like Operator play file abc.wav yeah a typing err n i apologize fr recv(server,....)it was n shud be recv(client,....). Constantly opening and closing file is reqd as the client may keep sending data. eg. 1.

          open channel ch_id,play file filename,record fiel fielrec

          , etc... i want to envoke shutdown in the outer loop n yes i ddnt free memory was my mistake..for once the data is written to the fiel the memory shud have been freed n file closed. amit mishra

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          You still do not need to allocate a buffer, since all you need is a pointer to a single character. Hmmm! I should have noted that recv(server,...) was a typo, since that was a typo, then shutdown(server...) is also a typo. I looked up the functions used and here is what your code will do once the typos are fix: FIRST TIME TRU LOOP: 1) Read 1 byte from the socket and then ignore it. 2) START INFINITE LOOP { 3) Open a file. 4) While( there is a byte received OR a SOCKET_ERROR ) { 5) copy byte to file. // before checking if error occured 6) if( error ) break; } 7) Shut down socket so you can no longer send or receive and data. (This means recv(...) will now always return SOCKET_ERROR) 8) Close file. 9) } // END OF INFINITE LOOP AFTER FIRST LOOP: 1) START INFINITE LOOP { 2) Open file. 3) While( SOCKET_ERROR ) { // because of shutdown above 4) Write the last character received (during first loop). 5) Break out of loop (since it was a SOCKER_ERROR). } 6) Try to shut down again (cann't because it was already done). 7) Close file. 8) } // END OF INFINITE LOOP I do not think that was what you were trying to do, but that is what should be happening. There is also the fact that the INFINITE LOOP cann't be stoped properly (any other way if bad). INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups