-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectToTracker.cpp
50 lines (48 loc) · 1.33 KB
/
ConnectToTracker.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
#include "includes.h"
using namespace std;
int connectToTracker(int port)
{
cout<<"\rConnecting to tracker on port "<<port<<endl;
sockin_t server_addr;
int sock_d;
if ((sock_d = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\rSocket creation error in client side\n");
perror("socket");
return -1;
}
memset(&server_addr, '0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(sock_d,(sock_t*)&server_addr,sizeof(sock_t))<0)
{
perror("connect");
return -1;
}
cout<<"\rconnection done!\n";
return sock_d;
}
int connectToPort(int port)
{
cout<<"\rConnecting to peer on port "<<port<<endl;
sockin_t server_addr;
int sock_d;
if ((sock_d = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\rSocket creation error in client side\n");
perror("socket");
return -1;
}
memset(&server_addr, '0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(sock_d,(sock_t*)&server_addr,sizeof(sock_t))<0)
{
perror("connect");
return -1;
}
cout<<"Connected!\n";
return sock_d;
}