-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
131 lines (123 loc) · 3.41 KB
/
server
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
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <winsock2.h>
#include <winsock2.h>
#define SERVER_PORT 5208 //侦听端口
#pragma comment(lib,"wsock32.lib")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int ret, nLeft, length, flag;
SOCKET sListen, sServer; //侦听套接字,连接套接字
struct sockaddr_in saServer, saClient; //地址信息
char *ptr;//用于遍历信息的指针
//WinSock初始化
wVersionRequested=MAKEWORD(2, 2); //希望使用的WinSock DLL 的版本
ret=WSAStartup(wVersionRequested, &wsaData);
if(ret!=0)
{
printf("WSAStartup() failed!\n");
return 6;
}
//创建Socket,使用TCP协议
sListen=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sListen == INVALID_SOCKET)
{
WSACleanup();
printf("socket() faild!\n");
return 5;
}
//构建本地地址信息
saServer.sin_family = AF_INET; //地址家族
saServer.sin_port = htons(SERVER_PORT); //注意转化为网络字节序
saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //使用INADDR_ANY 指示任意地址
//绑定
ret = bind(sListen, (struct sockaddr *)&saServer, sizeof(saServer));
if (ret == SOCKET_ERROR)
{
printf("bind() faild! code:%d\n", WSAGetLastError());
closesocket(sListen); //关闭套接字
WSACleanup();
return 4;
}
//侦听连接请求
ret = listen(sListen, 5);
if (ret == SOCKET_ERROR)
{
printf("listen() faild! code:%d\n", WSAGetLastError());
closesocket(sListen); //关闭套接字
return 3;
}
printf("Waiting for client connecting!\n\n");
//阻塞等待接受客户端连接
while(1)//循环监听客户端,永远不停止,所以,在本项目中,我们没有心跳包。
{
length = sizeof(saClient);
sServer = accept(sListen, (struct sockaddr *)&saClient, &length);
if (sServer == INVALID_SOCKET)
{
printf("accept() faild! code:%d\n", WSAGetLastError());
closesocket(sListen); //关闭套接字
WSACleanup();
return 2;
}
char receiveMessage[100],message[100];
ret = recv(sServer, receiveMessage, 100, 0);
if (ret == SOCKET_ERROR)
{
printf("recv() failed!\n");
return 1;
}
if (ret == 0) //客户端已经关闭连接
{
printf("Client has closed the connection\n");
break;
}
printf("%s\n",receiveMessage);
while(1)
{ //chat!
printf("1.just listen\t2.say something: ");
scanf("%d",&flag);
if(flag==1)
{
ret = recv(sServer, receiveMessage, 100, 0);
if (ret == SOCKET_ERROR)
{
printf("recv() failed!\n");
return 1;
}
if (ret == 0) //客户端已经关闭连接
{
printf("Client has closed the connection\n");
break;
}
printf("Client said: %s\n",receiveMessage);
}
//system("pause");
/*nLeft -= ret;
ptr += ret;*/
if(flag == 2)
{
printf("\nEnter what you wanna say:\n");
getchar();
gets(message);
ret = send (sServer, (char *)&message, sizeof(message), 0);
if (ret == SOCKET_ERROR)
{
printf("recv() failed!\n");
return 1;
}
if (ret == 0) //客户端已经关闭连接
{
printf("Client has closed the connection\n");
break;
}
}
}
//printf("receive message:%s\n", receiveMessage);//打印我们接收到的消息。
}
// closesocket(sListen);
// closesocket(sServer);
// WSACleanup();
}