First of all, write()
will expect the address to whatever memory is going to be sent, so you can't just pass the number (your client code) or dereference an integer that's never been set (your server code). Also, is there any specific reason you're sending 10 bytes? That's really odd for an integer.
I'd try the following:
Client:
int nread = 23;
write(sockfd, &nread, sizeof(int)); // &nread returns the address to nread
write(sockfd, &nread, sizeof(nread)); // Alternative version always using the proper type
Server:
int value;
read(sock, &value, sizeof(int)); // &value returns the address to value
read(sock, &value, sizeof(value)); // Alternative version always using the proper type
However, keep in mind that this might have more issues, e.g. when considering a mix of little endian/big endian systems as well as systems with different lengths for int
.