Thread: Reading + socket communication

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

    Reading + socket communication

    Below I would like to enter the characters "s1p1" into a remote terminal once the server has connected to the client and print the respective statement locally. The problem I am having when I want to write "s1p1" is that if i press s, the program processes s, but when I press "1" the program no longer has the s stored.
    The program performs actions to single characters successfully as shown by the second enclosed if statement.
    Code:
    int sock_fd;
    int new_fd;
    int rc;
    char recv_client_msg[100];
    
    rc=read(new_fd, recv_client_msg,sizeof(recv_client_msg));
    
    if(rc>0)
     {
       if(strcmp(recv_client_msg,"s1p1")==0){printf("s1p1\n");}
       if(recv_client_msg[0]=="s"){printf("m\n");}
     }
    Any suggestions?
    I assume the solution will involve somewhere an array and a for loop with incrementation.
    Before anyone suggests changing "strcmp" this is NOT the issue and the issure is storing the characters without overwriting them.

    Any help would be appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    This should not even compile... since "s" and 's' are different things

    You ignoring the rc value - use it to determine how many bytes were received and if not whole string yet - accumulate result in the intermediate buffer
    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

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    got it sorted by reading bytes, what could I write if I made the program do a action locally whilst the remote terminal was open for it to pront on the remote terminal that something has been changed locally?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by DA93 View Post
    what could I write if I made the program do a action locally whilst the remote terminal was open for it to pront on the remote terminal that something has been changed locally?
    Could you explain what do you mean?
    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

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    for example if I pressed a key on the remote terminal to tun a light off. I would like the program to in fact check that its off, and report to the user by printing on the REMOTE CLIENT TERMINAL "light off". Similarly if i told the light to turn on/off remotely but the command wasn't executed properly for it to display a different message to the remote user such as "command failed"

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    Also say if you had the remote terminal open and you saw that the light was off, if i then turned it on locally via a button or something else, a notice would appear on your remote terminal saying the light has been turned on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw Socket Client and Server Communication Problem
    By rplumii in forum C Programming
    Replies: 6
    Last Post: 02-10-2011, 07:02 AM
  2. raw socket to dgram communication
    By cole701 in forum Networking/Device Communication
    Replies: 3
    Last Post: 09-02-2010, 03:46 PM
  3. Serial Communication Code for Reading
    By coolrox86 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2010, 05:30 AM
  4. UDP socket partial reading
    By mynickmynick in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-25-2009, 11:43 AM
  5. Socket communication hanging
    By zee in forum C Programming
    Replies: 15
    Last Post: 08-05-2004, 11:52 PM

Tags for this Thread