unix - How do I communicate between a server and a client using sockets? [C] -
i doing unix, c assignment. creating server , client interact each other. pretty sure have set basic framework when try send/receive messages, doesn't work.
here while loop code server, tried show 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 code client:
do { result = connect(srvrfd, (struct sockaddr*)&srvraddr, srvrlen); if(result==-1) { sleep(1); } recv(srvrfd, buf, sizeof(buf), null); printf("%s", buf); //here try print message sent server } while (result==1); when run both server , client, should print "yourmessage". instead prints:
n0�, am doing wrong? thanks
i guess problem in accept function.
as said in linux programmer's manual:
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); the addrlen argument value-result argument: caller must initialize contain size (in bytes) of structure pointed addr; on return contain actual size of peer address.
Comments
Post a Comment