Find the answer to your Linux question:
Results 1 to 2 of 2
hi all this is wellknown question about file sending. Code: #include <gtk/gtk.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <linux/sockios.h> #include <string.h> #include <math.h> #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> ...
  1. #1
    Linux Newbie
    Join Date
    Feb 2007
    Location
    hyderabad, india
    Posts
    247

    how to send files

    hi all
    this is wellknown question about file sending.

    Code:
    #include <gtk/gtk.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <linux/sockios.h>
    #include <string.h>
    #include <math.h>
    #include <dirent.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <unistd.h>
    #include <syslog.h>
    these for global declarations
    Code:
    GtkWidget *dialog;
    GtkWidget *parent_window;
    gint result;
    gchar *file;
    gchar *ind;
    gchar *post=NULL;
    void server1();
    void server2();
    int n,i=2010,j=2030;
    gchar *item=NULL,*item2=NULL,*item1=NULL, *text=NULL;
    gchar *text1=NULL,*post1=NULL;
    when we pressed okbutton,singal will comes in to this
    Code:
    static void file_ok_sel()
    {
            g_print ("%s\n",file );
    
    	item1= g_strconcat  (file,NULL);
    	post=g_strconcat  (file,NULL);
    
      /*  pthread_t thread1;
        int  iret1;
        iret1=pthread_create( &thread1, NULL, server2, NULL);
        //pthread_join( thread1, 0);
        pthread_detach( thread1);*/
    	server2();
    
    }
    main function
    here i declared the gtk window and signal of okbutton
    Code:
    int main( int   argc,
              char *argv[] )
    {
        
        gtk_init (&argc, &argv);
        
    dialog = gtk_file_chooser_dialog_new ("File Selection", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);
    
    
    result = gtk_dialog_run (GTK_DIALOG (dialog));
    
    switch (result)
    {
    case (GTK_RESPONSE_OK):
    
     file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
     g_print("file=%s\n",file);
    file_ok_sel();
      /* Use the filename here. */
    default:
      gtk_widget_destroy (dialog);
    }
    
        gtk_main ();
        return 0;
    }
    and here i declared sever and client connections
    Code:
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    void server2()
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
      // int i=2030;
        char buffer[256];
      portno = i;
    printf("i=%d\n",i);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname("192.168.xxx.xxx");
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
    
    
        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(portno);
        if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) 
    {
    
    }
    
    else
    {
      post=g_strconcat(post,NULL);  
         g_print ("post=%s\n",post );  
       n = write(sockfd,post,strlen(post));
        if (n < 0) 
             error("ERROR writing to socket");
       
    g_print("post======%s \n",post);
    
    }
    i++;
     server1();
       
    }
    void server1()
    {
    int sockfd, newsockfd, portno, clilen;
         char buffer[256],*item;
         struct sockaddr_in serv_addr, cli_addr;
             sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if (sockfd < 0) 
            error("ERROR opening socket");
         bzero((char *) &serv_addr, sizeof(serv_addr));
         portno =j;
    printf("j=%d\n",j);
    bzero(item1,strlen(item1));
         serv_addr.sin_family = AF_INET;
         serv_addr.sin_addr.s_addr = INADDR_ANY;
         serv_addr.sin_port = htons(portno);
         if (bind(sockfd, (struct sockaddr *) &serv_addr,
                  sizeof(serv_addr)) < 0) 
                  error("ERROR on binding");
         listen(sockfd,5);
         clilen = sizeof(cli_addr);
         newsockfd = accept(sockfd, 
                     (struct sockaddr *) &cli_addr, 
                     &clilen);
    printf("j=%d\n",j);
    j++;
         if (newsockfd < 0) 
              error("ERROR on accept");
         
         n = read(newsockfd,item1,1024);
         if (n < 0) error("ERROR reading from socket");
         printf("Here is the message: %s\n",item1);
       
         if (n < 0) error("ERROR writing to socket");
    item1[strlen(item1)]='\0';
    
    int m;
    
    g_print("%s \n",item1);
    
    }
    to compile the code
    Code:
    gcc fileselect.c -o fileselect `pkg-config gtk+-2.0 --cflags --libs'
    it is is successfully compiled.
    but it is unable to send a file to others.

    help me

    thank you in advance

  2. #2
    Linux Newbie
    Join Date
    Feb 2007
    Location
    hyderabad, india
    Posts
    247

    thank you for quick replay

    hi
    before this i tried
    scp
    it is asking ipaddress and password,so, i stopped that.
    now am trying by sockets..
    can you please help me

    thank you in advance

Posting Permissions

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