Hi everyone, I am writing socket programming in c in Linux. I wrote client and sever programs. But when I run it, server is ok. But client showed that connection refused error. Why is it so?
My client code is
You should specify the protocol in: socket(AF_INET, SOCK_STREAM,0)
The BSD socket library attepts to fill in the protocol for you if you specify 0. But WinSock won't for example. So you may as well just say what it is. Asl it happens, PF_INET has the same value as AF_INET, but socket() really takes PF_INET, so you should use that. It should be: socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
It's always best to zero out the address structures before using them. You did that, but comment out the code.
You shouldn't use htol on INADDR_ANY. So this is wrong. serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
I can't really see anything else that's wrong other than the missing accept() lopp/client processing.