-
Notifications
You must be signed in to change notification settings - Fork 1
/
SocketServer.cpp
141 lines (131 loc) · 3.5 KB
/
SocketServer.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// testsomething.cpp : Defines the entry point for the console application.
//
//Server.cpp : Defines the entry point for the console application.
//服务器程序工作
//1.初始化 Winsock库
//2.创建监听套接字(socket()函数)
//3.填写要绑定的本地地址结构(bind()函数)
//4.给监听套接字绑定本地IP地址
//5.开始监听(listen()函数)
//6.循环执行 调用accept()函数接收客户端连接请求,如果accept()函数返回成功.则调用send()函数向客户端发送数据或者调用recv()函数从客户端接收数据
#include <iostream>
#include <WinSock2.h>
#include <string.h>
using namespace std;
#pragma comment(lib, "WS2_32.lib")
//表示链接WS2_32.lib这个库。
//#define PORT 1024
int main()
{
SOCKET sock_server, newsock;
struct sockaddr_in server_addr, client_addr;
char msg[] = { "Hello clinet\0" };
// 初始化 winsock2.dll[12/27/2017 MagicScaring]
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2); //生成版本号
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
cout << "加载 winsock.dll失败" << endl;
return 0;
}
// 创建套接字 [12/27/2017 MagicScaring]
if ((sock_server = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
{
cout << "创建套接字失败! 错误代码:" << WSAGetLastError() << endl;
WSACleanup(); //注销WinSock动态链接库
return 0;
}
// 填写需要绑定的本地地址 [12/27/2017 MagicScaring]
int addr_len = sizeof(struct sockaddr_in);
memset((void*)&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
int PORT=0;
cout << "输入要打开的端口:" << endl;
cin >> PORT;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock_server, (struct sockaddr*)&server_addr, addr_len) != 0)
{
cout << "绑定失败!错误代码:" << WSAGetLastError() << endl;
closesocket(sock_server); //关闭已连接套接字
WSACleanup(); //注销WinSock动态链接库
return 0;
}
// 开始监听 [12/27/2017 MagicScaring]
if (listen(sock_server, 0) != 0)
{
cout << "listen调用失败!错误代码:" << WSAGetLastError() << endl;
closesocket(sock_server);
WSACleanup();
return 0;
}
else
{
cout << "listening...." << endl;
}
// 循环:接收连接请求并收发数据 [12/27/2017 MagicScaring]
int size;
char msgrec[64];
while (true)
{
if ((newsock = accept(sock_server, (struct sockaddr *)&client_addr, &addr_len)) == INVALID_SOCKET)
{
cout << "accept 函数调用失败! 错误代码:" << WSAGetLastError() << endl;
break;
}
else
{
cout << "成功接收到一个连接请求!" << endl;
while(1){
char msg[1000];
cout << "输入要发送内容:" << endl;
cin >> msg;
size = send(newsock, msg, sizeof(msg), 0);
if (size == SOCKET_ERROR)
{
cout << "发送信息失败! 错误代码:" << WSAGetLastError() << endl;
closesocket(newsock);
continue;
}
else if (size == 0)
{
cout << "对方已关闭连接" << endl;
closesocket(newsock);
continue;
}
else
{
cout << "信息发送成功" << endl;
}
}
while(1)
{
//memset(msgrec,0x00,sizeof(msgrec));
//strcat(msg,"000");
size = recv(newsock, msgrec, sizeof(msgrec), 0);
//strcat(msg,"\0");
//cout<<'123'<<size<<endl;
if (size == SOCKET_ERROR)
{
cout << "接收信息失败! 错误代码:" << WSAGetLastError() << endl;
closesocket(newsock);
WSACleanup();
return 0;
}
else if (size == 0)
{
cout << "对方已关闭连接" << endl;
closesocket(newsock);
WSACleanup();
return 0;
}
else if(msgrec[0]>=48 && msgrec[0]<=57)
{
cout << "The message from Client:" << msgrec << endl;
//添加函数
}
}
}
}
return 0;
}