I have server.c and client.c and I want to send file to server but I don't know how to send file using it as an argument in client.
-
need context… _server_ launch _client_ as a sub-process? Are you using multi-threading (because "multithreading" tag is present)? – hexasoft Oct 15 '15 at 16:29
-
in client.c an argument should be 3 but i should change it to 4 and 4th one should be file name which will be send to server.c – John Oct 15 '15 at 16:32
-
int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[BUFFERLENGTH]; if (argc < 3) { fprintf (stderr, "usage %s hostname port\n", argv[0]); exit(1); } /* create socket */ portno = atoi (argv[2]); sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error ("ERROR opening socket"); /* enter connection data */ server = gethostbyname (argv[1]); if (server == NULL) { fprintf (stderr, "ERROR, no such host\n"); exit (1); } .. – John Oct 15 '15 at 16:33
-
@John - then your 4th parameter should be string and in client.c you treat it as a filename and do the work of sending it to the server – Les Oct 15 '15 at 16:34
-
don't put code into comments, edit your question instead – Les Oct 15 '15 at 16:34
-
yes I'm using multi-threading and should send file to server and server should either encrypt or decrypt and then save it – John Oct 15 '15 at 16:35
-
I put the link of client code Mr.Les but i don't know how to send string file as an argument.Can u modify the code above and explain please – John Oct 15 '15 at 16:40
2 Answers
There are plenty of examples on the web for client/server code using sockets. Take a look at this for one example, there are many. From there, the C standard IO libraries allow you to open, read, and write files. Search the web for examples of reading a file.
HINT: argv[] is an array of string parameters to your program. So, add another parameter when you call your program. argc will indicate the number of parameters passed (including the program name) and argv[argc-1] will be the last parameter.
When put together, and assuming you know C programming, you should be able to accomplish your task.

- 10,335
- 4
- 40
- 60
-
I just don't understand this part so what changes should be there?(( if (argc < 3) { fprintf (stderr, "usage %s hostname port\n", argv[0]); exit(1); } – John Oct 15 '15 at 16:51
-
You can take reference from the http://www.binarytides.com/server-client-example-c-sockets-linux/.
You can follow enter code here
below steps.
1)Connect to server from client.
2)Send the filename to server.
server will create a similar filename in server's home location.
3)Send file contents to server.
server will write contents to the file created in server's home folder.
A sample code snippet has given below.This describe how filename send to server.
Server side
/* server will receive the filename send by the client. client will create a filename with touch command */
char message[100];
read_size=recv(client_sock,client_message,2000,0);
printf("File Name received:%s\n",client_message);
printf("Creates a file with name:%s\n",client_message);
sprintf(message,"touch %s",client_message);
system(message);
write(client_sock ,"File Name received" , strlen(client_message));
client side
/* Client will send the name of the file to be send as an argument to the binary. argv[1] is filename to be send here. using strcpy,argv[1] is copying to char array 'filename'. Using 'send' api the client application will send the file name to server. Server will accept that name and creates the filename with touch command. Next step is to send the data to file created. */
char filename[256];
strcpy(filename,argv[1]);
if( send(sock , filename , strlen(filename) , 0) < 0)
{
puts("Send failed");
return 1;
}
memset(server_reply,'\0',sizeof(server_reply));
if( recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
}

- 26
- 3
-
Mr @Midhun Lohidakshan Please can u explain how to send filename to server?? – John Oct 15 '15 at 18:15