Connection refused in socket programming in c

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <sys/socket.h>  //for socket(), connect(), sendto() and recvform()
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>  // for printf() and fprintf()
#include <string.h>  //for memset()
#include <stdlib.h>   //for atoi() and exit()
#include <unistd.h>  //for close()
#include <errno.h>
#include <arpa/inet.h>  //for sockaddr_in and inet_addr()
 

#define SERVERPORT 11000 
int main()
{
	int sockfd  = 0;
	struct sockaddr_in serv_addr;
	if((sockfd=socket(AF_INET, SOCK_STREAM,0))< 0)
	{
		printf("Cannot create socket\n");
		return 1;
	}
	printf("Already create socket!!!\n");
	//memset(&serv_addr, 0 , sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;  //AF_INET is IP address family, Internet = IP addr 
	printf("Finished AF_INET\n");
	//serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //sin_addr = ip addr
	printf("Finished inet_addr\n");
	serv_addr.sin_port = htons(11000);  //sin_port = tcp/ip port no		
	printf("Finished port\n");
	//bzero(&serv_addr.sin_zero,8);
	
	if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0) 
		{
			perror("Cannot connect\n");			
			exit (1);
		}
	else 
		printf("Connected\n");
}


and my server code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

int main()
{
	int listenfd = 0;
	struct sockaddr_in serv_addr;
	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	
	if (listenfd < 0)
	{
		printf("Cannot create socket\n");
		close(listenfd);
		return 1;
	}
	printf("Already create socket!!!\n");
	serv_addr.sin_family = AF_INET;
	printf("Finished AF_INET\n");
	serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
	printf("Finished INADDR_ANY\n");
	serv_addr.sin_port = htons(11000);
	printf("Finished port\n");
	
	if(bind (listenfd,(struct sockaddr *)&serv_addr, sizeof (serv_addr))<0)
	{
		printf("Cannot bind socket\n");
		close(listenfd);
		return 1;
	}
	
	
}


is there any mistake in my programs? if it is so, pls kindly point out for me.
The server is listening for connections (with listen()), but it also needs to accept new connections with accept().

accept() returns a new socket that you use to communicate with the client. You read/write on it then close it when the session is done.

accept() is typically called in a loop. By default it will block until a client connects.
so in my server program, I should include until accept the connection right? Currently my client program gave me the error "Address family not supported by protocol". :(
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.
yes. I had change my socket(PF_INET,....) as you said. But I still had Address family not supported by protocol error. Why is it so?
Did you set the protocol?
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)

Did you remove that htonl on INADDR_ANY?
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
Last edited on
I used
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

and I removed, htonl(INADDR_ANY);

now I use
int res;
res = inet_pton(AF_INET,"127.0.0.1", &serv_addr,sin_addr);

is it correct? still got same address family not supported by protocol.
hi, I solved the address family not supported by protocol error.
I changed below code in my client program
serv_addr.sin_port=ntohs(11000);
:)
When I wrote a c# server program in window and c client program in Linux, when I run client program it showed connection refused again. :( Between 2 computers, I used crossover cable.
Topic archived. No new replies allowed.