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
The socket was created

workingBinding Socket
The Client 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
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);

}

Dani AI

Generated

A short, practical diagnosis and fixes (building on ’s clarification and ’s follow‑up).

The server always tries to open the path on its own filesystem. Since the server printed “File Open Failed: No such file or directory”, either the exact pathname is not present/accessible on the server process, or the filename string received by the server is corrupted (missing NUL, extra bytes, CR/LF, etc.). The thread shows both issues are possible at once: the client and server use different buffer lengths and there’s no check that the name is NUL‑terminated.

Key checks to resolve this

  • Confirm the file truly exists at that absolute path from the server machine and that the server process has read permission. The perror output already indicates ENOENT (missing file) rather than EACCES (permission), so the pathname is likely wrong or garbled.
  • Log the raw length and bytes the server actually receives (print the byte count and the filename in quotes, or print it as hex) to detect trailing garbage or hidden CR/LF.
  • Always check return values for socket calls (recv/send/accept/open) and handle <=0 from recv as an error/closed connection.

A reliable, minimal protocol change

  • Send only the meaningful filename bytes from the client (length = strlen(fname)+1 to include the terminating NUL).
  • On the server, use the returned length from recv and explicitly NUL‑terminate before using the string.

Example (illustrative lines):

/* client: send only the string bytes */
send(sock, fname, strlen(fname) + 1, 0);

/* server: check length and terminate */
ssize_t n = recv(newsock, fname, sizeof(fname)-1, 0);
if (n <= 0) { /* handle error */ }
fname[n] = '\0';

Other recommendations: use width‑limited input (or fgets) on the client to avoid overflow, include appropriate headers (e.g. <arpa/inet.h>), and add defensive logging. If the filename still fails to open after these fixes, confirm the server’s working environment (chroot, different user, mounted filesystems) so the path actually refers to the expected file.

Recommended Answers

All 5 Replies

>>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.