-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSocketConnection.cpp
51 lines (44 loc) · 1.26 KB
/
SocketConnection.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
//
// Created by Cry on 2018-12-20.
//
#include <SDL2/SDL_stdinc.h>
#include "SocketConnection.h"
SDL_bool SocketConnection::connect_server() {
//创建Socket
client_conn = socket(PF_INET, SOCK_STREAM, 0);
if (!client_conn) {
perror("can not create socket!!");
return SDL_FALSE;
}
struct sockaddr_in in_addr;
memset(&in_addr, 0, sizeof(sockaddr_in));
in_addr.sin_port = htons(9000);
in_addr.sin_family = AF_INET;
in_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
int ret = connect(client_conn, (struct sockaddr *) &in_addr, sizeof(struct sockaddr));
if (ret < 0) {
perror("socket connect error!!\\n");
return SDL_FALSE;
}
return SDL_TRUE;
}
void SocketConnection::close_client() {
if (client_conn >= 0) {
shutdown(client_conn, SHUT_RDWR);
close(client_conn);
client_conn = 0;
}
}
int SocketConnection::send_to_(uint8_t *buf, int len) {
if (!client_conn) {
return 0;
}
return send(client_conn, buf, len, 0);
}
int SocketConnection::recv_from_(uint8_t *buf, int len) {
if (!client_conn) {
return 0;
}
//rev 和 read 的区别 https://blog.csdn.net/superbfly/article/details/72782264
return recv(client_conn, buf, len, 0);
}