By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,192 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,192 IT Pros & Developers. It's quick & easy.

How to send send images via IdTCPClient

P: n/a
Hi, I make program, which sendig printscreen on remote PC and i have a problem. I don't know how to send an image from remote PC on the local PC. I programm in RAD Studio 2009 and for internet connection using components IdTCPClient and IdTCPServer. Thank you.
Oct 12 '10 #1
Share this Question
Share on Google+
3 Replies


ashitpro
Expert 100+
P: 542
I believe you have succeeded to save print screen image at remote machine. Your problem is how to transfer that image from remote to local machine, correct me if I am wrong.
And you wish to do this using tcp client/server mechanism.

Well, if this is the case then you will have to implement a tcp server at local machine and tcp client at remote machine. If you are done with this, then using send/recv system call, start sending data of image from remote to local machine. Once you have done with transfer, save that data as a image at local machine.
Oct 12 '10 #2

P: n/a
yes, you was right.. I had a mistake in my topic

now I have TCP client at my local machine and TCP server at remote machine. I need to solve this problem - sending picture from remote machine to local machine

can you decribe me procedure or show me a picture of it ?
Oct 14 '10 #3

ashitpro
Expert 100+
P: 542
Here's the link for details.
http://www.linuxhowtos.org/C_C++/socket.htm

If you look at the server code:

Expand|Select|Wrap|Line Numbers
  1. /* A simple server in the internet domain using TCP
  2.    The port number is passed as an argument */
  3. #include <stdio.h>
  4. #include <sys/types.h> 
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7.  
  8. void error(char *msg)
  9. {
  10.     perror(msg);
  11.     exit(1);
  12. }
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.      int sockfd, newsockfd, portno, clilen;
  17.      char buffer[256];
  18.      struct sockaddr_in serv_addr, cli_addr;
  19.      int n;
  20.      if (argc < 2) {
  21.          fprintf(stderr,"ERROR, no port provided\n");
  22.          exit(1);
  23.      }
  24.      sockfd = socket(AF_INET, SOCK_STREAM, 0);
  25.      if (sockfd < 0) 
  26.         error("ERROR opening socket");
  27.      bzero((char *) &serv_addr, sizeof(serv_addr));
  28.      portno = atoi(argv[1]);
  29.      serv_addr.sin_family = AF_INET;
  30.      serv_addr.sin_addr.s_addr = INADDR_ANY;
  31.      serv_addr.sin_port = htons(portno);
  32.      if (bind(sockfd, (struct sockaddr *) &serv_addr,
  33.               sizeof(serv_addr)) < 0) 
  34.               error("ERROR on binding");
  35.      listen(sockfd,5);
  36.      clilen = sizeof(cli_addr);
  37.      newsockfd = accept(sockfd, 
  38.                  (struct sockaddr *) &cli_addr, 
  39.                  &clilen);
  40.      if (newsockfd < 0) 
  41.           error("ERROR on accept");
  42.      bzero(buffer,256);
  43.      n = read(newsockfd,buffer,255);
  44.      if (n < 0) error("ERROR reading from socket");
  45.      printf("Here is the message: %s\n",buffer);
  46.      n = write(newsockfd,"I got your message",18);
  47.      if (n < 0) error("ERROR writing to socket");
  48.      return 0; 
  49. }
  50.  
Before line 46 you will have to write a code to read the image file and send the data through 'write' system call

Something like this:

Expand|Select|Wrap|Line Numbers
  1. struct stat file_stat;
  2. int file_size = 0;
  3. char* bin_data;
  4. FILE *fp = fopen("pic.jpg","rb");
  5. if(stat("main.c", &file_stat) != 0) {
  6.       perror("could not stat");
  7.       return (1);
  8.    }
  9. file_size = (int) file_stat.st_size;
  10. bin_data = malloc(sizeof(char) * file_size);
  11. fread(bin_data, 1, file_size, fp);
  12. fcolse(fp);
  13. //Up to this point we are reading file content into a string. Now send this data across the network
  14. n = write(newsockfd,bin_data,file_size);
  15.  
At client side you can use 'read' system call inside while to retrieve the data send from server.

You can use you own control characters, so that client could know that file transfer has been done. Now, save the content into a file using fwrite.
Oct 15 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.