-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
77 lines (71 loc) · 1.49 KB
/
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
#include "client.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <cstdio>
#include <cstdlib>
#include <netdb.h>
#include "network.h"
#include "utils.h"
Client::Client()
{
w = new MainWindow(0, this);
w->show();
connected = false;
}
int* Client::get_sock()
{
return &sock;
}
void Client::set_hostname(QString s)
{
strcpy(hostname, convert_to_cstring((s)));
}
void Client::set_username(QString s)
{
strcpy(username, convert_to_cstring((s)));
}
void Client::set_port(QString s)
{
port = s.toInt();
}
bool Client::is_connected()
{
return connected;
}
void Client::set_connect()
{
connected = true;
}
void Client::send_data()
{
if(send(sock, message, BUFLEN, 0) == -1)
{
// error
}
}
void Client::set_message(const char *s)
{
strcpy(message, s);
}
int Client::connect_to_server()
{
struct sockaddr_in server;
socklen_t serverlen = sizeof(server);
struct hostent *hp;
if((sock = new_tcp_socket()) == -1)
{
// socket not created.
}
bzero((char *)&server, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
if((hp = gethostbyname(hostname)) == NULL)
{
// fatal("Unknown server address"); // maybe not have fatal? Can have client re enter the IP address.
}
bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
return connect(sock, (struct sockaddr*) &server, serverlen);
}