"File transfer through Socket Programming in C"
Hi,
I am using following code for file transfer throuch socket programming in C. Separately when i am running server and client code, everything is fine. i.e (server.c and client.c) But when i try to include the code for server and client in a single code by calling self defined function. It's not working An empty file of zero bytes in being created. Here is the code. /* client_server_function_test.c*/ #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #define PORT 2200 void Socket_server(); void Socket_client(); main() { puts("Server called"); Socket_server(); //Socket_client(); return(0); } void Socket_server() { int sock1,sock2,clength,s; sock1 = socket(AF_INET,SOCK_STREAM,0); // printf("%d\n",sock1); struct sockaddr_in serv,cli; puts("Trying Connection"); serv.sin_family = AF_INET; serv.sin_port = htons(PORT); serv.sin_addr.s_addr = inet_addr("127.0.0.1"); bind(sock1,(struct sockaddr *)&serv, sizeof(serv)); listen(sock1,5); clength = sizeof(cli); char buf[50]; puts("Trying again"); Socket_client(); sock2 = accept(sock1,(struct sockaddr *)&cli,&clength); printf("\n Client Connected\n"); FILE* fp = fopen("1342181561","r"); while(!feof(fp)){ // bzero(buf,sizeof(buf)); fread(buf,sizeof(char),50,fp); printf("%s",buf); write(sock2,buf,50); puts("Trying to write"); } write(sock2,"quit1234",50); fclose(fp); } void Socket_client() { int sock3; sock3 = socket(AF_INET,SOCK_STREAM,0); struct sockaddr_in serv; serv.sin_port = htons(PORT); printf("%x %x\n",PORT,htons(PORT)); serv.sin_family = AF_INET; serv.sin_addr.s_addr = inet_addr("127.0.0.1"); printf("client connecting\n"); connect(sock3, (struct sockaddr *)&serv,sizeof(serv)); // return(sock3); puts("Connected"); char buf[50]; FILE* fp1 = fopen("client_new.txt","w"); // return(sock3); while(1) { puts("File opened for writing"); // bzero(buf,sizeof(buf)); read(sock3,buf,50); puts("Start writing"); if(strcmp(buf,"quit1234")==0) { break; } } fprintf(fp1,"%s",buf); } //return(sock1); fclose(fp1); } Here's the output: surendra@ubuntu:~/Linuxcodes/File_handling$ gcc client_server_function_test.c surendra@ubuntu:~/Linuxcodes/File_handling$ ./a.out Server called Trying Connection Trying again 898 9808 client connecting Connected File opened for writing /File_handling$ ls -lrt -rw-r--r-- 1 0 2012-07-17 21:57 client_new.txt Any suggestions. |
Hi there,
It's hard to tell where things are going wrong as you don't seem to be checking the return codes from your function calls. I'd strongly recommend that you check the return codes of all function calls (like bind, listen, connect, accept, write, read, etc), and handle any errors appropriately. I think a large part of the problem is that this code is single threaded. For the client and server to work, they each need a turn to do a bit of work, i.e. the server sends 50 bytes, the client reads them, then the server gets a turn again to send the next 50 bytes. In your code this can't happen - your server is opening the socket, then calling the client and waiting for it to finish, before trying to accept the connection and send the data. The code is most likely stuck at the read() in Socket_client(), as the connection hasn't been accept()ed, and there is no data to read. Is there a specific reason you want both the server and client in a single program? Good luck! |
"File transfer through Socket Programming in C"
I am implementing an application to send messagea with in an Intranet.
So far i have created a file with current timestamp that holds the message I have transferred the message locally within the system by writing and running separate for codes for client and server. But now i want entire application within a single program. So , i was trying to call server and client through user defined function. Is it possible. Can u help me with code or any useful link. Thanks |
Quote:
Code:
... |
just try to print the return value of read() & write() functions, it will help to check whether some text has been processed or not.
|
I have edited the code but it's still the same ,it's creating the file but unable to write into it.
ubuntu:~/Linuxcodes/File_handling$ ./a.out Trying Connection Trying again 898 9808 client connecting Connected File opened for writing 898 9808 client connecting Connected File opened for writing ^C @ubuntu:~/Linuxcodes/File_handling$ ls -lrt -rw-r--r-- 1 0 2012-07-18 08:06 client_new.txt |
Its good if you consider handling error codes, You are getting stuck in something before you read/write, but your printed output is unable to express where the problem is.
|
Quote:
Here's a functional "copy" of your code; note that there are still areas which are still not "safe" from errors. I'll leave it as an exercise for you to find these areas. Code:
/* client_server_function_test.c*/ |
Hi again,
Just one more observation I forgot to mention erlier: in the original code you use fread() and write() to read & send fixed blocks of 50 bytes of data in the server. In the client you try and read() the same 50 bytes. All of this works on fixed blocks of raw data, not strings (no null termination). You then try and fprintf() it to the file on the client, though. This is bound to cause serious problems. In addition to doing the much needed error checking, dwhitney67's example above uses fwrite() instead, and checks the number of bytes read/send/received/written, which solves this problem (and a few others). |
Quote:
|
Thanks a lot.
It's working now |
Just one query, you have not passed IP address in the code .Like we generally pass
serv.sin_addr.s_addr = inet_addr("127.0.0.1"); for tranferring file on local machine. U have used serv.sin_addr.s_addr = htonl(INADDR_ANY); Can u please explain. |
Quote:
Bear in mind, that per the man-page for inet_addr(), its use is not recommended. Use of either inet_aton(), inet_pton() or getaddrinfo() is preferred. |
All times are GMT -5. The time now is 06:36 PM. |