hi guys i've problem in tcp:client/server program when i run it:
this program is to print the contents of a file in other m/c connected to LAN im sure that i've given the correct IPAdress,and also path of the file in other m/c but still its not printing the contents of the file.this is how i ran the client and server program:

tcpserver:
[root@localhost cworkspace]# gcc tcpserver.c
[root@localhost cworkspace]# ./a.out 192.168.1.19
The socket was created

workingBinding Socket
The Client 192.168.1.20 is Connected...
A request for filename /root/Desktop/scjp.txt Received..
File Open Failed: No such file or directory

tcpclient:
[root@localhost cworkspace]# gcc tcpclient.c
[root@localhost cworkspace]# ./a.out 192.168.1.20
The Socket was created
The connection was accepted with the server 192.168.1.20...
Enter The Filename to Request : /root/Desktop/scjp.txt
Request Accepted... Receiving File...

The contents of file are...


EOF
my code for server/client goes like this:

tcpserver:

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<sys/stat.h>

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>

#include<fcntl.h>



int main()

{

  int cont,create_socket,new_socket,addrlen,fd;

  int bufsize = 1024;

  char *buffer = malloc(bufsize);

  char fname[256];

  struct sockaddr_in address;



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



  printf("\nworking");



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

     recv(new_socket,fname, 255,0);

     printf("A request for filename %s Received..\n", fname);

  if ((fd=open(fname, O_RDONLY))<0)

    {
	perror("File Open Failed"); 
	exit(0);
    }

  while((cont=read(fd, buffer, bufsize))>0) 
	{

     		send(new_socket,buffer,cont,0);

  	}

  printf("Request Completed\n");

  close(new_socket);

  return close(create_socket);

}

tcpclient:

#include<sys/socket.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<unistd.h>

#include<stdlib.h>

#include<stdio.h>



int main(int argc,char *argv[])

{

  int cont,create_socket;

  int bufsize = 1024;

  char *buffer = malloc(bufsize);

  char fname[256];

  struct sockaddr_in address;



  if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)

    printf("The Socket was created\n");

  address.sin_family = AF_INET;

  address.sin_port = htons(10000);

  inet_pton(AF_INET,argv[1],&address.sin_addr);



  if (connect(create_socket,(struct sockaddr *) &address,sizeof(address)) == 0)

    printf("The connection was accepted with the server %s...\n",argv[1]);

  printf("Enter The Filename to Request : "); 
  scanf("%s",fname);

  send(create_socket, fname, sizeof(fname), 0);

  printf("Request Accepted... Receiving File...\n\n");

  printf("The contents of file are...\n\n");

  while((cont=recv(create_socket, buffer, bufsize, 0))>0) 
	{

    		write(1, buffer, cont);

 	}

  printf("\nEOF\n");

  return close(create_socket);

}

>>A request for filename /root/Desktop/scjp.txt Received..
which machine do you expect the file to reside on? If the file is on the client machine than of course the server don't find it because server is looking on its own computer.

i expect the file to be in client m/c and im sure that i've given the correct path for that file from the server m/c ...

So: client asks server to print a file that resides on the client computer. How do you think the server is going to do that? The client machine would have to be mounted on the server's computer before the server can access that file, unless of course they do a file transfer across the socket connection.

hi ancient dragon ,first of all im really sorry for making a stupid statement that file is in client m/c and i should 've been careful before posting it.here goes the scenario...

i expect the file to be in server m/c and im sure that i've given the correct path for that file from the client m/c and im(client) asking server to print the contents of a file in the server m/c ...im actually not transferring file from the server ,im just reading a file in the server m/c and then printing it in client m/c ...so is it possible to do it in tcp ??

>>im just reading a file in the server m/c and then printing it in client m/c ...so is it possible to do it in tcp ??

depends. MS-Windows not without a file transfer from server to client so that client can print it on its own machine. *nix: maybe if the server machine knows the tcp/ip address of the client's terminal.

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.