Like Tree3Likes
  • 3 Post By antred

server doesn't send string

This is a discussion on server doesn't send string within the C Programming forums, part of the General Programming Boards category; This is my server: Code: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52

    server doesn't send string

    This is my server:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <netdb.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <sys/sendfile.h>
    #include <sys/stat.h>
    
    
    int main(int argc, char *argv[]){
    	if(argc != 2){
    		perror("./server <numero porta>\n");
    		exit(1);
    	}
    	int DescrittoreServer, DescrittoreClient, LunghezzaClient;
    	int NumPorta = atoi(argv[1]);
    	struct sockaddr_in serv_addr, cli_addr; /* indirizzo del server e del client */
    	char Buffer[1024] = {};
    	int rc, fd;
    	off_t offset = 0;
    	struct stat stat_buf;
    	char filename[1024] = {};
    	
    	DescrittoreServer = socket(AF_INET, SOCK_STREAM, 0);
    	if(DescrittoreServer < 0){
    		perror("Errore: creazione socket\n");
    		exit(1);
    	}
    	bzero((char *) &serv_addr, sizeof(serv_addr)); /* bzero scrive dei null bytes dove specificato per la lunghezza specificata */
    	serv_addr.sin_family = AF_INET; /* la famiglia dei protocolli */
    	serv_addr.sin_port = htons(NumPorta); /* porta htons converte nell'ordine dei byte di rete */
    	serv_addr.sin_addr.s_addr = INADDR_ANY; /* dato che  un server bisogna associargli l'indirizzo della macchina su cui sta girando */
    	
    	/* int bind(int descrittore_socket, struct sockaddr* indirizzo, int lunghezza_record_indirizzo) */
    	if(bind(DescrittoreServer, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
    		perror("Errore di bind\n");
    		close(DescrittoreServer);
    		exit(1);
    	}
    	/* int listen (int descrittore_socket, int dimensione_coda) */
    	listen(DescrittoreServer, 5);
    	LunghezzaClient = sizeof(cli_addr);
    	while(1){
    		/* int accept(int descrittore_socket, struct sockaddr* indirizzo, int* lunghezza_record_indirizzo) */
    		DescrittoreClient = accept(DescrittoreServer, (struct sockaddr *) &cli_addr, &LunghezzaClient);
    		if(DescrittoreClient < 0){
    			perror("Errore: non  possibile stabilire la connessione\n");
    			close(DescrittoreServer);
    			close(DescrittoreClient);
    			exit(1);
    		}
    
    
    		/* get the file name from the client */
        	rc = recv(DescrittoreClient, filename, sizeof(filename), 0);
        	if (rc == -1) {
          		fprintf(stderr, "recv failed: %s\n", strerror(errno));
          		exit(1);
        	}
    
    
        	/* null terminate and strip any \r and \n from filename */
    		filename[rc] = '\0';
        	if (filename[strlen(filename)-1] == '\n')
        		filename[strlen(filename)-1] = '\0';
        	if (filename[strlen(filename)-1] == '\r')
        		filename[strlen(filename)-1] = '\0';
        
        	fprintf(stderr, "received request to send file %s\n", filename);
    
    
        	/* open the file to be sent */
        	fd = open(filename, O_RDONLY);
       	 	if (fd == -1) {
        		fprintf(stderr, "unable to open '%s': %s\n", filename, strerror(errno));
        		exit(1);
        	}
    
    
        	/* get the size of the file to be sent */
        	fstat(fd, &stat_buf);
    
    
        	/* copy file using sendfile */
        	offset = 0;
        	rc = sendfile(DescrittoreClient, fd, &offset, stat_buf.st_size);
        	if (rc == -1) {
          		fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
          		exit(1);
        	}
        	if (rc != stat_buf.st_size) {
          		fprintf(stderr, "incomplete transfer from sendfile: %d of %d bytes\n", rc, (int)stat_buf.st_size);
          		exit(1);
        	}
    		strcpy(Buffer, "Dati inviati correttamente");
    		send(DescrittoreClient, Buffer, strlen(Buffer), 0);
    
    
    		char *ip_address = inet_ntoa(cli_addr.sin_addr); /* inet_ntoa converte un hostname in un ip */
    		printf("IP del client: %s\n", ip_address);		
    
    
    		close(DescrittoreClient);
    		close(fd);
    	}
    	close(DescrittoreServer);
    	return EXIT_SUCCESS;
    }
    and this is my client:

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <netdb.h>
    #include <fcntl.h>
    
    
    int main(int argc, char *argv[]){
    	
    	/* Controllo che vi sia argv[0], argv[1] e argv[2] */
    	if(argc != 4){
    		perror("./client <hostname> <numero porta> <nomefile>\n");
    		exit(1);
    	}
    
    
    	int DescrittoreClient, fd; /* descrittore del socket */
    	int NumPorta = atoi(argv[2]); /* numero di porta */
    	struct sockaddr_in serv_addr; /* indirizzo del server */
    	char nread, Buffer[1024] = {}; /* contiene i dati di invio e ricezione */
    	struct hostent *hp; /* con la struttura hostent definisco l'hostname del server */
    	char *filename = argv[3];
    	
    	hp = gethostbyname(argv[1]);
    	bzero((char *) &serv_addr, sizeof(serv_addr)); /* bzero scrive dei null bytes dove specificato per la lunghezza specificata */
    	serv_addr.sin_family = AF_INET; /* la famiglia dei protocolli */
    	serv_addr.sin_port = htons(NumPorta); /* la porta */
    	serv_addr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr)) -> s_addr; /* memorizzo il tutto nella struttura serv_addr */
    	
    	/* int socket(int dominio, int tipo, int protocollo) */
    	DescrittoreClient = socket(AF_INET, SOCK_STREAM, 0);
    	if(DescrittoreClient < 0){
    		perror("Errore nella creazione della socket");
    		exit(1);
    	}
    	/* int connect (int descrittore_socket, struct sockaddr* indirizzo_server, int lunghezza_record_indirizzo) */
    	connect(DescrittoreClient, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    	if(connect < 0){
    		perror("Errore nella connessione");
    		close(DescrittoreClient);
    		exit(1);
    	}
    	strcpy(Buffer, filename);
    	/* int send(int descrittore_socket, const void* buffer, int lunghezza_messaggio, unsigned int opzioni) */
    	send(DescrittoreClient, Buffer, strlen(Buffer), 0);
    	fd = open(filename, O_CREAT | O_WRONLY,0644);
    	if (fd  < 0) {
    		perror("open");
    		exit(1);
    	}
    	memset(Buffer,0,sizeof(Buffer));
    	/* int recv (int descrittore_socket, const void* buffer, int dimensione_buffer, unsigned int opzioni) */
    	while ((nread = read(DescrittoreClient, Buffer, sizeof(Buffer))) != 0) {
    		// e li memorizza sul file 
    		write(fd, Buffer, nread);
    			memset(Buffer,0,sizeof(Buffer));
    	}
    	recv(DescrittoreClient, Buffer, sizeof(Buffer), 0);
    	printf("Risposta del server: %s\n", Buffer);
    	close(DescrittoreClient);
    	return EXIT_SUCCESS;
    }
    The problem is that the server has to send "Dati ricevuti correttamente" but the client doesn't receive the string!
    (it receives the file but not the string)

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Why aren't you checking the return value of recv() here?
    Code:
    recv(DescrittoreClient, Buffer, sizeof(Buffer), 0);
    From the recv() man page:
    The recv() call is normally used only on a connected socket (see connect(2)) and is identical to recvfrom() with a NULL src_addr argument.

    All three routines return the length of the message on successful completion. If a message is too long to fit in the supplied buffer, excess bytes may be discarded depending on the type of socket the message is received from.

    If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and the external variable errno is set to EAGAIN or EWOULDBLOCK. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.
    Compile the client in debug mode and set a breakpoint on that line. See what, if anything, comes back from the server.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    You have to recv() in a while loop and check how much data you recieved.
    There is no guarantee that every byte will be sent to you at one time.
    Compiler MSVC++ 2010 with Code::Blocks.

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Do yourself a favor and break that down into several, smaller functions, each with a clearly define responsibility. Having your entire program in one gigantic main function will quickly get out of hand and turn your program into an unmaintainable mess.
    stahta01, Salem and laserlight like this.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    @antred: i know, this only a test program to test sendfile...in the final program i will do it
    @Ducky: i've understood..so the client has always to recv in a while loop!
    @rags: i haven't checked the return value of recv for the same reason that I wrote above to antred

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    52
    I've checked the server and the client behaviour and the server send the string but the client doesn't receive it
    i've tried with this:
    Code:
    int gg;
    while( (gg = recv(DescrittoreClient, Buffer, sizeof(Buffer), 0)) == -1) continue;
    printf("%d\n", gg);
    but it doesn't work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tcp client/server pair can send data only one way
    By evb@studioe.com in forum C Programming
    Replies: 5
    Last Post: 02-24-2012, 06:22 PM
  2. Send integers to server and server can read them?
    By rahul.hbk007 in forum C Programming
    Replies: 7
    Last Post: 05-02-2010, 10:14 AM
  3. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  4. File send app - md5 checksum different when load is put on server.
    By (Slith++) in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-31-2007, 12:23 PM
  5. Server - All Clients Send.
    By mill1k in forum Networking/Device Communication
    Replies: 13
    Last Post: 09-30-2004, 12:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21