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

UDP发送和接收

时间:2014-05-26 12:07:41      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

MainRecv.cpp

bubuko.com,布布扣
#include <iostream>
#include <WinSock2.h>
#include <sstream>

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

#define RECV_IP "127.0.0.1"
#define RECV_PORT 8899
#define LEN_RECV_BUF 2048

int main()
{
    std::ostringstream ossTemp;
    WSAData wsaData;

    std::cout<<"Start..."<<std::endl;

    WSAStartup(MAKEWORD(2,2),&wsaData);
    SOCKET sktRecv=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

    sockaddr_in addrRecv;
    addrRecv.sin_family=AF_INET;
    addrRecv.sin_addr.s_addr=inet_addr(RECV_IP);
         //htonl(INADDR_ANY);
    addrRecv.sin_port=htons(RECV_PORT);

    int retVal;

    retVal=bind(sktRecv,(sockaddr*)&addrRecv,sizeof(addrRecv));

    sockaddr_in addrClient;
    char buf[LEN_RECV_BUF];
    int len=sizeof(addrClient);
    while(true)
    {
        memset(buf,0,LEN_RECV_BUF);
        retVal=recvfrom(sktRecv,buf,LEN_RECV_BUF,0,(sockaddr*)&addrClient,&len);
        if(SOCKET_ERROR==retVal)
        {
            ossTemp<<"recvfrom error : "<<retVal<<"\n";
            OutputDebugString(ossTemp.str().c_str());
            break;
        }
        if(strlen(buf)>0)
            std::cout<<buf<<std::endl;
    }

    closesocket(sktRecv);
    WSACleanup();

    std::cout<<"Recv Over"<<std::endl;
    getchar();
    return -1;
}
bubuko.com,布布扣

MainSend.cpp

bubuko.com,布布扣
#include <iostream>
#include <sstream>
#include <WinSock2.h>

#define IP_SEND "127.0.0.1"
#define PORT_SEND 8899
#define LEN_SEND_BUF 1024

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

int main()
{
    WSAData wsaData;
    WSAStartup(MAKEWORD(2,2),&wsaData);

    std::cout<<"Begin Sending..."<<std::endl;

    std::ostringstream ossTemp;
    SOCKET sktSend=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    sockaddr_in addrSend;
    addrSend.sin_family=AF_INET;
    addrSend.sin_addr.s_addr=inet_addr(IP_SEND);
    addrSend.sin_port=htons(PORT_SEND);
    int retVal;

    char buf[LEN_SEND_BUF];
    int len=sizeof(addrSend);
    int index=0;
    while(true)
    {
        if(index>255)
            break;
        ossTemp.str("");
        ossTemp<<index;
        memset(buf,0,LEN_SEND_BUF);
        memcpy(buf,ossTemp.str().c_str(),sizeof(int));
        retVal=sendto(sktSend,buf,LEN_SEND_BUF,0,(sockaddr*)&addrSend,len);
        if(SOCKET_ERROR==retVal)
        {
            ossTemp.str("");
            ossTemp<<"sendto error : "<<retVal;
            OutputDebugString( ossTemp.str().c_str() );
            break;
        }
        std::cout<<"Send : "<<buf<<std::endl;
        index++;
    }

    closesocket(sktSend);
    WSACleanup();

    std::cout<<"Send Over"<<std::endl;
    getchar();
    return 0;
}
bubuko.com,布布扣

 设置端口缓存大小:

//2. may set socket behavior
int nErrCode;
unsigned int uiRcvBuf;
int uiRcvBufLen = sizeof(uiRcvBuf);
nErrCode= getsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF, (char*)&uiRcvBuf, &uiRcvBufLen);
if(SOCKET_ERROR == nErrCode) return FALSE;
if(uiRcvBuf!=1024*8){
  //uiRcvBuf *= BUF_MULTIPLE;
  uiRcvBuf=1024*8;
  nErrCode = setsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, uiRcvBufLen);
if (SOCKET_ERROR == nErrCode) return FALSE;

nErrCode=getsockopt(m_tcpClient, SOL_SOCKET, SO_RCVBUF,(char*)&uiRcvBuf, &uiRcvBufLen);
if (SOCKET_ERROR == nErrCode) return FALSE;
if(uiRcvBuf!=1024*8) return FALSE;
}

UDP发送和接收,布布扣,bubuko.com

UDP发送和接收

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/tiancun/p/3746883.html

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