0

I have to write program using ANSI C which allows to copy directories [files and / or catalogs] between two computers in network [client - server program] using sockets. On both computers is installed linux. I CAN'T using system commands for this.

To explain how it should work: In terminal I type:

name_of_my_program /path_of_file_or_catalog computer_ip/path_where_to_copy

And it should copy files or whole catalog to this place.

I don't have any idea how to move on directories without system commands. I don't ask you for code. Just ideas or litle samples of code - you know, just "framework". How to do this? I don't have any problems with write program using sockets. I already have that.

I will be grateful for ideas and tips. Thanks in advance!

raziel
  • 47
  • 7

1 Answers1

1

Write a function which sends a file including its path and name using open(), read(), close(), socket(), connect(), send().

Write a function which receives a file including its path and name using socket(), bind(), listen(), accept(), recv(), open(), write(), close().

Write a function which scans a directory tree using opendir(), readdir(), closedir().

Combine everything and your done.

alk
  • 69,737
  • 10
  • 105
  • 255
  • OK, I have one more problem. I'm sending [char *c] via socket. For example: "Hi!", and on the server I receve "Hi7". How to avoid that? I'm doing this right this: `send(sockFD, exPath, strlen(exPath), 0)` `recv(connFD, destPath, MAXLINE, 0)` Probably the last symbol is the nullterminated symbol. But how to ignore him? – raziel Sep 03 '13 at 12:57
  • @raziel: You might like to read about how C uses character array to represent "strings". – alk Sep 03 '13 at 13:01
  • @raziel: You can not expect that the number of calls to `send` match the number of calls to `recv`. TCP is a streaming protocol. All that is assured to match is the **total** number of bytes tramsitted during **one** connection. So make sure that on the sending side as well as on the receiving side you always keep track of how much characters actually have been sent and recived. This is done by checking the value returned by `send` and `recv`. – alk Sep 03 '13 at 13:05
  • I've checked. I'm sending 12bytes, and receving 12bytes... I swear I will read about C and "strings" represents, but at this moment i dont have enough time for this. Could you suggest some solution? Thanks in advance...[and no, this is not any homework] – raziel Sep 03 '13 at 13:15
  • 1
    @raziel: As it's always a good idea to not use uninitialised variables: Init the receivers buffer with all `0` before having `recv()` write to it. – alk Sep 03 '13 at 13:24