Socket Programming Send File


 
Thread Tools Search this Thread
Top Forums Programming Socket Programming Send File
# 1  
Old 06-23-2009
Error Socket Programming Send File

Hello my friends;

Look at this 2 program:

Client:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main ( int agrc, char *argv[] )
{
	int Socket;
	struct sockaddr_in Server_Address;	
	Socket = socket ( AF_INET, SOCK_STREAM, 0 );
	if ( Socket == -1 )
	{	
		printf ("Can Not Create A Socket!");	
	}
	int Port ;
	Port = atoi(argv[2]);	
	Server_Address.sin_family = AF_INET;
	Server_Address.sin_port = htons ( Port );
	Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
	if ( Server_Address.sin_addr.s_addr == INADDR_NONE )
	{
		printf ( "Bad Address!" );
	}	
	connect ( Socket, (struct sockaddr *)&Server_Address, sizeof (Server_Address) );


        char *path;
        char *filename;
        path = "/home/hosein/Desktop/Project-30-3/Scan.pdf";
        filename = strrchr(path, '/') + 1;
        send(Socket,filename,sizeof(filename),0);

        FILE *in = fopen("1.tar.gz","r");
        char Buffer[2] = "";
        int len;
        while ((len = fread(Buffer,sizeof(Buffer),1, in)) > 0)
        {            
            send(Socket,Buffer,sizeof(Buffer),0);            
        }
        send(Socket,"Hi",sizeof(Buffer),0);

        char Buf[BUFSIZ];
	recv(Socket, Buf, BUFSIZ, 0);
        if ( strcmp (Buf,"ACK") == 0  )
        {
            printf("Recive ACK\n");
        }        
        close (Socket);
        fclose(in);
	return 0;	
}

Server:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main ( int agrc, char *argv[] )
{
	/******** Program Variable Define & Initialize **********/
	int Main_Socket; 	// Main Socket For Server
	int Communication_Socket; // Socket For Special Clients
	int Status; // Status Of Fucntion
	struct sockaddr_in Server_Address; // Address Of Server
	struct sockaddr_in Client_Address; // Address Of Client That Communicate Whit Server
	int Port ;
        char Buff[100] = "";
	Port = atoi(argv[2]);
	printf ("Server Communicating By Using Port %d\n", Port);
	/******** Create A Socket To Communicate With Server **********/
	Main_Socket = socket ( AF_INET, SOCK_STREAM, 0 );
	if ( Main_Socket == -1 )
	{
		printf ("Sorry System Can Not Create Socket!\n");
	}
	/******** Create A Address For Server To Communicate **********/
	Server_Address.sin_family = AF_INET;
	Server_Address.sin_port = htons(Port);
	Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
	/******** Bind Address To Socket **********/
	Status = bind ( Main_Socket, (struct sockaddr*)&Server_Address, sizeof(Server_Address) );
	if ( Status == -1 )
	{
		printf ("Sorry System Can Not Bind Address to The Socket!\n");
	}
	/******** Listen To The Port to Any Connection **********/        
	listen (Main_Socket,12);	
	socklen_t Lenght = sizeof (Client_Address);
        while (1)
        {
            Communication_Socket = accept ( Main_Socket, (struct sockaddr*)&Client_Address, &Lenght );
            if (!fork())
            {

                char CH[8];
                recv(Communication_Socket, CH, sizeof(CH), 0);
                printf("%s\n",CH);
                
                FILE *fp=fopen("sample.tar.gz","w");
                while(1)
                {
                    char Buffer[2]="";
                    if (recv(Communication_Socket, Buffer, sizeof(Buffer), 0))
                    {
                        if ( strcmp (Buffer,"Hi") == 0  )
                        {
                            break;
                        }
                        else
                        {
                            fwrite(Buffer,sizeof(Buffer),1, fp);
                        }
                    }
                }
                fclose(fp);
                send(Communication_Socket, "ACK" ,3,0);
                printf("ACK Send");
		exit(0);
            }
        }
	return 0;
}

