码迷,mamicode.com
首页 > 其他好文 > 详细

【tcp-ip学习总结】基础UDP实现的简单通信功能小程序

时间:2015-04-22 22:22:54      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:udp

环境:VS2013

#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#pragma comment(lib,"ws2_32.lib")

#define DEFAULT_PORT 8888
#define BUFFER_LENGTH 1024

void main()
{
    int iPort = DEFAULT_PORT;
    WSADATA wsaData;//协议栈
    SOCKET sSocket;//服务端的socket
    int iLen;//客户端地址长度
    int iSend;//发送数据的长度
    int iRecv;//接受数据的缓冲区长度
    char sendBuf[] = "Hello! I am server";//服务端要发送的字符串
    char recvBuf[BUFFER_LENGTH];//服务器端接受数据的缓冲区
    struct sockaddr_in ser, cli; //本地地址和客户地址
    printf("-----------------\n");
    printf("Server waiting\n");
    printf("-----------------\n");

    //加载协议栈
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("Failed to load Winsock.\n");
        return;
    }

    //创建服务器端的socket套接口
    sSocket = socket(AF_INET, SOCK_DGRAM, 0);
    if (sSocket == INVALID_SOCKET)
    {
        printf("socket(Failed:%d\n)", WSAGetLastError);
        return;
    }
    //初始化服务器端的地址与端口号
    ser.sin_family = AF_INET;
    ser.sin_port = htons(8888);//服务器端监听的端口号
    ser.sin_addr.s_addr = htonl(INADDR_ANY);//服务端的端口地址

    //将服务器端socket和服务器的端ip地址和端口号绑定,
    if (bind(sSocket, (LPSOCKADDR)&ser, sizeof(ser)) == SOCKET_ERROR)
    {
        printf("bind() Failed:%d\n", WSAGetLastError());
        return;
    }

    iLen = sizeof(cli);
    memset(recvBuf, 0, sizeof(recvBuf));//将接收缓冲区内容全部初始化为0

    while (1)
    {
    //从服务器端套接口中接收数据,并把客户端的地址情况保存到cli上面
        iRecv = recvfrom(sSocket, recvBuf, BUFFER_LENGTH, 0,(SOCKADDR*)&cli,&iLen);
        if (iRecv == SOCKET_ERROR)
        {
            printf("recvfrom()Faild;%d\n", WSAGetLastError());
            break;
        } 
        else if (iRecv == 0)
        {
            break;
        }
        else
        {
            printf("recvfrom():%s\n", recvBuf);
            printf("accepted client ip[%s], port[%d]\n", inet_ntoa(cli.sin_addr), ntohs(cli.sin_port));
        }
    //向客户端发送数据
        iSend = sendto(sSocket, sendBuf, sizeof(sendBuf), 0, (SOCKADDR*)&cli, sizeof(cli));
        if (iSend == SOCKET_ERROR)
        {
            printf("sendto() Faild:%d\n", WSAGetLastError());
            printf("----------\n");
            break;
        }
        else if (iSend == 0)
            break;
        else
        {
            printf("sendto() succeeded\n");
            printf("-=---------\n");
        }
    }
    closesocket(sSocket);//关闭套接口
    WSACleanup();//清空协议栈
}

client.c

#include<Winsock2.h>
#include<stdio.h>
#pragma comment(lib,"ws2_32.lib")

void main()
{
    WSADATA wsaData;//协议栈
    SOCKET sClient;//客户端套接口
    struct sockaddr_in ser;//客户端地址
    int iPort = 8888;
    int iLen;//客户端地址长度
    int iSend;
    int iRecv;
    char sendBuf[1024];//发送数据缓冲区
    char recvBuf[1024];//接收数据缓冲区

    memset(recvBuf, 0, sizeof(recvBuf));//把接收数据缓冲区初始化为0
    //加载协议栈
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("Failed to load Winsock\n");
        return;
    }
    //初始化客户端地址情况
    ser.sin_family =  AF_INET;
    ser.sin_port = htons(8888);
    ser.sin_addr.s_addr = inet_addr("127.0.0.1");
    //创建客户端套接口
    sClient = socket(AF_INET, SOCK_DGRAM, 0);
    if (sClient == INVALID_SOCKET)
    {
        printf("socket() Faild %d\n", WSAGetLastError());
        return;
    }

    iLen = sizeof(ser);

    while (1) {
        printf_s("请输入要发送的消息:");
        gets_s(sendBuf, 1024);
        //开始发送数据
        iSend = sendto(sClient, sendBuf, sizeof(sendBuf), 0, (struct sockaddr*)&ser, iLen);
        if (iSend == SOCKET_ERROR)
        {
            printf("sendto()faild %d", WSAGetLastError());
            return;
        }
        else if (iSend = 0)
        {
            return;
        }
        else {
            printf("sendto() succeeded\n");

        }
        //接收数据
        iRecv = recvfrom(sClient, recvBuf, sizeof(recvBuf), 0, (struct sockaddr*)&ser, &iLen);
        if (iRecv == SOCKET_ERROR)
        {
            printf("recvfrom() faild %d\n", WSAGetLastError());
            return;
        }
        else if (iRecv == 0)
            return;
        else {
            printf("recvfrom() %s", recvBuf);
            printf("-----------------\n");
        }
    }

    closesocket(sClient);//关闭套接口
    WSACleanup();//清空协议栈
}

【tcp-ip学习总结】基础UDP实现的简单通信功能小程序

标签:udp

原文地址:http://blog.csdn.net/ttf1993/article/details/45199307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!