linux(armlinux)下UDP協議實現IP多播代碼

來源:互聯網
上載者:User

/****************************************************
*   功能:   linux  arm-linux  UPD多播       *
*   時間:   2006-03-07               *
*   撰寫:   李亞楠                 * 
*****************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int port = 6789;                       //通訊連接埠
int main(void)
 {
 int    socket_descriptor;
 struct sockaddr_in address;
  socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
  if (socket_descriptor == -1)
  {
     perror("Opening socket");
     exit(EXIT_FAILURE);
  }
  memset(&address, 0, sizeof(struct sockaddr_in));
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = inet_addr("233.0.0.1");
  address.sin_port = htons(port);
  while(1)
 {
    if(sendto(socket_descriptor, "test from broadcast", sizeof("test from broadcast"), 0, (struct sockaddr *)&address, sizeof(address)) < 0)
    {
     printf("sendto/n");
     exit(EXIT_FAILURE);
    }
    sleep(2);
    }
   exit(EXIT_SUCCESS);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.