I am doing a Unix, C assignment. I am creating a Server and a Client which will interact with each other. I am pretty sure I have set up the basic framework but I when I try to send/receive messages, it doesn't work.
Here is the while loop code for the server, I tried to show only the relevant code:
while(1) {
clntAdrLen = sizeof(clntAddr);
clntFd = accept(srvrFd, (struct sockaddr*)&clntAddr, NULL);
if (fork() == 0) {
send(clntFd, "YourMessage", 12, NULL);
close(clntFd);
exit(0);
} else {
close(clntFd);
}
}
And here is the code for client:
do {
result = connect(srvrFd, (struct sockaddr*)&srvrAddr, srvrLen);
if(result==-1) {
sleep(1);
}
recv(srvrFd, buf, sizeof(buf), NULL);
printf("%s", buf); //here I try to print the message sent by server
} while (result==1);
When I run both server and client, It should print "YourMessage". Instead it prints:
N0�,
Am I just doing it wrong? Thanks