client connect continuously

I want to connect continuously with the client to establish a connection to the server, and if the server is disconnected, the client re-connects to the server,
but if server is not up first error connection refuse and then print ERROR opening socket and connection error Bad file descriptor in this case if server listening doesn't connect to the client any more

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
bool setup::conn() {

	struct sockaddr_in serv_addr;
	struct hostent *server;
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1){
		printf("ERROR opening socket");
		fflush(stdout);
	}
	else {
		printf("everything is Ok\n");
		fflush(stdout);
	}

	server = gethostbyname("192.168.1.140");
	if (server <= 0) {
		fprintf(stderr, "ERROR, no such host\n");
		fflush(stdout);
	} else {
		printf("connected");
		fflush(stdout);
	}
	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *) server->h_addr,
	(char *)&serv_addr.sin_addr.s_addr,
	server->h_length);
	serv_addr.sin_port = htons(MYPORT);

	while (true) {
		fflush(stdout);
		csock = (int*) malloc(sizeof(int));
		*csock = connect(sock, (struct sockaddr *) &serv_addr,
				sizeof(serv_addr));
		if (*csock == -1) {
			perror("ERROR connecting");
			close(*csock);
			break;

		}
      }
}
Last edited on
Is there any reason for line 32? This way it consumes memory (and leaks) while trying to connect.

Do not close connection when it failed. Instead pause for a while.
I do this change but when once the client connect to the server print ERROR connecting: Transport endpoint is already connected
and if server disconnect and listening again ,can not connect to server,now what should I do?
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
bool setup::conn() {

	struct sockaddr_in serv_addr;
	struct hostent *server;
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1){
		printf("ERROR opening socket");
		fflush(stdout);
	}
	
	server = gethostbyname("192.168.1.140");
	if (server <= 0) {
		fprintf(stderr, "ERROR, no such host\n");
		fflush(stdout);
	}
	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *) server->h_addr,
	(char *)&serv_addr.sin_addr.s_addr,
	server->h_length);
	serv_addr.sin_port = htons(MYPORT);

	while (true) {
		fflush(stdout);
		csock = (int*) malloc(sizeof(int));
		*csock = connect(sock, (struct sockaddr *) &serv_addr,
				sizeof(serv_addr));
		if (*csock == -1) {
			perror("ERROR connecting");
			usleep(1000);

		}
      }
}
Last edited on
You need to break; when connect(...) was successfull (not when it fails).
I do this and it work ,thank you

1
2
3
4
5
6
7
8
9
10
11
while (true) {
		fflush(stdout);
		csock = (int*) malloc(sizeof(int));
		*csock = connect(sock, (struct sockaddr *) &serv_addr,
				sizeof(serv_addr));
		if (*csock == -1) {
			perror("ERROR connecting");
			if (errno == EISCONN)
                        break;
		}
      }
Last edited on
Topic archived. No new replies allowed.