⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listener.c

📁 基于ARM9嵌入式开发板LINUX环境下的程序代码-linux udp
💻 C
字号:
/*****************************************Copyright (c)****************************************************                               Guangzhou Zhiyuan Electronic Co.,LTD.**                                     graduate school**                                 http://www.zyinside.com****------------------------------------- File Info ------------------------------------------------------** File name:           listener.c** Last modified Date:  2005-12-30** Last Version:        1.0** Descriptions:        listener of UDP.**------------------------------------------------------------------------------------------------------** Created by:          Ganda** Created date:        2005-12-27** Version:             1.0** Descriptions:        Preliminary version.****------------------------------------------------------------------------------------------------------** Modified by:		Chenxibing** Modified date:	2005-12-30	** Version:		1.0.1** Descriptions:	modified for MagicARM2410.***********************************************************************************************************/#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/wait.h>#include <sys/socket.h>                                  #define PORT 5000                      // The port which is communicate with server#define BACKLOG 10#define LENGTH  512                    // Buffer length                                                                                 int main (){   int sockfd;                        // Socket file descriptor    int nsockfd;                       // New Socket file descriptor    int num;    int sin_size;                      // to store struct size    char revbuf[LENGTH];               // Send buffer    struct sockaddr_in addr_local;         struct sockaddr_in addr_remote;                       /* Get the Socket file descriptor */      if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 )      {           printf ("ERROR: Failed to obtain Socket Despcritor.\n");        return (0);    }    else    {        printf ("OK: Obtain Socket Despcritor sucessfully.\n");    }        /* Fill the local socket address struct */    addr_local.sin_family = AF_INET;           // Protocol Family    addr_local.sin_port = htons(PORT);         // Port number    addr_local.sin_addr.s_addr  = INADDR_ANY;  // AutoFill local address    bzero(&(addr_local.sin_zero), 8);          // Flush the rest of struct    /*  Blind a special Port */    if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )    {      	  printf ("ERROR: Failed to bind Port %d.\n",PORT);        return (0);    }    else    {        printf("OK: Bind the Port %d sucessfully.\n",PORT);    }       sin_size = sizeof(struct sockaddr);       if(num = recvfrom(sockfd, revbuf, LENGTH, 0, (struct sockaddr *)&addr_remote, &sin_size) == -1)     {        printf("ERROR!\n");    }      else    {        printf("OK: %s.\n",revbuf);    }    close(sockfd);      return (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -