Reputation: 149
I have a TCP code, but it keeps giving me error in the server code.
When I run it using valgrind, I get the following in my server terminal:
==12370== Use of uninitialised value of size 8
==12370== at 0x4E6B506: ____strtol_l_internal (in /lib64/libc-2.18.so)
==12370== by 0x4E687DF: atoi (in /lib64/libc-2.18.so)
==12370== by 0x400F1D: main (in /mnt/castor/seas_home/x/xyz/TCP/serv)
==12370==
==12370== Invalid read of size 1
==12370== at 0x4E6B506: ____strtol_l_internal (in /lib64/libc-2.18.so)
==12370== by 0x4E687DF: atoi (in /lib64/libc-2.18.so)
==12370== by 0x400F1D: main (in /mnt/castor/seas_home/a/xyz/TCP/serv)
==12370== Address 0xffdfffac00000 is not stack'd, malloc'd or (recently) free'd
==12370==
==12370==
==12370== Process terminating with default action of signal 11 (SIGSEGV)
==12370== General Protection Fault
==12370== at 0x4E6B506: ____strtol_l_internal (in /lib64/libc-2.18.so)
==12370== by 0x4E687DF: atoi (in /lib64/libc-2.18.so)
==12370== by 0x400F1D: main (in /mnt/castor/seas_home/a/xyz/TCP/serv)
==12370==
==12370== HEAP SUMMARY:
==12370== in use at exit: 0 bytes in 0 blocks
==12370== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==12370==
==12370== All heap blocks were freed -- no leaks are possible
==12370==
==12370== For counts of detected and suppressed errors, rerun with: -v
==12370== Use --track-origins=yes to see where uninitialised values come from
==12370== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)
But I can't figure out how to fix it. Could anyone tell me how to fix this?
Upvotes: 0
Views: 161
Reputation: 16540
there are several basic problems with the server code.
here is just one that needs correction.
the server code is using the 'listen'/'accept' socket
to write to the client.
However, the accept function returns a NEW socket
That new socket is the one to use to communicate with the client.
here is an excerpt from the man page about the accept() function.
The accept() system call is used with connection-based socket types
(SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection
request on the queue of pending connections for the listening socket,
sockfd, creates a new connected socket, and returns a new file descrip‐
tor referring to that socket. The newly created socket is not in the
listening state. The original socket sockfd is unaffected by this
call.
The argument sockfd is a socket that has been created with socket(2),
bound to a local address with bind(2), and is listening for connections
after a listen(2).
the key information is:
"creates a new connected socket, and returns a new file descrip‐
tor referring to that socket."
suggest reading the manual
for system functions that are called within your code
so the variable 'recsize' is actually a socket for communication with the client.
Upvotes: 0
Reputation: 15232
This is the important part of your code:
char *words[10];
char buffer[1024];
memset(buffer, 0, sizeof buffer);
nwords = getwords(buffer, words, 10);
option = atoi(words[0]);
Because you call getwords
with an empty buffer, it will return 0 and not set anything in words
, so words[0]
is also uninitialised.
You don't check the return value of getwords
, but you should. If nwords==0
, you should not use words[0]
.
Upvotes: 1