Windows网络编程

前言

因为在写代码的时候感觉用VS太过臃肿,每次写个程序还要创建一个项目,并且特别占用空间,因此安装了mingw,可以在命令行中直接gcc编译,特别方便,安利一下~
并且在编译socket程序的时候,一定要加上-l wsock32选项,因为mingw默认没有包含windows库

TCP版本

  • 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
#include <Winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>

int main(int argc, char* argv[]) {
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
if (WSAStartup(sockVersion, &wsaData) != 0) {
return 0;
}
SOCKET sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sListen == INVALID_SOCKET) {
printf("socket error\n");
return 0;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(4500);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(sListen, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR) {
printf("socket error\n");
closesocket(sListen);
return 0;
}
if (listen(sListen, 5) == SOCKET_ERROR) {
printf("socket error\n");
closesocket(sListen);
return 0;
}
struct sockaddr_in remoteAddr;
SOCKET sClient;
int nAddrLen = sizeof(remoteAddr);
char revData[255];
while(TRUE){
sClient = accept(sListen, (SOCKADDR*)&remoteAddr, &nAddrLen);
if (sClient == SOCKET_ERROR) {
printf("accept() error\n");
continue;
}
printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
int ret = recv(sClient, revData, 255, 0);
if (ret > 0) {
revData[ret] = 0x00;
printf(revData);
}
char* buff = (char*)"\r\nHello, my friend\r\n";
send(sClient, buff, strlen(buff), 0);
closesocket(sClient);
}
closesocket(sListen);
WSACleanup();
return 0;



}
  • CLIENT端
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
#include <WinSock2.h>
#include <stdio.h>

#pragma comment(lib, "ws2_32.lib")

int main(int argc, char* argv[]) {
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
if (WSAStartup(sockVersion, &wsaData) != 0) {
return 0;
}
SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == SOCKET_ERROR) {
printf("socket error\n");
return 0;
}
struct sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(4500);
servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (connect(sClient, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) {
printf("socket error\n");
closesocket(sClient);
return 0;
}
char* buff = (char*)"\r\nHello, my friend\r\n";
send(sClient, buff, strlen(buff), 0);
char revData[255];
int ret = recv(sClient, revData, 255, 0);
if (ret > 0) {
revData[ret] = 0x00;
printf(revData);
}
closesocket(sClient);
WSACleanup();
return 0;
}
  • 编译
1
2
gcc server.c -o server -lwsock32
gcc client.c -o client -lwsock32

UDP版本

  • 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
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>


int main(int argc, char* argv[])
{
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
if (WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}

SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (serSocket == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}

struct sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(8000);
serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(serSocket, (SOCKADDR*)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("bind error !");
closesocket(serSocket);
return 0;
}

struct sockaddr_in remoteAddr;
int nAddrLen = sizeof(remoteAddr);
while (TRUE)
{
char recvData[255];
int ret = recvfrom(serSocket, recvData, 255, 0, (SOCKADDR*)&remoteAddr, &nAddrLen);
if (ret > 0)
{
recvData[ret] = 0x00;
printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
printf(recvData);
}

char * sendData = "一个来自服务端的UDP数据包\n";
sendto(serSocket, sendData, strlen(sendData), 0, (SOCKADDR*)&remoteAddr, nAddrLen);

}
closesocket(serSocket);
WSACleanup();
return 0;
}
  • CLIENT端
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
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>


int main(int argc, char* argv[])
{
WORD socketVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(socketVersion, &wsaData) != 0)
{
return 0;
}
SOCKET sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8000);
sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
int len = sizeof(sin);

char * sendData = "Hello, my friend\n";
sendto(sclient, sendData, strlen(sendData), 0, (SOCKADDR*)&sin, len);

char recvData[255];
int ret = recvfrom(sclient, recvData, 255, 0, (SOCKADDR*)&sin, &len);
if (ret > 0)
{
recvData[ret] = 0x00;
printf(recvData);
}

closesocket(sclient);
WSACleanup();
return 0;
}
  • 编译
1
2
gcc server_udp.c -o server_udp -lwsock32
gcc client_udp.c -o client_udp -lwsock32

总结

对于TCP版本的程序来说

1
server 端:创建WSAStartup -》检测!=0 -》创建socket -》检测SOCKET_ERROR -》声明sockaddr_in结构 -》bind -》检测SOCKET_ERROR -》listen -》检测SOCKET_ERROR -》进入循环 -》accept -》检测SOCKET_ERROR -》接收、发送消息

1
client 端:创建WSAStartup -》检测!=0 -》创建socket -》检测SOCKET_ERROR -》声明sockaddr_in结构 -》connect -》检测SOCKET_ERROR -》发送、接收消息

对于UDP版本的程序来说

1
server 端:创建WSAStartup -》检测!=0 -》创建socket -》检测SOCKET_ERROR -》声明sockaddr_in结构 -》bind -》检测SOCKET_ERROR -》进入循环 -》发送、接收消息

1
client 端:创建WSAStartup -》检测!=0 -》创建socket -》检测SOCKET_ERROR -》声明sockaddr_in结构 -》发送、接收消息

TCP中,使用send和recv函数进行发送和接收消息
UDP中,使用sendto和recvfrom函数进行发送和接收消息

本文标题:Windows网络编程

文章作者:Pino-HD

发布时间:2018年09月24日 - 14:09

最后更新:2018年09月27日 - 17:09

原始链接:https://pino-hd.github.io/2018/09/24/Windows网络编程/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!