hi

i am adjusting a socket program code to make it able to read and write on files and send and recieve packets

but i am stuck it only performs the first while loop and doesn't go to the next lines to execute them !

i dont usually program using C ...but now i have to since i am dealing with linux to write the code !

any help would be really appreciated

i have added the part of reading and writing on files and save them in buffers ...but now i need to exchange data between them

my code

/* udpserver.c */ 

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

int main()
{

/*-------------file reading and buffering------------------*/
int in_count;// counter for input files 
char* files_list[]={"input.txt"};// array for input files names to send them to sender 

for(in_count=0;in_count<1;in_count++){
//printf("\n %s \n",files_list[in_count]); // test print
}

    static const char filename[] = "input.txt";

    FILE *file = fopen ( filename, "r" );

    int row, clm;

    char buffer[128][128];

    char line[128]; // line length

    for(row=0; row<128; row++)

    for(clm=0; clm<128; clm++)

        buffer[row][clm] = '\0';

    for(row=0; row<128; row++)

        line[row] = '\0';

    if ( file != NULL ){
        row=0; 

        while ( fgets ( line, sizeof line, file ) != NULL ) // reading line by line from file 
        {
         strcpy(buffer[row], line);
         //printf("%s", buffer[row]);
         row++;
        }

        fclose ( file );
      }//end if
    else
    {
printf ("error " ) ;
      //perror ( filename ); /* why didn't the file open? */
    }//end else

/* -------------------end of file reading------------------*/


        int sock;
        int addr_len, bytes_read;
        char recv_data[1024];
        struct sockaddr_in server_addr , client_addr;


        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
            perror("Socket");
            exit(1);
        }

        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(5000);
        server_addr.sin_addr.s_addr = INADDR_ANY;
        bzero(&(server_addr.sin_zero),8);


        if (bind(sock,(struct sockaddr *)&server_addr,
            sizeof(struct sockaddr)) == -1)
        {
            perror("Bind");
            exit(1);
        }

        addr_len = sizeof(struct sockaddr);

    printf("\nUDPServer Waiting for client on port 5000");
        fflush(stdout);

    while (1)// i think the problem is here 
    {

          bytes_read = recvfrom(sock,recv_data,1024,0,
                        (struct sockaddr *)&client_addr, &addr_len);


      recv_data[bytes_read] = '\0';

          printf("\n(%s , %d) said : ",inet_ntoa(client_addr.sin_addr),
                                       ntohs(client_addr.sin_port));
          printf("%s", recv_data);
      fflush(stdout);

        }


          sendto(sock, files_list[in_count], strlen(files_list[in_count]), 0,
              (struct sockaddr *)&client_addr, sizeof(struct sockaddr));// sending file name to the cilent
// recieve

          sendto(sock, buffer[0], strlen(buffer[0]), 0,
              (struct sockaddr *)&client_addr, sizeof(struct sockaddr));//sending data inside the file 


        return 0;
}

now i still have to put another recieve ...but its not getting inside the send above it so i left it as comment to add it later ..

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

int main()
{
int sock;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024];
char C_buff[1024] ;
int bytes_read;
char *name_files[100];
int addr_len;

host= (struct hostent *) gethostbyname((char *)"127.0.0.1");


if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);

   while (1)
   {

    printf("Type Something (q or Q to quit):");
    gets(send_data);

    if ((strcmp(send_data , "q") == 0) || strcmp(send_data , "Q") == 0)
       break;

    else
       sendto(sock, send_data, strlen(send_data), 0,
              (struct sockaddr *)&server_addr, sizeof(struct sockaddr));

   }

       recvfrom(sock,name_files,1024,0,
                        (struct sockaddr *)&server_addr, &addr_len);
       scanf("%s", name_files[0]);//enter file name choosen 


       sendto(sock, name_files[0], strlen(name_files[0]), 0,
              (struct sockaddr *)&server_addr, sizeof(struct sockaddr));//sending the shosen file name 

       recvfrom(sock,C_buff,1024,0,
                        (struct sockaddr *)&server_addr, &addr_len);//recieve files contents from server



//-----------------------writing on a file from buffer---------------

//char * array [] ={"fnj" , "dfg","fgg"};
FILE * fout ;

     int i;
     fout = fopen("out.txt","w");      /* open the file in  */
     //for (i=0; i<3; i++)
          fprintf(fout,"%s\n",C_buff); /* write */ 

     fclose(fout);                       /* close the file */

     return 0; 



}

thanks :)

while (1)// i think the problem is here

That will loop forever. Change the while loop's condition, or put a break in it where you want it to stop looping.

i tried to put break it didn't work ...if remove while i get a segmentation fault, and i need it so the server keep listening if the client sends a request

i think i am gonna rewrite it again

i tried to put break ...it didn't work ...if i remove while(1) i get a segmentation fault, and i need it so the server keep listening if the client sends a request

i think i am gonna rewrite it again

So you need the loop to loop forever, but you also need other code to run?

i am running both of them server and client ,so the client will stop the server, once client sends a text it will appear on the server's screen ..but now i am trying to exchange files ...simulating the uploading and downloading ...after thinking i will remove the while loop and make it simple send and recieve :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.