真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C語言UDP傳輸系統(tǒng)源碼

 本文實例為大家分享了C語言UDP傳輸系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、延安網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、html5商城網(wǎng)站制作、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為延安等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

/*加載庫文件*/
#pragma comment( lib, "ws2_32.lib" )
/*加載頭文件*/
#include 
#include 
#include 
#include 


/*定義多播常量*/
#define MCASTADDR   "224.3.5.8"
#define MCASTPORT   25000
#define BUFSIZE    1024
#define MCOUNT    10

/*定義廣播常量*/
#define BCASTPORT   5050
#define BCOUNT    10

/*定義廣播全局變量*/
SOCKET       socketBro;
SOCKET       socketRec;
struct sockaddr_in addrBro;
struct sockaddr_in addrRec;
BOOL        broadSendFlag;
BOOL        broadFlag;

DWORD       bCount;
DWORD       bcastAddr;
short       bPort;

/*定義多播全局變量*/
SOCKET       socketMul;
SOCKET       sockJoin;
struct sockaddr_in addrLocal;
struct sockaddr_in addrMul;

BOOL        multiSendFlag;
BOOL        bLoopBack;  
BOOL        multiFlag;

DWORD       dwInterface; 
DWORD       dwMulticastGroup;
DWORD       mCount;     
short       mPort;      

/*自定義函數(shù)*/
void initial();
void GetArgments(int argc, char **argv);

void userHelpAll();
void userHelpBro();
void userHelpMul();

void broadcastSend();
void broadcastRec();

void mulControl();
void multicastSend();
void multicastRec();

/*初始化全局變量函數(shù)*/
void initial()
{
  /*初始化廣播全局變量*/
  bPort = BCASTPORT;
  bCount = BCOUNT;
  bcastAddr = INADDR_BROADCAST;
  broadSendFlag = FALSE;
  broadFlag = FALSE;
  multiFlag = FALSE;

  /*初始化多播全局變量*/
  dwInterface = INADDR_ANY;
  dwMulticastGroup = inet_addr(MCASTADDR);
  mPort = MCASTPORT;
  mCount = MCOUNT;
  multiSendFlag = FALSE;
  bLoopBack = FALSE;
}

/*參數(shù)獲取函數(shù)*/
void GetArgments(int argc, char **argv)
{
  int i;
  /*如果參數(shù)個數(shù)小于2個*/
  if(argc<=1)
  {
    userHelpAll();
    return ;
  }
  /*獲取廣播選項*/
  if(argv[1][0]=='-'&&argv[1][1]=='b')
  {
    /*廣播標志設(shè)置為真*/
    broadFlag = TRUE;
    for(i=2; i < argc ;i++)
   {
      if (argv[i][0] == '-')
   {
        switch (tolower(argv[i][1]))
     {
          /*如果是發(fā)送者*/
         case 's': 
            broadSendFlag = TRUE;
        break;
          /*廣播的地址*/
         case 'h':
            if (strlen(argv[i]) > 3)
              bcastAddr = inet_addr(&argv[i][3]);
        break;
          /*廣播的端口號*/
         case 'p':
            if (strlen(argv[i]) > 3)
              bPort = atoi(&argv[i][3]);
        break;
          /*廣播(接收或者發(fā)送)的數(shù)量*/
         case 'n': 
            bCount = atoi(&argv[i][3]);
        break;
          /*其他情況顯示用戶幫助,終止程序*/
         default:
      {
             userHelpBro();
             ExitProcess(-1);
      }
        break;
     }
   }
   }
    return ;
  }

  /*獲取多播選項*/
  if(argv[1][0]=='-'&&argv[1][1]=='m')
  {
    /*多播標志設(shè)置為真*/
    multiFlag = TRUE;
    for(i=2; i < argc ;i++)
   {
      if (argv[i][0] == '-')
   {
        switch (tolower(argv[i][1]))
     {
          /*如果是發(fā)送者*/
         case 's': 
            multiSendFlag = TRUE;
        break;
          /*多播地址*/
         case 'h': 
            if (strlen(argv[i]) > 3)
              dwMulticastGroup = inet_addr(&argv[i][3]);
        break;
          /*本地接口地址*/
         case 'i': 
            if (strlen(argv[i]) > 3)
              dwInterface = inet_addr(&argv[i][3]);
        break;
          /*多播端口號*/
         case 'p': 
            if (strlen(argv[i]) > 3)
              mPort = atoi(&argv[i][3]);
        break;
          /*環(huán)回標志設(shè)置為真*/
         case 'l': 
            bLoopBack = TRUE;
        break;
          /*發(fā)送(接收)的數(shù)量*/
         case 'n':
            mCount = atoi(&argv[i][3]);
        break;
          /*其他情況,顯示用戶幫助,終止程序*/
         default:
           userHelpMul();
        break;
     }
   }
   }
  
  }
  return;
}

