Find the answer to your Linux question:
Results 1 to 5 of 5
I have created a small client/server program for file transfers. When I run it, I get the error: Code: ERROR - Call to recv() failed: Transport endpoint is not connected ...
  1. #1
    Just Joined! KPolulak's Avatar
    Join Date
    Feb 2009
    Location
    New Jersey, USA
    Posts
    42

    Question C Socket Trouble

    I have created a small client/server program for file transfers. When I run it, I get the error:

    Code:
    ERROR - Call to recv() failed: Transport endpoint is not connected
    This message is defined on line 79 in the server source code (errno is 107).

    I have run both programs through gdb numerous times but cannot seem to pinpoint the point of failure (have you ever tried debugging two apps simultaneously that communicate with each other? )

    Here is the server's source code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netdb.h>
    
    #define SERVERPORT 4444
    #define MAXBUF 1024
    
    int main(void) {
    	int socket1, socket2;
    	int return_status;
    	struct sockaddr_in xfer_server, xfer_client;
    	socklen_t addrlen = 0;
    
    	/* Create first socket */
    	if ((socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    		perror("ERROR - Call to socket() failed");
    		exit(EXIT_FAILURE);
    	}
    
    	/* Zero-out client and server address structures */
    	memset(&xfer_server, 0, sizeof(xfer_server));
    	memset(&xfer_client, 0, sizeof(xfer_server));
    
    	/* Setup server address structure */
    	xfer_server.sin_family = AF_INET;
    	xfer_server.sin_port = htons(SERVERPORT);
    	xfer_server.sin_addr.s_addr = INADDR_ANY;
    
    	/* Bind address/port to socket */
    	return_status = bind(socket1, (struct sockaddr *) &xfer_server, sizeof (xfer_server));
    
    	if (return_status == -1) {
    		perror("ERROR - Call to bind() failed");
    
    		close(socket1);
    		exit(EXIT_FAILURE);
    	}
    
    	/* Listen for incoming connections and limit queue to 5 incoming connections */
    	return_status = listen(socket1, 5);
    
    	if (return_status == -1) {
    		perror("ERROR - Call to listen() failed");
    
    		close(socket1);
    		exit(EXIT_FAILURE);
    	}
    
    	/* Main loop */
    	while (1) {
    		int fd, i = 0;
    		char *buf_ptr;
    		char buf[MAXBUF], filename[MAXBUF];
    		ssize_t read_c, write_c;
    
    		addrlen = sizeof (xfer_client);
    
    		/* Accept a new connection on a socket */
    		socket2 = accept(socket1, (struct sockaddr *) &xfer_client, &addrlen);
    
    		if (socket2 == -1) {
    			perror("ERROR - Call to accept() failed");
    
    			close(socket1);
    			exit(EXIT_FAILURE);
    		}
    
    		/* Get filename from client */
    		if ((read_c = recv(socket1, filename, MAXBUF, 0)) > 0) {
    			i += read_c;
    			filename[i + 1] = '\0';
    		} else {
    			perror("ERROR - Call to recv() failed");
    
    			close(socket2);
    			continue;
    		}
    
    		printf("Requested file: %s\n", filename);
    
    		/* Open requested file as read-only */
    		fd = open(filename, O_RDONLY);
    
    		if (fd == -1) {
    			perror("ERROR - Call to open() failed");
    
    			close(socket2);
    			continue;
    		}
    
    		/* Reset read counter */
    		read_c = 0;
    
    		/* Send file to client */
    		while ((read_c = read(fd, buf, MAXBUF)) > 0) {
    			write_c = 0;
    			buf_ptr = buf;
    
    			while (write_c < read_c) {
    				read_c -= write_c;
    				buf_ptr += write_c;
    
    				write_c = send(socket2, buf_ptr, read_c, 0);
    
    				if (write_c == -1) {
    					perror("ERROR - Call to send() failed");
    
    					close(fd);
    					close(socket2);
    					continue;
    				}
    			}
    		}
    
    		close(fd);
    		close(socket2);
    	}
    
    	close(socket1);
    	return (EXIT_SUCCESS);
    }
    Client's source code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    
    #define SERVERPORT 4444
    #define MAXBUF 1024
    
    int main(int argc, char *argv[]) {
    	int sock, count, fd, ret_stat;
    	char buf[MAXBUF];
    	struct sockaddr_in xfer_server;
    
    	if (argc < 3) {
    		fprintf(stderr, "Usage: %s <ip address> <filename> [destination filename]\n", argv[0]);
    		exit(EXIT_FAILURE);
    	}
    
    	/* Create socket */
    	if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    		perror("ERROR - Call to socket() failed");
    		exit(EXIT_FAILURE);
    	}
    
    	/* Zero-out server address structre */
    	memset(&xfer_server, 0, sizeof (xfer_server));
    
    	/* Setup server address structre */
    	xfer_server.sin_family = AF_INET;
    	xfer_server.sin_port = htons(SERVERPORT);
    
    	ret_stat = inet_pton(AF_INET, argv[1], &(xfer_server.sin_addr.s_addr));
    
    	if (ret_stat == 0) {
    		fprintf(stderr, "%s is not a valid IP address\n", argv[1]);
    		exit(EXIT_FAILURE);
    	}
    
    	/* Connect to server */
    	ret_stat = connect(sock, (struct sockaddr *) &xfer_server, sizeof (xfer_server));
    
    	if (ret_stat == -1) {
    		perror("ERROR - Call to connect() failed");
    		
    		close(sock);
    		exit(EXIT_FAILURE);
    	}
    
    	/* Send filename to server */
    	ret_stat = send(sock, argv[2], strlen(argv[2]) + 1, 0);
    
    	if (ret_stat == -1) {
    		perror("ERROR - Call to send() failed");
    
    		close(sock);
    		exit(EXIT_FAILURE);
    	}
    
    	/* Shutdown socket and make it read-only */
    	shutdown(sock, SHUT_WR);
    
    	/* Open destination file */
    	if (argv[3]) {
    		fd = open(argv[3], O_WRONLY | O_CREAT | O_APPEND);
    	} else {
    		/* Default */
    		fd = open("xfer_server_file", O_WRONLY | O_CREAT | O_APPEND);
    	}
    
    	if (fd == -1) {
    		perror("ERROR - Call to open() failed");
    
    		close(sock);
    		exit(EXIT_FAILURE);
    	}
    
    	/* Read file from server */
    	while ((count = recv(sock, buf, MAXBUF, 0)) > 0) {
    		/* Write to destination file */
    		write(fd, buf, count);
    
    		if (count == -1) {
    			perror("ERROR - Call to recv() failed");
    
    			close(sock);
    			close(fd);
    			exit(EXIT_FAILURE);
    		}
    	}
    
    	close(sock);
    	return (EXIT_SUCCESS);
    }

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    When recv is done(shutdown) it returns a 0 which is an error in your if statement.

    Code:
    		if ((read_c = recv(socket1, filename, MAXBUF, 0)) > 0) {
    			i += read_c;
    			filename[i + 1] = '\0';
    		} else {
    			perror("ERROR - Call to recv() failed");
    
    			close(socket2);
    			continue;
    		}
    Make mine Arch Linux

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Actually this is the error...

    Code:
    		socket2 = accept(socket1, (struct sockaddr *) &xfer_client, &addrlen);
    
    		if (socket2 == -1) {
    			perror("ERROR - Call to accept() failed");
    
    			close(socket1);
    			exit(EXIT_FAILURE);
    		}
    
    		/* Get filename from client */
    		if ((read_c = recv(socket1, filename, MAXBUF, 0)) > 0) {
    			i += read_c;
    			filename[i + 1] = '\0';
    		} else {
    			perror("ERROR - Call to recv() failed");
    
    			close(socket2);
    			continue;
    		}
    You create socket2 and try to read from socket1(listening socket).
    Last edited by gerard4143; 04-29-2010 at 03:10 PM.
    Make mine Arch Linux

  4. #4
    Just Joined! KPolulak's Avatar
    Join Date
    Feb 2009
    Location
    New Jersey, USA
    Posts
    42
    Thanks!

    This was just a sample program from "The Definitive Guide to Linux Network Programming" that didn't really have a purpose. The book recommended making two sockets so that multiple requests could be made to the server at the same time. Not only did this end up confusing the hell out of me when it wouldn't work but I also couldn't help but think that there must be a more efficient way to handle multiple requests. Is there a more advanced technique for doing so?

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by KPolulak View Post
    Thanks!

    This was just a sample program from "The Definitive Guide to Linux Network Programming" that didn't really have a purpose.
    Sure it had a purpose..to send the contents of a file to the client.

    Quote Originally Posted by KPolulak View Post
    The book recommended making two sockets so that multiple requests could be made to the server at the same time. Not only did this end up confusing the hell out of me when it wouldn't work but I also couldn't help but think that there must be a more efficient way to handle multiple requests.
    This is the normal way...One socket to listen and one socket to handle requests.

    Quote Originally Posted by KPolulak View Post
    Is there a more advanced technique for doing so?
    Your having problems with this and you want a more advanced technique?

    I can recommend a book - Internetworking With TCP/IP Volume III: Client-Server Programming and Applications, Linux/POSIX Socket Version (with D. Stevens), 2000. 0-13-032071-4 - Its one of the most practical books on the subject.
    Make mine Arch Linux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...