473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to transfer images from client and server using TCP/IP sockets.

Hi,

I have developed the code and i am putting the code for the reference, kindly skim through the code and say your changes.
Expand|Select|Wrap|Line Numbers
  1. server.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <fcntl.h>
  9.  
  10. void set_socket(struct sockaddr_in *socket, int type, int host_short, int addr_type)
  11. {
  12.     socket -> sin_family = type;
  13.     socket -> sin_port = htons(host_short);
  14.     socket -> sin_addr.s_addr = htonl(addr_type);
  15. }
  16.  
  17. void serialize(char *buffer, int count, char *data)
  18. {
  19.     int i=0, j=0;
  20.     char temp1[20];
  21.  
  22.     sprintf(temp1, "%d", count);
  23.     while(temp1[i] != '\0')
  24.     {
  25.         buffer[j++] = temp1[i++];
  26.     }
  27.     buffer[j++]=' ';
  28.  
  29.     for(i=0; data[i] != '\0'; i++)
  30.     {
  31.         buffer[j++] = data[i];
  32.     }
  33.     buffer[j] = '\0';
  34.     printf("BUFFER =%ld\n", sizeof(buffer));
  35. }
  36.  
  37. int main()
  38. {
  39.     int sid = 0, bid = 0, fp;
  40.     char *send_data = (char *)malloc(1024*sizeof(char));
  41.     char temp[1024];
  42.     char *receive_data = (char *)malloc(1024*sizeof(char));
  43.     int fd, count, cnt=0;
  44.     struct sockaddr_in server_socket, client_socket;
  45.     int size = sizeof(client_socket);
  46.  
  47.     if((sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  48.     {
  49.         printf("Connection error..\n");
  50.         exit(1);
  51.     }
  52.  
  53.     set_socket(&server_socket, AF_INET, 6000, INADDR_ANY);
  54.  
  55.     if((bid = bind(sid, (struct sockaddr *)&server_socket, sizeof(struct sockaddr))) == -1)
  56.     {
  57.         printf("Binding error..\n");
  58.         exit(1);
  59.     }
  60.  
  61.     printf("I am waiting for client..\n");
  62.  
  63.     recvfrom(sid, receive_data, 1024, 0,(struct sockaddr *)&client_socket, &size);
  64.     printf("received data is : %s\n", receive_data);
  65.     fd = open(receive_data, O_RDONLY);
  66.     printf("size = %ld\n", sizeof(send_data));
  67.  
  68.     while((count=read(fd, temp, 500)) != 0)
  69.     {
  70.         printf("I am inside the loop : %d\n", cnt++);
  71.         serialize(send_data, count, temp);
  72.         printf("Serialized : %s\n", send_data);
  73.         sendto(sid, send_data, 1024, 0, (struct sockaddr *)&client_socket, size);
  74.     }
  75.     printf("I am outside the loop : %d\n", count);
  76.     strcpy(temp, "ENDOFFILE");
  77.     serialize(send_data, sizeof(temp), temp);
  78.     sendto(sid, send_data, 1024, 0, (struct sockaddr *)&client_socket, size);
  79.  
  80.     fcloseall();
  81.     close(sid);
  82.     close(fd);
  83.     return 0;
  84. }


And also i am putting the client code

Expand|Select|Wrap|Line Numbers
  1. client.c
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <fcntl.h>
  11. #include <stddef.h>
  12.  
  13.  
  14. void set_socket(struct sockaddr_in *socket, int type, int host_short)
  15. {
  16.     socket -> sin_family = type;
  17.     socket -> sin_port = htons(host_short);
  18. }
  19.  
  20. void deserialize(char *buffer, int *size, char *data)
  21. {
  22.     int i=0, j=0;
  23.     char temp1[20];
  24.  
  25.     while(buffer[i] != ' ')
  26.     {
  27.         temp1[j++] = buffer[i++];
  28.     }
  29.     temp1[j] = '\0';
  30.     printf("\nINT : %s\n", temp1);
  31.     *size = atoi(temp1);
  32.  
  33.     i++;
  34.     j=0;
  35.  
  36.     while(buffer[i] != '\0')
  37.     {
  38.         data[j++] = buffer[i++];
  39.     }
  40.     data[j++] = '\0';
  41. }
  42.  
  43. int main()
  44. {
  45.     int sid = 0, bid = 0, con = 0;
  46.     char *send_data = (char *)malloc(1024*sizeof(char));
  47.     char *receive_data = (char *)malloc(1024*sizeof(char));
  48.     char *temp = (char *)malloc(1024*sizeof(char));
  49.     struct hostent *host;
  50.     struct sockaddr_in server_socket;
  51.     int size = sizeof(server_socket);
  52.  
  53.     if((sid = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  54.     {
  55.         printf("Connection error at client side..\n");
  56.         exit(1);
  57.     }
  58.  
  59.     set_socket(&server_socket, AF_INET, 6000);
  60.  
  61.     if (inet_aton("127.0.0.1", &server_socket.sin_addr)==0) 
  62.     {
  63.         fprintf(stderr, "inet_aton() failed\n");
  64.         exit(1);
  65.     }
  66.  
  67.     printf("Enter the name of the file you want to see : ");
  68.     scanf("%s", send_data);
  69.     int fd = open("Cameo.mp3", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
  70.     sendto(sid, send_data, 1024, 0, (struct sockaddr *)&server_socket, size);
  71.     printf("================= Contents of the File =====================\n");
  72.     while(1)
  73.     {
  74.         int size;
  75.         recvfrom(sid, temp, 1024, 0, (struct sockaddr *)&server_socket, &size);
  76.         printf("Deserialize it : %s\n",temp);
  77.         deserialize(temp, &size, receive_data);
  78.         if(!strcmp(receive_data, "ENDOFFILE"))
  79.         {
  80.             printf("============================================================\n");
  81.             break;
  82.         }
  83.         else
  84.             write(fd, receive_data, size);
  85.     }
  86.     fcloseall();
  87.     close(sid);
  88.  
  89.     return 0;
  90. }
Feb 11 '15 #1
0 2500

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

Similar topics

16
by: Frankie | last post by:
Hello: I'm coding a website which allows the owner to make add/change/deletes to his product database, which includes product images stored separately on the server. I have coded the Delete page...
1
by: amontiliado | last post by:
-using the UNIX ,create an announcement environment. -this environment consists of an announcement server and a listening client. -the announcement server is a process which is executed on the...
0
by: Raj | last post by:
Hi All.., I have a VB.Net Windows Application.. I need to Run this Exe From Some Other System(Client) and I need to Upload some specified files to the Server System.. Now my doubts are.. ...
3
by: Guy007 | last post by:
I am trying to do the following in C# (VS 2005): - I have a client computer and a server computer - The server generates a System.Drawing.Bitmap object - The client requests the Bitmap from the...
2
yashg
by: yashg | last post by:
I am building a data backup application in C# using Sockets. It has a server component and a client component. The client is going to upload files to the server through TCP sockets. I've got all...
1
by: danfolkes | last post by:
Hey Everyone, I am trying to send repeated messages from a "Node" to a "Server". It works the first time I send the from the Node to Server, but after that it either errors, or does not do...
4
by: Babloo | last post by:
Hi everyone, i wanted to implement a client- server connection and transfer a file over the network. i was able to implement a normal set up where in i was able to send chat messages and small...
2
by: kashifjawed | last post by:
I'm developing a c# asynchronous socket application for tranfering file or large data from client to server and server to client as well in chunks. Application sometimes transfer file from client...
1
by: HaroonAbbasi | last post by:
Hi Everyone! I need some help. i am working on .net client server application.This application is using on both server/client side enitity framewok of same entities architecture. but from different...
1
by: povs937 | last post by:
I am trying to send the screenshots of the pc(java server) to android(client) using sockets. I am not able to do this task. Well, also I have successfully send the screenshots from pc(java server)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.