-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.cpp
More file actions
121 lines (100 loc) · 2.57 KB
/
Copy pathsocket.cpp
File metadata and controls
121 lines (100 loc) · 2.57 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
KE Wang
822002009
*/
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#define ERROR_FAIL 1
int Socket::startup()
{
WORD Version;
WSADATA Data;
Version = MAKEWORD(2, 2);
int ret = WSAStartup(Version, &Data);
if(ret != ERROR_SUCCESS)
{
printf("Winsock DLL not found.\n");
return ERROR_FAIL;
}
if (LOBYTE(Data.wVersion) != 2 || HIBYTE(Data.wVersion) != 2 )
{
/* Tell the user that we could not find a usable WinSock DLL.*/
printf("Server: The dll do not support the Winsock version %u.%u!\n", LOBYTE(Data.wVersion), HIBYTE(Data.wVersion));
return ERROR_FAIL;
}
return ERROR_SUCCESS;
}
bool Socket::sock_create()
{
if((sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) == INVALID_SOCKET)
{
printf("Fail to create socket with error = %d.\n",WSAGetLastError());
return false;//return ERROR_FAIL;
}
bool option = true;
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char*)&option,sizeof(bool)) == SOCKET_ERROR)
{
printf("Setsockopt for SO_REUSEADDR failed with error: %u\n", WSAGetLastError());
return false;
}
//LINGER linger;
//linger.l_onoff = 0;
//if(setsockopt(sock,SOL_SOCKET,SO_LINGER,(char*)&linger,sizeof(LINGER)) == SOCKET_ERROR)
//{
// printf("Setsockopt for SO_LINGER failed with error: %u\n", WSAGetLastError());
// return ERROR_FAIL;
//}
SOCKADDR_IN server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = 0;
if(bind(sock,(const sockaddr*)&server,sizeof(sockaddr))
== SOCKET_ERROR)
{
printf("Fail to connect to server with error = %d.\n",WSAGetLastError());
return false;
}
return true;
}
bool Socket::sock_send(char* send_buf,int count,char* address,char* port)
{
SOCKADDR_IN server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(address);
server.sin_port = htons(atoi(port));
if(sendto(sock,send_buf,count,0, (SOCKADDR*)&server,sizeof(SOCKADDR)) == SOCKET_ERROR)
{
printf("Fail to send to server with error = %d.\n",WSAGetLastError());
return false;
}
return true;
}
bool Socket::sock_recv()
{
SOCKADDR_IN server;
recv_buf = new char[1024];
memset(recv_buf,0,recv_buf_size);
int recv_count = 0;
fd_set fd;
FD_ZERO(&fd);
FD_SET(sock, &fd);
int ret;
timeval timeout;
timeout.tv_sec = 30;
timeout.tv_usec = 0;
int count = 0;
ret = select(0, &fd, NULL,NULL, &timeout);
if(ret > 0)
{
int size = sizeof(SOCKADDR);
recvfrom(sock,recv_buf,recv_buf_size - total_recv_count,0,(SOCKADDR*)&server, &size);
return true;
}
else if(ret == SOCKET_ERROR)
{
printf("Fail to select = %d.\n",WSAGetLastError());
return false;
}
return false;
}