Hi,
I am newbie to network programming in C. I want to implement a file transfer program in C on a Linux environment .
I am sending the file name to the server and tries to print it on the server. But the server doesn't print it. I think I am not using the send function well... Can anybody helps me in this regard? the code is below...

1. client code

//Including necessary header files

#include  <stdio.h>
#include  <stdlib.h>
#include  <unistd.h>
#include  <errno.h>
#include  <string.h>
#include  <netdb.h>
#include  <sys/types.h>
#include  <netinet/in.h>
#include  <sys/socket.h>

#define MAXDATASIZE 500  // The Size Of Data Buffer


int main(int argc, char *argv[])
{
    //Declaring some variables
    int sockfd;
    int numbytes;
    int port;
    char buf[MAXDATASIZE];
    uint16_t length;
    char file_name[10];
    
    //Declaring Address Structures
    struct hostent *he;
    struct sockaddr_in serverAddress; 

    //Checking the Command Line Arguments
    if (argc != 3) {
         fprintf(stderr,"usage: client hostname\n");
         exit(1);
    }

    //Get The IP Address of the Server From Command Line Arguments

    serverAddress.sin_addr.s_addr = inet_addr(argv[1]);
    he=gethostbyaddr((char *) &serverAddress.sin_addr.s_addr,sizeof(serverAddress.sin_addr.s_addr),AF_INET);
   
    //Get The Server Port From The Command Line Arguments	
    port=atoi(argv[2]);

     //Creating A Socket

     if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
         perror("socket");
         exit(1);
     }
   
     //Filling the Server Address Structure
     serverAddress.sin_family = AF_INET;    
     serverAddress.sin_port = htons(port); 
     serverAddress.sin_addr = *((struct in_addr *)he->h_addr);

     //Setting The Whole Structure To Zero
     
     memset(serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);

     //Connecting To The Server

     if (connect(sockfd, (struct sockaddr *)&serverAddress,sizeof (serverAddress)) == -1) {
         perror("connect");
         exit(1);
     }
    
    //Receiving Some Data From Server
    if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
         perror("recv");
         exit(1);
     }

   //Terminating The Received Data With Null
   buf[numbytes] = '\0';
	
    //Printing The Received Data
     printf(" %s",buf);

     //Acquring the file name from console
     printf("Enter the file name: ");
     fgets(file_name, 10, stdin);

     

     bzero(&buf,sizeof(buf));	
  
     sprintf(buf,"%s",file_name);

	send(sockfd,buf,10,0);

    //Closing The Socket
     close(sockfd);
     return 0;
 }

2. Server Code

//Including necessary header files

#include  <stdio.h>
#include  <stdlib.h>
#include  <unistd.h>
#include  <errno.h>
#include  <string.h>
#include  <sys/types.h>
#include  <sys/socket.h>
#include  <netinet/in.h>
#include  <arpa/inet.h>
#include  <sys/wait.h>
#include  <signal.h>

#define SERVER_PORT 9999 // The Well Know Server Port
#define BACKLOG 10    // Maximum Pending Connections
#define MAX_SIZE 500 // The Size Of Data Buffer

 int main(void)
 {
	
     //Declaring some socket handlers
     int listenSocket;
     int activeSocket;

     //Declaring Some Error Checking Bits
     int numberOfBytes;
     int socketOption;
     int bindCheck;
     int listenCheck;
     int sendCheck;
     int yes=1;

     //Declaring some size variables
     char buffer[MAX_SIZE];
     char welcomeMessage[50];
     socklen_t client_size;
	
     //Declaring Address Structures for Client and Server
     struct sockaddr_in serverAddress; 
     struct sockaddr_in clientAddress; 
     
       
     //Opening a Socket in Passive Mode
     listenSocket = socket(AF_INET, SOCK_STREAM, 0);

     //Doing Error Checking
     if(listenSocket==-1){

         perror("socket");
         exit(1);
     }
     
     //Setting the Socket Option
     socketOption=setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

     //Doing Error Checking   
     if(socketOption==-1){

         perror("setsockopt");
         exit(1);
     }

     //Filling the Server Adress Structure
     serverAddress.sin_family = AF_INET;         
     serverAddress.sin_port = htons(SERVER_PORT);     
     serverAddress.sin_addr.s_addr = INADDR_ANY; 

     //Setting the Whole Structure To Zero
     memset(serverAddress.sin_zero, '\0', sizeof (serverAddress.sin_zero));

     //Binding The Listening Socket to the server well known port
     bindCheck=bind(listenSocket, (struct sockaddr *)&serverAddress, sizeof (serverAddress));

     //Doing Error Checking
     if(bindCheck==-1){
	
         perror("bind");
         exit(1);
     }

     //Start Litsening for Connections on The Listening Socket
     listenCheck=listen(listenSocket, BACKLOG);

      //Doing Error Checking
      if(listenCheck==-1){

         perror("listen");
         exit(1);
     }
     
     //The Infinite For Loop Starts
     for(;;) { 

	//Size of The incoming Client Adress Structure
         client_size = sizeof (clientAddress);

	 //Accepting the incoming connection from client and creating another socket for it
         activeSocket = accept(listenSocket, (struct sockaddr *)&clientAddress,  &client_size);

	//Doing Error Checking
	if(activeSocket==-1){

             perror("accept");
             continue;
         }
	
       /*  printf("server: got connection from %s\n %d\n", \
             inet_ntoa(clientAddress.sin_addr),activeSocket);*/
	
	//Preparing Welcome Message To The Client

	 sprintf(welcomeMessage,"Welcome User: IP Address-%s Port-%d\n",inet_ntoa					(clientAddress.sin_addr),activeSocket);	

	//Sending A Message To Client
        sendCheck=send(activeSocket, welcomeMessage, MAX_SIZE, MSG_DONTWAIT);

	//Doing Error Checking
	if(sendCheck==-1){

                 perror("send");
   	}

	//Receving Some Text from Client
	numberOfBytes=recv(activeSocket, buffer,MAX_SIZE, 0);

	//Doing Error Checking
	if(numberOfBytes==-1) 
	{
         perror("recv");
         exit(1);
     	}

	//Terminating The Received Data with Null
       buffer[numberOfBytes] = '\0';

       //Printing The Received Data on Console
       printf("Received: %s",buffer);

      
     }

  //Closing the Socket which communicating with Client
  close(activeSocket);
	     
return 0;
 }

Thanks

1. Do these code valid?

port=atoi(argv[2]);

I suppose atoi() isn't ANSI C
You could use this for exchange

sprintf(argv[2], "%d", port)

2.
This syntax is risky

numberOfBytes=recv(activeSocket, buffer,MAX_SIZE, 0);

It will be safer if you use these one

numberOfBytes=recv(activeSocket, buffer,MAX_SIZE-1, 0);

Forgive me whether none of of my answer help you
Even I'm not sure with my answer :p

Thanks,

Your help was fruitful.. Though sprintf doesn't seems to work with my code.... (not sure why).....

You could read the manual for every function to know why it doesn't work.
Just ask Mr. Google and Mrs. Wikipedia :p

This question has already been answered. Start a new discussion instead.