-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_client.cpp
executable file
·168 lines (145 loc) · 3.94 KB
/
image_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<iostream>
#include<sstream>
#include<fstream>
#include<errno.h>
#include<stdlib.h>
#include<errno.h>
using namespace std;
int send_image(int socket, int sequence)
{
cout<<"ENTERING SEND"<<endl;
std::string path = "/home/andreas/images/image_";
std::string jpg =".jpg";
stringstream sstream ;
sstream << path << sequence << jpg;
std::string full_path = sstream.str();
FILE *picture;
int size, read_size, stat, packet_index;
char send_buffer[10240], read_buffer[256];
packet_index = 1;
cout << full_path.c_str()<<endl;
picture = fopen(full_path.c_str(), "r");
printf("Getting Picture Size\n");
if(picture == NULL) {
perror(full_path.c_str());
printf("Error %d \n", errno); }
cout << "1" << endl;
fseek(picture, 0, SEEK_END);
cout << "2" << endl;
size = ftell(picture);
cout << "3" << endl;
fseek(picture, 0, SEEK_SET);
printf("Total Picture size: %i\n",size);
//Send Picture Size
printf("Sending Picture Size\n");
write(socket, (void *)&size, sizeof(int));
//Send Picture as Byte Array
printf("Sending Picture as Byte Array\n");
do
{ //Read while we get errors that are due to signals.
cout<<"Reading"<< endl;
stat=read(socket, &read_buffer , 255);
printf("Bytes read: %i\n",stat);
} while (stat < 0);
printf("Received data in socket\n");
printf("Socket data: %c\n", read_buffer);
while(!feof(picture))
{
//while(packet_index = 1){
//Read from the file into our send buffer
read_size = fread(send_buffer, 1, sizeof(send_buffer)-1, picture);
//Send data through our socket
do
{
stat = write(socket, send_buffer, read_size);
}while (stat < 0);
printf("Packet Number: %i\n",packet_index);
printf("Packet Size Sent: %i\n",read_size);
printf(" \n");
printf(" \n");
packet_index++;
//Zero out our send buffer
bzero(send_buffer, sizeof(send_buffer));
}
//fflush(stdout);
cout<<"EXITING"<<endl;
return 0;
}
int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
char *parray;
//Create socket
/*
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
cout<<"Here"<<endl;
if (socket_desc == -1)
{
printf("Could not create socket");
}
memset(&server,0,sizeof(server));
server.sin_addr.s_addr = inet_addr("192.168.2.2");
server.sin_family = AF_INET;
server.sin_port = htons( 8889 );
/*
//Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server ,
sizeof(server)) < 0)
{
cout<<strerror(errno);
close(socket_desc);
puts("Connect Error");
return 1;
}
puts("Connected\n");
fflush(stdout);
for(int i=1; i<11; i++)
{
send_image(socket_desc, i);
}
close(socket_desc);
*/
int sequence=0;
while (true)
{
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
cout<<"Here"<<endl;
if (socket_desc == -1)
{
printf("Could not create socket");
}
memset(&server,0,sizeof(server));
server.sin_addr.s_addr = inet_addr("192.168.2.2");
server.sin_family = AF_INET;
server.sin_port = htons( 8889 );
if (connect(socket_desc , (struct sockaddr *)&server ,
sizeof(server)) < 0)
{
cout<<strerror(errno);
close(socket_desc);
puts("Connect Error");
return 1;
}
puts("Connected\n");
fflush(stdout);
if (sequence<10)
{
sequence++;
send_image(socket_desc, sequence);
}
else
{
return 1;
}
close(socket_desc);
}
close(socket_desc);
return 0;
}