win 平台 UDP 客户端与服务器本机数据交互()

#include <stdio.h>
#include <winsock2.h>

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

int main(void)
{
    //初始化wsa
    WORD sockVision = MAKEWORD(2, 2);
    WSADATA wsadata;
    //其他变量
    SOCKET serSocket;
    struct sockaddr_in remoteAddr;
    struct sockaddr_in sin;
    int ret = -1;
    int nAddrlen = sizeof(remoteAddr);
    char revdata[255];
    char senddata[255];

    if (WSAStartup(sockVision, &wsadata) != 0)
    {
        printf("WSA初始化失败\n");
        return 0;
    }

    //创建套接字
    serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (serSocket == INVALID_SOCKET)
    {
        printf("socket服务器创建失败\n");
        return 0;
    }

    //绑定IP和端口
    sin.sin_family = AF_INET;
    sin.sin_port = htons(8888);
    sin.sin_addr.S_un.S_addr = INADDR_ANY;
    if (bind(serSocket, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
        printf("绑定IP和端口\n");
        return 0;
    }

    //循环接收数据
    while (1)
    {
        ret = recvfrom(serSocket, revdata, 255, 0, (SOCKADDR*)&remoteAddr, &nAddrlen);
        if (ret > 0)
        {
            revdata[ret] = 0x00;
            printf("接收到一个连接:%s \n", inet_ntoa(remoteAddr.sin_addr));
            printf("\n");
            printf("客户:");
            printf(revdata);
            printf("\n");
        }

        if (strcmp(revdata, "bye") == 0)
            break;

        //发送数据
        printf("我:");
    //    scanf("%s", senddata);
        sendto(serSocket, senddata, strlen(senddata), 0, (SOCKADDR*)&remoteAddr, nAddrlen);
    }

    closesocket(serSocket);
    WSACleanup();
    return 0;
} 
#include <stdio.h>
#include <winsock2.h>

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


int main(void)
{
    WORD sockVision = MAKEWORD(2, 2);
    WSADATA wsadata;
    SOCKET sclient;
    struct sockaddr_in clientAddr;
    char senddata[255];
    char revdata[255];
    int ret = -1;
    int len = -1;
    if (WSAStartup(sockVision, &wsadata) != 0)
    {
        printf("WSA初始化失败\n");
        return 0;
    }

    sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sclient == INVALID_SOCKET)
    {
        printf("socket客户端创建失败\n");
        return 0;
    }

    clientAddr.sin_family = AF_INET;
    clientAddr.sin_port = htons(8888);
    clientAddr.sin_addr.S_un.S_addr = INADDR_ANY;// inet_addr("192.168.1.105");
    len = sizeof(clientAddr);

    while (1)
    {
        printf("我:");
        scanf("%s", senddata);
        sendto(sclient, senddata, strlen(senddata), 0, (SOCKADDR*)&clientAddr, len);
        if (strcmp(senddata, "bye") == 0)
            break;
        //ret = recvfrom(sclient, revdata, 255, 0, (SOCKADDR*)&clientAddr, &len);
        if (ret > 0)
        {
            revdata[ret] = 0x00;
            printf("\n");
            printf("服务器:");
            printf(revdata);
            printf("\n");
        }
    }

    closesocket(sclient);
    WSACleanup();

    system("pause");
    return 0;


} 
————————
#include <stdio.h>
#include <winsock2.h>

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

int main(void)
{
    //初始化wsa
    WORD sockVision = MAKEWORD(2, 2);
    WSADATA wsadata;
    //其他变量
    SOCKET serSocket;
    struct sockaddr_in remoteAddr;
    struct sockaddr_in sin;
    int ret = -1;
    int nAddrlen = sizeof(remoteAddr);
    char revdata[255];
    char senddata[255];

    if (WSAStartup(sockVision, &wsadata) != 0)
    {
        printf("WSA初始化失败\n");
        return 0;
    }

    //创建套接字
    serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (serSocket == INVALID_SOCKET)
    {
        printf("socket服务器创建失败\n");
        return 0;
    }

    //绑定IP和端口
    sin.sin_family = AF_INET;
    sin.sin_port = htons(8888);
    sin.sin_addr.S_un.S_addr = INADDR_ANY;
    if (bind(serSocket, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
        printf("绑定IP和端口\n");
        return 0;
    }

    //循环接收数据
    while (1)
    {
        ret = recvfrom(serSocket, revdata, 255, 0, (SOCKADDR*)&remoteAddr, &nAddrlen);
        if (ret > 0)
        {
            revdata[ret] = 0x00;
            printf("接收到一个连接:%s \n", inet_ntoa(remoteAddr.sin_addr));
            printf("\n");
            printf("客户:");
            printf(revdata);
            printf("\n");
        }

        if (strcmp(revdata, "bye") == 0)
            break;

        //发送数据
        printf("我:");
    //    scanf("%s", senddata);
        sendto(serSocket, senddata, strlen(senddata), 0, (SOCKADDR*)&remoteAddr, nAddrlen);
    }

    closesocket(serSocket);
    WSACleanup();
    return 0;
} 
#include <stdio.h>
#include <winsock2.h>

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


int main(void)
{
    WORD sockVision = MAKEWORD(2, 2);
    WSADATA wsadata;
    SOCKET sclient;
    struct sockaddr_in clientAddr;
    char senddata[255];
    char revdata[255];
    int ret = -1;
    int len = -1;
    if (WSAStartup(sockVision, &wsadata) != 0)
    {
        printf("WSA初始化失败\n");
        return 0;
    }

    sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sclient == INVALID_SOCKET)
    {
        printf("socket客户端创建失败\n");
        return 0;
    }

    clientAddr.sin_family = AF_INET;
    clientAddr.sin_port = htons(8888);
    clientAddr.sin_addr.S_un.S_addr = INADDR_ANY;// inet_addr("192.168.1.105");
    len = sizeof(clientAddr);

    while (1)
    {
        printf("我:");
        scanf("%s", senddata);
        sendto(sclient, senddata, strlen(senddata), 0, (SOCKADDR*)&clientAddr, len);
        if (strcmp(senddata, "bye") == 0)
            break;
        //ret = recvfrom(sclient, revdata, 255, 0, (SOCKADDR*)&clientAddr, &len);
        if (ret > 0)
        {
            revdata[ret] = 0x00;
            printf("\n");
            printf("服务器:");
            printf(revdata);
            printf("\n");
        }
    }

    closesocket(sclient);
    WSACleanup();

    system("pause");
    return 0;


}