码迷,mamicode.com
首页 > Windows程序 > 详细

windows和linux中UDP编程

时间:2015-07-27 13:19:21      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:udp编程

//servser.cpp

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define IP "127.0.0.1"
#define PORT 8888
#pragma comment(lib,"ws2_32.lib")

using namespace std;
char* GetTime();
int main()
{
    WSADATA wsaData;
    WORD Ret;
    if ((Ret = WSAStartup(MAKEWORD(2, 2), &wsaData) != 0))
    {
        cout << "WSAStartup() Error Whit:" << Ret << endl;
        WSACleanup();
        return -1;
    }//统一版本类型。

    SOCKET sockSer, sockCli;
    sockSer = socket(AF_INET,SOCK_DGRAM,0);
    SOCKADDR_IN addrSer, addrCli;


    memset(&addrSer,0,sizeof(addrSer));
    memset(&addrCli,0,sizeof(addrCli));


    addrSer.sin_family = AF_INET;
    addrSer.sin_port = htons(PORT);
    addrSer.sin_addr.S_un.S_addr = inet_addr(IP);

    int len = sizeof(SOCKADDR);
    bind(sockSer,(SOCKADDR*)&addrSer,len);

    char serbuff[255];

    //不需要监听连接。

    while (1)
    {
        recvfrom(sockSer,serbuff,sizeof(serbuff),0,(SOCKADDR*)&addrCli,&len);
        cout << serbuff << endl;
        sendto(sockSer, GetTime(), 30, 0, (SOCKADDR*)&addrCli, len);
    }
    WSACleanup();
    closesocket(sockSer);
    closesocket(sockCli);
    return 0;
}
char* GetTime()
{
    char buff[255];
    SYSTEMTIME sys;
    GetLocalTime(&sys);//初始化本地时间。
    sprintf(buff,"%d:%d:%d:%d:%d:%d",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond);
    return buff;
}

#include <iostream>
#include <windows.h>
#define PORT 8888
#define IP "127.0.0.1"
using namespace std;

#pragma comment(lib,"ws2_32.lib")
int main()
{
    DWORD Ret;
    WSAData wsaData;
    if ((Ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)
    {
        cout <<"WSAStartup() Error Wiht : "<< Ret << endl;
        WSACleanup();
        return -1;
    }

    SOCKET sockCli = socket(AF_INET,SOCK_DGRAM,0);
    SOCKADDR_IN addrSer;
    addrSer.sin_family = AF_INET;
    addrSer.sin_port = htons(PORT);
    addrSer.sin_addr.S_un.S_addr = inet_addr(IP);

    int len = sizeof(addrSer);
    const char timebuff[] = "times";
    char serbuff[255];

    connect(sockCli,(SOCKADDR*)&addrSer,len);

    while (1)
    {
        sendto(sockCli,timebuff,strlen(timebuff)+1,0,(SOCKADDR*)&addrSer,len);
        recvfrom(sockCli,serbuff,sizeof(serbuff),0,(SOCKADDR*)&addrSer,&len);
        cout <<timebuff<<serbuff << endl;
        Sleep(2000);
    }
    return 0;
}

//linux:
//ser.cpp

#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8888
#define IP "127.0.0.1"
using namespace std;
int main()
{
    int sockSer = socket(AF_INET,SOCK_DGRAM,0);
    sockaddr_in addrSer,addrCli;
    addrSer.sin_family = AF_INET;
    addrSer.sin_port = htons(PORT);
    addrSer.sin_addr.s_addr = inet_addr(IP);

    socklen_t len=sizeof(sockaddr);
    bind(sockSer,(sockaddr*)&addrSer,len);
    char serbuff[255]="!Hello !I am server!";
    char clibuff[255];
    while(1)
    {
       recvfrom(sockSer,clibuff,sizeof(clibuff),0,(sockaddr*)&addrSer,&len);
       cout<<"CLient Say:"<<clibuff<<endl;
       sendto(sockSer,serbuff,strlen(serbuff)+1,0,(sockaddr*)&addrSer,len);
    }
    return 0;
}

//cli.cpp

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define IP "127.0.0.1"

using namespace std;
int main()
{
    int sockCli = socket(AF_INET,SOCK_DGRAM,0);
    sockaddr_in addrSer;
    addrSer.sin_family = AF_INET;
    addrSer.sin_port = htons(PORT);
    addrSer.sin_addr.s_addr = inet_addr(IP);

    socklen_t len = sizeof(sockaddr);
    char clibuff[]="hello! I am client!";
    char serbuff[255];
    while(1)
    {
      sendto(sockCli,clibuff,strlen(clibuff)+1,0,(sockaddr*)&addrSer,len);
      recvfrom(sockCli,serbuff,sizeof(serbuff),0,(sockaddr*)&addrSer,&len);
    cout<<"Server Say:>"<<serbuff<<endl;
    sleep(2);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

windows和linux中UDP编程

标签:udp编程

原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/47082449

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