If I declare CH[x] ( x <= 8 ), everything work right and file will send in a true way. but with CH[x] ( x > 8 ) I can't send file! what's the problem?Smilie
# 2  
Old 06-23-2009
Let me guess, you're compiling on a 64bit architecture.

sizeof() is a compile-time instruction and will, in your case, return the size of the pointer for the character array. Try declaring the a macro for the size of the array & use that macro for the recv() call.
# 3  
Old 06-23-2009
No! I don't use 64 bit architecture! I try strlen() instead of sizeof()! there is problem JUST in receiving File Name (recv(Communication_Socket, CH, sizeof(CH), 0);
I don't know why! Smilie
# 4  
Old 06-23-2009
OK then, what exactly do you mean by "can't send file!"? Does recv() block, is the file corrupted, won't the program end?
# 5  
Old 06-23-2009
Is it possible for you to test and compile that 2 program?
Thanks for your kindness. Smilie
# 6  
Old 06-25-2009
Quote:
Originally Posted by pludi
sizeof() is a compile-time instruction
I'm nit-picking, but it isn't always the case that sizeof is compile-time.

C99 allows sizeof to be runtime for VLA's.

---------- Post updated at 09:57 AM ---------- Previous update was at 09:56 AM ----------

To the OP: it would be a good idea to check the return values from all the socket and i/o functions you're using... you're flying blind, not knowing what is happening.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Socket Programming file sending

Hello everyone,, I am doing one socket programming.Is it possible to send one file from client to server without using buffer??.I am sending tar files. Thanks in Advance (9 Replies)
Discussion started by: andrew.paul
9 Replies

2. AIX

Sample C program to Send/Recieve a file using Socket

Hi All, I urgently need a Sample C program to Send/Recieve a file using Socket. Thanks Sara (1 Reply)
Discussion started by: saraperu
1 Replies

3. Programming

Socket programming

Hi everyone, I'm new to this forum. I'm working on new project for last few days and this forum already helped me on couple of occasions. I don't have any prior experience with network programming so I'll appreciate any advise given. I'm trying to do the following: 1. open user... (2 Replies)
Discussion started by: _thomas
2 Replies

4. Programming

Help with socket programming in C

hi guys i got this code trying to make connection between the server and multi clients but when i do ./server i got message server waiting then when i run ./client it says client 1 nosuch file i dont know whats that should i use any argument plz help how to compile and run and whats the expected... (1 Reply)
Discussion started by: kedah160
1 Replies

5. UNIX for Advanced & Expert Users

socket programming

can we send udp message to a destination ip address .. without having an ip address configured in our machine using recvfrom ? (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

6. Programming

Store file into a buffer to send it through a socket

Hello, I'm doing a very simple program which reads a file and sends whatever is in the file through a socket. Like the program "file2cable". Let's say i have a file containing the following, which is a hex dump of an ARP request frame: ff ff ff ff ff ff 00 1b 24 79 5a 73 08 06 00 01 08... (5 Replies)
Discussion started by: semash!
5 Replies

7. Programming

help regarding socket programming

i m using sockets for setting up a connection between a server and a client. When the clients gets connected to the server, its ip is conveyed to the server through one of the predefined structures in c library... i save this ip address in an array....1st client's ip address goes to the zeroth... (1 Reply)
Discussion started by: abmxla007
1 Replies

8. IP Networking

socket programming

Hello Everyone Iam working on tcp/ip programming.with some time interval server has to send data.client has to close the connection and to open the connection between the time interval.this is the scenario when iam closing the connection in client side the connection terminates.how to... (1 Reply)
Discussion started by: sureshvaikuntam
1 Replies

9. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

10. Programming

Socket programming

Suppose i am writing a C program which is going to use Socket calls. I want to use a Unix port for my Socket. How can i determine a port which is not already in use? (1 Reply)
Discussion started by: Nadeem Mistry
1 Replies
Login or Register to Ask a Question