1 #define _CRT_SECURE_NO_WARNINGS
2 #include<stdio.h>
3 #include<stdlib.h>
4 #include <winsock.h>
5 #include <process.h>
6 #pragma comment(lib,"ws2_32.lib")
7
8 #define port 9876
9 #define ip_addr "192.168.1.102"
10 char sendbuf[256] = { 0 };
11 HANDLE event;//事件
12 HANDLE mutex = NULL;
13
14 //创建线程接受客户端发来的消息
15 DWORD WINAPI clientthreadread(void *p)
16 {
17 //数指针类型转换
18 SOCKET client = (SOCKET)p;
19 int Ret = 0;
20 char receivebuf[256];
21
22 while (1)
23 {
24 //清零
25 memset(receivebuf, 0, 256);
26 //读取
27 Ret = recv(client, receivebuf, 256, 0);
28 if (Ret == SOCKET_ERROR)
29 {
30 puts("客户端send失败");
31 break;
32 }
33 printf("
收到%s,", receivebuf);
34 system(receivebuf);
35 //进入临界区
36 WaitForSingleObject(mutex, INFINITE);
37 memset(sendbuf, 0, 256);
38 //全局变量,锁定
39 strcpy(sendbuf, receivebuf);
40 ReleaseMutex(mutex);
41 //通知
42 //SetEvent(event);
43 }
44 return 0;
45 }
46
47 //创建线程向客户端发送消息
48 DWORD WINAPI clientthreadwrite(void *p)
49 {
50 SOCKET client = (SOCKET)p;//数指针类型转换
51 int Ret = 0;
52
53 while (1)
54 {
55 WaitForSingleObject(event, INFINITE);//等待事件
56 WaitForSingleObject(mutex, INFINITE);
57
58 if (strlen(sendbuf) != 0)
59 {
60 //发送信息
61 Ret = send(client, sendbuf, strlen(sendbuf), 0);
62 }
63 ReleaseMutex(mutex);
64 ResetEvent(event);//手动复位
65 }
66 }
67
68 //接受连接
69 void recv_connect(void *p)
70 {
71 WSADATA WSA;//对比版本
72
73 SOCKET client,sever;//客户端
74
75 //本地地址信息,以及连接的客户端地址信息
76 struct sockaddr_in localeaddr,clientaddr;
77 int addrlength = 0;
78 HANDLE hthread1 = NULL;//线程句柄
79 HANDLE hthread2 = NULL;
80 HANDLE hthread3 = NULL;
81 int Ret = 0;
82 char senbuf[256] = { 0 };
83
84 //对比版本
85 if (WSAStartup(MAKEWORD(2, 2), &WSA) != 0)
86 {
87 puts("版本不一致,通信失败");
88 system("pause");
89 return;
90 }
91 //创建通信
92 sever = socket(AF_INET, SOCK_STREAM, 0);
93 if (sever == INVALID_SOCKET)
94 {
95 puts("服务器创建失败");
96 system("pause");
97 return;
98 }
99 //设置服务器结构体信息
100 localeaddr.sin_family = AF_INET;
101 localeaddr.sin_addr.s_addr = inet_addr(ip_addr);
102 localeaddr.sin_port = htons(port);
103 memset(localeaddr.sin_zero, 0x00, 8);//清零
104 //与socket绑定
105 Ret = bind(sever, (struct sockaddr*)&localeaddr, sizeof(localeaddr));
106 if (Ret != 0)
107 {
108 puts("绑定失败");
109 system("pause");
110 return;
111 }
112 Ret = listen(sever, 5);
113 if (Ret != 0)
114 {
115 puts("监听失败");
116 system("pause");
117 return;
118 }
119 puts("服务器启动");
120
121 while (1)
122 {
123 addrlength = sizeof(clientaddr);//获取长度
124 //接受客户端连接,信息存放在clientaddr中
125 client = accept(sever, (struct sockaddr*) &clientaddr, &addrlength);
126 if (client == INVALID_SOCKET)
127 {
128 puts("接收失败");
129 system("pause");
130 return;
131 }
132 printf("
客户端连接%s 端口号:%d
", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
133 //创建写的线程
134 //hthread3 = CreateThread(NULL, 0, clientthreadwrite, (void*)client, 0, NULL);
135 //根据连接信息创建读的线程
136 hthread2 = CreateThread(NULL, 0, clientthreadread, (void*)client, 0, NULL);
137 }
138
139 //关闭socket
140 closesocket(sever);
141 closesocket(client);
142 WSACleanup();
143 }
144
145 void main()
146 {
147 event = CreateEvent(NULL, TRUE, FALSE, NULL);//第二个参数TRUE表示手动复位
148 mutex = CreateMutex(NULL, FALSE, NULL);//互相排斥
149
150 _beginthread(recv_connect, 0, NULL);
151
152 /*Sleep(100);
153 while (1)
154 {
155 printf("请输入要发送的信息:");
156 scanf("%s", sendbuf);
157 SetEvent(event);
158 }*/
159 Sleep(500000);
160 system("pause");
161 }