LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2007, 09:16 AM   #31
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908

The peer node has to tell you it's address or IP name. The user should be able to get this information from his system. It may not be a fixed address, as many Internet Service Providers assign different IPs each time the node boots, or after some arbitrary time period. The node may have a fixed name, even in the absence of a fixed IP, and the user at that node should be able to find out that name. A dynamic DNS service may be of value for this. If you are using Linux, the easiest way it find the IP assigned to an interface is with ifconfig. If there is one or more firewalls between the connecting hosts, you may have to modify the firewall rules to accommodate your software, and your software may need to accommodate the firewall(s), by sending client requests to the firewall IP rather than the end host.

The type of messages sent through TCP connections or UDP datagrams is completely arbitrary. The communications layer knows only how to build & maintain connections, and transport data through the connections. It will be completely up to you and your software to figure out how to transfer files through that layer. In the case of TCP, the connection can be kept open for as long as required to perform the transfer(s). That is, in theory, at least. In practice, connections sometimes get broken, and your software will have to recognize this, and handle rebuilding the connection as appropriate. For a 10GB file transfer, you should expect this circumstance to arise. You will want your software to be able to resume a transfer after rebuilding a new connection. You don't want to restart a file transfer after 9.9GB of data have already been sent.

TCP &/or UDP is undoubtedly used for instant messaging.

--- rod.
 
Old 04-04-2007, 10:40 PM   #32
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
thank you for quick replay

hi
i tried like this is this the correcte way to transfer a file

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>

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;




/* Get the selected filename and print it to the console */
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();

}

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;
}


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("axillsearch.com");
    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);

}
and compile like
gcc fileselect.c -o fileselect `pkg-config gtk+-2.0 --cflags --libs

can you please help me in this way
thank you in advance
 
Old 04-04-2007, 11:39 PM   #33
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Why don't you tell us? Does it work, or not?

Posting a long piece of code like that, not even in CODE tags so formatting is all over the map, and expecting someone to debug it by inspection is asking a lot. Why don't you break out the elements, like the file transfer parts, into separate, smaller functions, and test and debug those as individual pieces. Then you can focus on a smaller subset of the overall problem, and also modularize your code more. For testing, use input arguments from argc/argv or read from stdin. Most of the people who read your code will not understand the mixture of GTK GUI code (assuming that is what it is) and the relatively straightforward IP sockets components. Divide and conquer.

Create separate client side and server side stubs, and test & develop slowly, building on to what you get working as you you procede. For testing and developing networking code, I find tools like C-Kermit, telnet, ethereal/wireshark, and especially netcat indispensible. When developing a package that has two interoperable components, it is helpful to have one side of the equation that you can rely on to be fully functional. If the overall program doesn't work, it is difficult to test & debug one end with a peer component which itself may be buggy. The tools I mentioned may be already installed with your distro, but if not, are easily obtained online.

Having said that, nothing immediately jumps out at me as being clearly wrong, although the code is very difficult to read as-is. Some things I find a bit off-putting: too few comments, poor use of variable names, hardcoded IP names, unmodular architecture. Does it compile and link cleanly? When you run it, what does it do or not do?

--- rod.

Last edited by theNbomr; 04-04-2007 at 11:40 PM.
 
Old 04-05-2007, 12:08 AM   #34
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
thank you for quick replay

i included these for, gtk,tcp, sever client connections
sorry for that.
actually i dont know how to send this code so,


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);

}
it is is successfully compiled.
but it is unable to send a file to others.

help me

thank you in advance
 
Old 04-05-2007, 10:20 AM   #35
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, you should break the socket level code out into separate functions, and create simple wrappers with main() functions that exercise the individual client and server components. Build these as standalone programs, and test & debug them. Use netcat as the peer, and see that connections are being properly created and that data is being transferred. Once these components are functional, move them back into your target application. Make most or all configurable items such as IP addresses, ports, filenames, and the like into function parameters, and start the stub application by passing them as commandline arguments, which your main() function will parse from argc/argv.

So you will start out with a simple server & simple client. Run them independently, giving runtime parameters such as IP, port, and payload like:
Code:
client 12.34.56.78 1234 "This is a string to send"
Once you see the 'string to send' arriving at the netcat server, move on to sending a file instead of a string. This is the sort of progression that will give you some success and learning experience. Once the client is working, do the same for the server side. Then move the working code back into your overall package.

I'm sorry I don't know much about GTK applications, except that most of what goes on as application-specific code happens as callback procedures, and this often makes it difficult to test non-trivial callback code. There may be issues regarding how much time can be spent in a callback, without disrupting the end-user interaction with the program, similar to the constraints usually applied to interrupt processing.

You should not expect anyone in this forum to download your code, build it, debug it and send you the working result. You will learn little or nothing from that, and by your actions, the only thing to be gained from this endeavor is the learning experience.

--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
transfer dvds from ogg file to vod file kingfish217217 Linux - Software 3 02-19-2007 06:19 PM
File Transfer ~P3~ Linux - Newbie 2 05-19-2006 04:25 PM
File Transfer ~P3~ Linux - Laptop and Netbook 1 05-19-2006 09:53 AM
file transfer from vm phfowol Debian 3 10-23-2004 12:39 AM
File transfer Joe47 Linux - Newbie 2 02-09-2003 04:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration