C++ UDP socket编程

客户端:

//#include "stdafx.h"
#include
#include
#include 
#include 
#include 
using namespace std;


#pragma comment(lib, "ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
    WSAData wsd;           //初始化信息
    SOCKET soSend;         //发送SOCKET
    int nRet = 0;
    //int i = 0;
    int dwSendSize = 0;
    SOCKADDR_IN siLocal;    //远程发送机地址和本机接收机地址

    //启动Winsock
    if (WSAStartup(MAKEWORD(2,2),&wsd) != 0) {/*进行WinSocket的初始化,
        windows 初始化socket网络库,申请2,2的版本,windows socket编程必须先初始化。*/
        cout << "WSAStartup Error = " << WSAGetLastError() << endl;
        return 0;
    }

    //创建socket

    //AF_INET 协议族:决定了要用ipv4地址(32位的)与端口号(16位的)的组合
    //SOCK_DGRAM --  UDP类型,不保证数据接收的顺序,非可靠连接;
    soSend = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if (soSend == SOCKET_ERROR) {
        cout << "socket Error = " << WSAGetLastError() << endl;
        return 1;
    }

    //设置端口号
    int nPort = 5150;
    siLocal.sin_family = AF_INET;
    siLocal.sin_port = htons(nPort);
    siLocal.sin_addr.s_addr = inet_addr("127.0.0.1");

    for (int i = 0; i < 30; i ++){
        //开始发送数据
        //发送数据到指定的IP地址和端口
        nRet = sendto(soSend,"123 mutouren",strlen("123 mutouren"),0,(SOCKADDR*)&siLocal,sizeof(SOCKADDR));
        if (nRet == SOCKET_ERROR) {
            cout << "sendto Error " << WSAGetLastError() << endl;
            break;
        }
    }
    //关闭socket连接
    closesocket(soSend);
    //清理
    WSACleanup();

    return 0;
}

服务端:

//#include "stdafx.h"
#include
#include
#include 
#include 
#include 
using namespace std;


#pragma comment(lib, "ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])//_tmain,要加#include 才能用
{
    WSAData wsd;           //初始化信息
    SOCKET soRecv;              //接收SOCKET
    char * pszRecv = NULL; //接收数据的数据缓冲区指针
    int nRet = 0;
    //int i = 0;
    int dwSendSize = 0;
    SOCKADDR_IN siRemote,siLocal;    //远程发送机地址和本机接收机地址

    //启动Winsock
    if (WSAStartup(MAKEWORD(2,2),&wsd) != 0) {
        cout << "WSAStartup Error = " << WSAGetLastError() << endl;
        return 0;
    }

    //创建socket
    soRecv = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if (soRecv == SOCKET_ERROR) {
        cout << "socket Error = " << WSAGetLastError() << endl;
        return 1;
    }

    //设置端口号
    int nPort = 5150;

    //int nPort = 1234;
    siLocal.sin_family = AF_INET;
    siLocal.sin_port = htons(nPort);
    siLocal.sin_addr.s_addr = inet_addr("127.0.0.1");

    //绑定本地地址到socket
    if (bind(soRecv,(SOCKADDR*)&siLocal,sizeof(siLocal)) == SOCKET_ERROR) {
        cout << "bind Error = " << WSAGetLastError() << endl;
        return 1;
    }

    //申请内存
    pszRecv = new char[4096];
    if (pszRecv == NULL) {
        cout << "pszRecv new char Error " << endl;
        return 0;
    }

    for (int i = 0; i < 30; i ++){
        dwSendSize = sizeof(siRemote);
        //开始接受数据
        nRet = recvfrom(soRecv,pszRecv,4096,0,(SOCKADDR*)&siRemote,&dwSendSize);
        if (nRet == SOCKET_ERROR) {
            cout << "recvfrom Error " << WSAGetLastError() << endl;
            break;
        }
        else if (nRet == 0) {
            break;
        }
        else{
            pszRecv[nRet] = '\0';
            cout << inet_ntoa(siRemote.sin_addr) << endl
                <

 

reference:   socket接口详解

                   udp发送和接受

 

你可能感兴趣的:(C++)