Repository navigation
Expand file tree
/
Copy pathclient.c
More file actions
76 lines (68 loc) · 1.86 KB
/
Copy pathclient.c
File metadata and controls
76 lines (68 loc) · 1.86 KB
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#define SERVER_PORT 5000
int main(int argc, char *argv[]) {
int sock, rc, i;
struct sockaddr_in localAddr, srvAddr;
struct hostent *host;
if(argc < 3) {
printf("usage: %s <servername> <data> \n", argv[0]);
}
host = gethostbyname(argv[1]);
if( host == NULL) {
perror("fail to get host by host name");
exit(EXIT_FAILURE);
}
srvAddr.sin_family = AF_INET;
memcpy((char *)&srvAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
srvAddr.sin_port = htons(SERVER_PORT);
//create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if( sock < 0 ) {
perror("fail to open socket!\n");
exit(EXIT_FAILURE);
}
//bind local port
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
if(bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) {
perror("fail to bind address");
exit(EXIT_FAILURE);
}
if(connect(sock, (struct sockaddr *)&srvAddr, sizeof(srvAddr)) < 0) {
perror("fail to connect remote address");
exit(EXIT_FAILURE);
}
//open a file
FILE *imgfd;
struct stat *imgstat;
imgfd = fopen(argv[2], "r");
int imgfsize;
stat(argv[2], imgstat);
imgfsize = imgstat->st_size;
printf("sending picture size:%d\n", imgfsize);
char imgbuffer[300];
while(!feof(imgfd)) {
fread(imgbuffer, sizeof(imgbuffer), 1, imgfd);
write(sock, imgbuffer, sizeof(imgbuffer));
bzero(imgbuffer,sizeof(imgbuffer));
}
write(sock,"\n", 2);
printf("finished!\nwaiting for feedback\n");
char sockbuf[500],out[500];
read(sock, sockbuf, sizeof(sockbuf));
//remove \n in the string;
strncpy(out,sockbuf,strlen(sockbuf)-5);
printf("the prediction is\n");
printf("%s\n",out);
fclose(imgfd);
close(sock);
}