I can't seem to find a solution to this, most people have the following but this doesn't work for me.
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");}
}
My sockets are completely functional. When the client is open, if I press the "m" key the program prints "m" on the local terminal. However I want to print "s1p1" on the local terminal when I type "s1p1" when I have the client open. However this doesn't happen despite reading previous examples and the compilation being successful.
Any tips would be appreciated
My theory is it may be something to do with the program processing the first character "s", successfully but when I enter "1" the first "s" is overwritten by the "1" rather than takingg up the second slot in the string.
Use
memcmp()
instead, thestrcmp()
function requires that both parameters benul
terminated, which is not guaranteed when passing the string through a socket, you can usememcmp()
which takes the number of bytes to compare as a parameter.the
(rc == 5)
test is performed because if thenul
termination byte is present, then the comparison should also give true.