c - getting bad input from dirty buffer -
essentially have simulating tcp connection sockets , sending information between client , server between sockets. want use single buffer each file, unfortunately when writing, overwrites buffer getting bad/dirty string in line (b) buffer containing noexistshed
, hed
part, being remnant of established
previous message written. @ first client prints connection status: established
fine, later on, because of dirty buffer, printf in line (c), prints 1
, since "noexistshed" , "noexist" different.
all buffers declared char buffer[1024];
. user , pass defined char user[80]
, char pass[80]
, (although seeking make these 2 single buffer).
this problematic section of client:
read(clientsocket, buffer, 1024); /*---- print received message ----*/ printf("connection status: %s\n", buffer); printf("please enter username:\n"); scanf ("%s", user); write(clientsocket, user, strlen(user)+1); read(clientsocket, buffer, 80); // error happens here -- (b) printf("%s comparison result %d", buffer, strcmp(buffer, "noexists")); // --- (c) if(strcmp(buffer, "noexists") == 0) { printf("user doesn't exist\n"); exit(1); }
this problematic section of server:
strcpy(buffer,"established."); write(sendersocket,buffer,13,0); memset(buffer,0,sizeof(buffer)); // ---- (a) read(sendersocket, buffer, 80, 0); int x; for(x=0; x < 7 && strcmp(ul[0][0], buffer) != 0; x++); printf("%d", x); if(x == 7) { strcpy(buffer,"noexists"); } else { strcpy(buffer,"exists"); } write(sendersocket, buffer, strlen(buffer), 0); read(sendersocket, buffer, 80, 0);
line (a) presume might solution problems have read in other questions/sites, not working. have tried replacing buffer[0] = '\0';
, buffer[n] = '\0';
(with n being assigned value of read/write) no luck. i've tried putting after/before writes , reads, there many permutations can done, don't think can solve on own. appreciate help.
Comments
Post a Comment