Chinaunix首页 | 论坛 | 博客
  • 博客访问: 295814
  • 博文数量: 52
  • 博客积分: 814
  • 博客等级: 军士长
  • 技术积分: 689
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-21 19:41
文章分类
文章存档

2017年(1)

2016年(2)

2014年(1)

2012年(42)

2011年(6)

UDP

分类: LINUX

2012-05-06 15:57:46

 
 
 
UDP 与TCP相比来说,具有占用系统资源小的优点。但是缺点多多,UDP(用户数据报协议),不提供可靠性。
网络编程这一块,首先被提及到的就是握手协议。UDP通讯模块,如下图:
 
在UDP这里,握手显的不是很重要,作为server端,只管发送,不管client端那边是否得到信息。看起来有点不负责人,所以就是不可靠的协议,无连接的协议。
 
涉及到网络协议的开发,套接口这个概念需要弄清楚.
套接口:  网络进程的ID,其实网络之间的通信,说白了就是进程间的通信。因为每一个运行的任务就是一个进程,这个任务就是通信。
一个IP地址代表了一台PC,但是一台PC可以运行很多的process,那么怎么确定是哪个进程与哪个进程通信,这里socket就给出了一个解决方案。每个进程都有一个port,端口号与进程是one to one,这就确立了进程的唯一性,独立性,这样port号就明确进程。
 
socket()套接口 ===== IP ADDRESS PROT NUMBER
 
 
监听端                                                发话端
 
1 取得UDP()套接字                                  取得UDP套接字
    socket()                                         socket()
 
填入本地IP和port信息                                 填入远端IP和port信息
 
 
本地ip&prot 与bind()相连
 
 
 
等待接收 recvfrom()      <-------------------------------- 向远端发送数据 sendto()
 
 
 
 
 
打印接收信息()printf()                                   打印发送成功信息 printf()
 
 
 
删除套接字close()                                                   删除套接字()
 
 
 
结束                                                                       结束
 
 
 
在ARMlinux端运行  talk 程序,在宿主机运行lister 程序, 实现ARMLINXU发送字符到宿主机。
负载 talk.c
 
#include
#include
#include
#include
#include
#include
#include
#include

#define PORT 5000                      // The port which is communicate with server
#define LENGTH 512                     // Buffer length
int main(int argc, char *argv[])
{   int sockfd;                        // Socket file descriptor
    int num;                           // Counter of received bytes 
    char sdbuf[LENGTH];                // Receive buffer
    struct sockaddr_in addr_remote;    // Host address information
    char sdstr[]= {"MagicARM2410 UDP Experiment."};   
   
    /* Check parameters number */
    if (argc != 2)                    
    {   
        printf ("Usage: talker HOST IP (ex: ./talker 192.168.0.94).\n");
        return (0);
    }
    /* Get the Socket file descriptor */
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        printf("ERROR: Failed to obtain Socket Descriptor!\n");
        return (0);
    }
   
    /* Fill the socket address struct */
    addr_remote.sin_family = AF_INET;                   // Protocol Family
    addr_remote.sin_port = htons(PORT);                 // Port number
    inet_pton(AF_INET, argv[1], &addr_remote.sin_addr); // Net Address
    bzero(&(addr_remote.sin_zero), 8);                  // Flush the rest of struct
 
    /* Try to connect the server */
    bzero(sdbuf,LENGTH);
    num = sendto(sockfd, sdstr, strlen(sdstr), 0, (struct sockaddr *)&addr_remote, sizeof(struct sockaddr_in));
    if( num < 0 )
    {
            printf ("ERROR: Failed to send your data!\n", argv[1], num);
    }
    else
    {
            printf ("OK: Sent to %s total %d bytes !\n", argv[1], num);
    }
    close (sockfd);
    return (0);
}
 
 
listen.c 程序
 
#include
#include
#include
#include
#include
#include
#include
#include
                                 
#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);
}
 
 
分别编译后运行可执行文件。
阅读(1130) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~