Q:
Respected Sir,
is it possible to pass a text file from server to client using tcp
socket in C?.
Provide me with an example which should run under Linux
- *NAME-DELETED*
A:
It depends on what you call a "text file". It is possible. An extremely
good example is the Apache web server available in source code.
However what you really need is (I guess) is a good socket programming
tutorial. Try this:
http://www.google.com/search?q=socket+programming+tutorial&hl=hu
The first link in the list is a good tutorial.
Regards,
Peter
=========================================================
respected sir,
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
main()
{
int cont,create_socket,new_socket,addrlen;
int bufsize = 1024;
char *buffer = malloc(bufsize);
struct sockaddr_in address;
FILE *fpt,*fpt1;
char temp[80];
char *fst_ptr = NULL;
fpt = fopen("c:/fu.txt","r+");
printf("\x1B[2J");
if(fpt == NULL)
printf("Access Denied File Cannot be Opened");
fst_ptr = temp;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
printf("The socket was created\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(15000);
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) ==
0)
printf("Binding Socket\n");
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr
*)&address,&addrlen);
if (new_socket > 0) {
printf("The Client %s is connected...\n",inet_ntoa
(address.sin_addr));
/* for(cont=1;cont<5000;cont++)
printf("\x7"); */
}
do
{
while(!feof(fpt))
{
fgets(temp,81,fpt);
send(new_socket,temp,bufsize,0);
printf("Message recieved: %s\n",fst_ptr);
}
} while(strcmp(buffer,"/q"));
fclose(fpt);
close(new_socket);
close(create_socket);
}
doubt:
this is my server side code. whlie running under linux , its not able
to open the file but binding , connection to the client are taking place
properly.
/* if(fpt == NULL)
printf("Access Denied File Cannot be Opened"); */
all time "Access Denied File Cannot be Opened" is printed(i had a file
named fu.txt in hard disk).
when i run the prg in turbo c its working properly without error. tell
me ,whether linux got someother way of opening the file.
pls , help to solve this problem.
Q:
A:
The name "c:/fu.txt" is not a valid file name under UNIX. This is the
reason. You can not make a file with that name. Use a different file name
that does not contain any "drive letter", which is typically DOS like file
name. For example you can name the file "fu.txt" that is going to be a
file in the actual directory.
There should not be other problem. Linux opens the files (from C) the same
way as DOS if you use fopen.
Regards,
Peter
[ back to toc ]