-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cc
55 lines (39 loc) · 1.01 KB
/
client.cc
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
/*
TCP Client from scratch
*/
//include necessary headers
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h> // for socket, send, recv
#include <unistd.h>
#include <arpa/inet.h>
#include <cstring>
#include "server_client_functions.h"
#define PORT 8080
#define BUFFER_SIZE 1024
using namespace std;
int main(int argc, char const *argv[])
{
int clientfd;
struct sockaddr_in server_info;
clientfd = socket(AF_INET, SOCK_STREAM, 0);
if(clientfd == -1){
cerr <<"Error creating socket.."<<endl;
return 1;
}
// set up server address structure
memset(&server_info, 0, sizeof(server_info));
server_info.sin_port = htons(PORT);
server_info.sin_addr.s_addr = inet_addr("127.0.0.1");
server_info.sin_family = AF_INET;
//connect to server
if(connect(clientfd, (struct sockaddr* ) &server_info, sizeof(server_info)) == -1){
cerr <<"Error connecting to server\n";
close(clientfd);
return 1;
}
cout << "Connected to server !";
send_image(clientfd, "period.png");
close(clientfd);
return 0;
}