Windows网络编程

2018-09-27  本文已影响0人  Pino_HD



欢迎关注我的新博客https://pino-hd.github.io,最新的博文都会发布在上面哦~

前言

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

TCP版本

#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;



}
#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;
}
gcc server.c -o server -lwsock32
gcc client.c -o client -lwsock32

UDP版本

#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;
}
#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;
}
gcc server_udp.c -o server_udp -lwsock32
gcc client_udp.c -o client_udp -lwsock32

总结

对于TCP版本的程序来说

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

对于UDP版本的程序来说

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

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

上一篇下一篇

猜你喜欢

热点阅读