Thread: few issues with sending commands with sockets in c

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    40

    few issues with sending commands with sockets in c

    I have a few questions regarding the sending of commands in C, and the solutions are probably obvious once I see them. I should mention that if I enter the exact string of characters followed by the enter key, the corresponding command DOES WORK as it should.

    Code:
    void ReadSock()
     {  // main accept() loop
      rc = read(new_fd, recv_client_msg +bytesread, sizeof(recv_client_msg) - bytesread); 
       if(read_pin(0x21, 12)==1) { 
      if(rc>0)
       { 
        if(write(new_fd,recv_client_msg +bytesread , rc) <0 ) { printf("error writing to stream socket\n"); } 
        bytesread +=rc; 
        for(b = 0; b < bytesread; ++b)
         {
          if (recv_client_msg[b]=='\n'|| recv_client_msg[b] == '\r')
           {
            recv_client_msg[b+1]=0;     
            printf("%s\n %d\n" , recv_client_msg, b);     
            
            if(b>0 && memcmp(recv_client_msg, "s1p1" , b)==0) 
             {
              commandSwitch(1,1);
             }
                  
            if(b>0 && memcmp(recv_client_msg, "s1p2" , b)==0) 
             {
              commandSwitch(1,2);
             }
                  
            if(b>0 && memcmp(recv_client_msg, "s2p1" , b)==0) 
             {
              commandSwitch(2,1);
             }
                  
            if(b>0 && memcmp(recv_client_msg, "s2p2" , b)==0) 
             {
              commandSwitch(2,2);
             }
                  
            if(b>0 && memcmp(recv_client_msg,   "x"  , b)==0) 
             {
              close(new_fd); new_fd=-1;
             }
                  
            bytesread=0; 
           }     
         }
       }//end of if rc==0
    }//end off if wrire pin
       else{      
            if (send(new_fd, "Unable to operate remotely, system is in local mode", 51, 0) == -1);
           }
         
      if(rc<=0)
       {
        if(rc==0){printf("connection closed nicely at the other end\n"); close(new_fd); new_fd=-1;}
        else{if(errno!=EAGAIN) {fprintf(stderr,"Error reading: %s\n", strerror(errno));}} 
       }
     }
    1). How could i make it so after I type in a command and it executes for my remote terminal to advance to the next line automatically? As, it stands after processing the command the cursor resets to the start of the current line upon pressing enter and if I press down on the keyboard it registers as a character so I have to then press enter again.

    2). Pretty small bug but however a problem: If I press enter if I have happened to only press the "s" key, all 4 commands execute as they all begin with the character "s". Similarly if I only type "s1" before pressing enter, the two commands beginning with "s1" execute. Long story short I would only like my system to execute a command if the characters are entered exactly as they are.

    3). And Finally: How do I make it so pressing the backspace key doesn't register as a character? e.g. if i want to type "s2p1" but if I type "s2p2" by accident then press backspace followed by 1, even though I will see "s2p1" on my terminal, it won't run the command as I have in actual fact entered 6 characters: s,2,p,2,backspace,1.

    Hope these can be answered, I'm sure the changes required a quite small and trivial, especially with 2).

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    EDIT: 1). solved: by writing if(send(new_fd, "\r\n", 4 ,0)== -1);
    Still can't think of 2). or 3). though...

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not know why would you send backspace instead of handling it locally before sending buffer, but for your comparison bug - check that you have enough characters to compare

    Code:
    if(b>=4 && memcmp(recv_client_msg, "s1p2" , 4)==0) 
    {
              commandSwitch(1,2);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    Thanks, i'll give that a try, and the reason for that is, say for example if I want to control the system remotely where the system is in another building a 5 minute drive away. if I enter the wrong string before pressing enter, if i pressed backspace to alter it, the string would be extended and not work as the string will have x ammount of 08 backspace characters in it even though the terminal says the correct message

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    Vart, replacing the b with a 4 worked thanks ^^
    Just 3). to clear up now, I'm sure the solution will be pretty simple, and involve a similar process to when I used the enter key to confirm rather than a character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending Hex to sockets?
    By chrisjmoss in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-03-2008, 05:50 AM
  2. sending commands
    By DeepFyre in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2006, 01:19 PM
  3. Sending Shell Commands
    By Pudnut in forum C# Programming
    Replies: 2
    Last Post: 08-29-2004, 01:13 AM
  4. Sending commands over IDE
    By Coherent in forum Windows Programming
    Replies: 0
    Last Post: 10-07-2003, 11:12 AM
  5. Sending commands to port
    By Neha in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-28-2002, 02:52 PM

Tags for this Thread