/*全局用戶幫助函數(shù)*/
void userHelpAll()
{
  printf("Please choose broadcast[-b] or multicast[-m] !\n"); 
  printf("userHelpAll: -b [-s][p][-h][-n] | -m[-s][-h][-p][-i][-l][-n]\n");
  userHelpBro();
  userHelpMul();
}

/*廣播用戶幫助函數(shù)*/
void userHelpBro()
{
  printf("Broadcast: -b -s:str -p:int -h:str -n:int\n");
  printf("      -b   Start the broadcast program.\n");
  printf("      -s   Act as server (send data); otherwise\n");
  printf("         receive data. Default is receiver.\n");
  printf("      -p:int Port number to use\n ");
  printf("         The default port is 5050.\n");
  printf("      -h:str The decimal broadcast IP address.\n");
  printf("      -n:int The Number of messages to send/receive.\n");
  printf("         The default number is 10.\n");
}

/*多播用戶幫助函數(shù)*/
void userHelpMul()
{
  printf("Multicast: -m -s -h:str -p:int -i:str -l -n:int\n");
  printf("      -m   Start the multicast program.\n");
  printf("      -s   Act as server (send data); otherwise\n");
  printf("          receive data. Default is receiver.\n");
  printf("      -h:str The decimal multicast IP address to join\n");
  printf("          The default group is: %s\n", MCASTADDR);
  printf("      -p:int Port number to use\n");
  printf("          The default port is: %d\n", MCASTPORT);
  printf("      -i:str Local interface to bind to; by default \n");
  printf("          use INADDRY_ANY\n");
  printf("      -l   Disable loopback\n");
  printf("      -n:int Number of messages to send/receive\n");
  ExitProcess(-1);
}

/*廣播消息發(fā)送函數(shù)*/
void broadcastSend()
{
  /*設(shè)置廣播的消息*/
  char *smsg="The message received is from sender!";
  BOOL opt=TRUE;
  int nlen=sizeof(addrBro);
  int ret;
  DWORD i=0;
  
  /*創(chuàng)建UDP套接字*/
  socketBro=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_OVERLAPPED);
  /*如果創(chuàng)建失敗*/
  if(socketBro==INVALID_SOCKET)
  {
    printf("Create socket failed:%d\n",WSAGetLastError());
    WSACleanup();
    return;
  }
  
  /*設(shè)置廣播地址各個選項*/
  addrBro.sin_family=AF_INET;
  addrBro.sin_addr.s_addr=bcastAddr;
  addrBro.sin_port=htons(bPort);
  
  /*設(shè)置該套接字為廣播類型*/
  if (setsockopt(socketBro,SOL_SOCKET,SO_BROADCAST,(char FAR *)&opt,
                     sizeof(opt))==SOCKET_ERROR)
  /*如果設(shè)置失敗*/
  {
    printf("setsockopt failed:%d",WSAGetLastError());
    closesocket(socketBro);
    WSACleanup();
    return;
  }
  /*循環(huán)發(fā)送消息*/
  while(i\n", recvbuf,inet_ntoa(from.sin_addr));
   }
  /*關(guān)閉套接字、釋放占用資源*/
  closesocket(socketMul);
  WSACleanup();
}

/*主函數(shù)*/
int main(int argc, char **argv)
{
  WSADATA wsd;

  initial();
  GetArgments(argc, argv);

  /*初始化Winsock*/
  if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
  {
    printf("WSAStartup() failed\n");
    return -1;
  }

  /*如果是執(zhí)行廣播程序*/
  if(broadFlag)
  {
    /*以發(fā)送者身份發(fā)送消息*/
    if(broadSendFlag)
   {
      broadcastSend();
      return 0;
   }
    /*以接收者身份接收消息*/
    else
   {
      broadcastRec();
      return 0;
   }
  }

  /*如果是執(zhí)行多播程序*/
  if(multiFlag)
  {
    /*以發(fā)送者身份發(fā)送消息*/
    if(multiSendFlag) 
   {
      multicastSend();
      return 0;
    }
    /*以接收者身份接收消息*/
    else  
   {
      multicastRec();
      return 0;
   }
  }
  return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


網(wǎng)站名稱:C語言UDP傳輸系統(tǒng)源碼
URL標題:http://weahome.cn/article/pspoho.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部