Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36735
  • 博文数量: 8
  • 博客积分: 1482
  • 博客等级: 上尉
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-18 13:55
文章分类

全部博文(8)

文章存档

2011年(1)

2008年(7)

我的朋友

分类: C/C++

2008-10-28 12:48:30

   一个基于UDP的广播程序,包括服务器和客户端的代码,都很简单,就是在客户端广播一个消息,这样登录的服务器都能收到消息了。简单就不多说了,代码如下:

/*
 * filename: broadcast_client.c
 * DESC: the client endpoint of the UDP broadcast program
 * Author: linjian
 * Date: 2008/10/28
 */


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "../my_error.h"

#define PORT 8003
#define BUF_SIZE 512

int main( int argc, char *argv[] )
{
    int sockfd;
    int optval;
    struct sockaddr_in serv_addr;
    char send_buf[ BUF_SIZE ];

    if ( argc != 2 ) {
        printf( "usage: ./broadcast_client [ip] \n" );
        exit ( EXIT_FAILURE );
    }

    /* we have to use the UDP for only the UDP support broadcast*/
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
        my_error( "socket failed" );

    /* set the broadcast option */
    optval = 1;
    if ( setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,
                 (void *)&optval, sizeof(int)) < 0 )
        my_error( "setsockopt" );

    memset( &serv_addr, 0, sizeof(struct sockaddr_in) );
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons( PORT );
    serv_addr.sin_addr.s_addr = inet_addr( argv[1] );

    printf( "input the string will be send:\n" );
    fgets( send_buf, BUF_SIZE, stdin );

    if ( sendto(sockfd, send_buf, strlen(send_buf), 0,
               (struct sockaddr *)&serv_addr,

                sizeof(struct sockaddr)) < 0 )
        my_error( "send failed" );

    close( sockfd );

    return 0;
}


/*
 * filename: broadcast_serv.c
 * DESC: the sever endpoint of the UDP broadcast program
 * Author: linjian
 * Date: 2008/10/28
 */


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "../my_error.h"

#define PORT 8003
#define BUF_SIZE 512

int main( int argc, char *argv[] )
{
    int sockfd, connfd;
    int optval;
    int client_len;
    struct sockaddr_in serv_addr, client_addr;
    char recv_buf[ BUF_SIZE ];

    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
        my_error( "socket failed" );

    memset( &serv_addr, 0, sizeof(struct sockaddr_in) );
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons( PORT );
    serv_addr.sin_addr.s_addr = htons( INADDR_ANY );

    if ( bind(sockfd, (struct sockaddr *)&serv_addr,

              sizeof(struct sockaddr)) < 0 )
        my_error( "bind failed" );

    client_len = sizeof( struct sockaddr_in );
    if ( recvfrom(sockfd, recv_buf, BUF_SIZE, 0,

                 (struct sockaddr *)&client_addr, 

                  &client_len) < 0 )
        my_error( "recvfrom failed" );

    printf( "recv from client: %s\n", recv_buf );

    close( sockfd );

    return 0;
}

//客户端这边发送信息

ken@ken-laptop:~/linux_c/$./broadcase_client 192.168.18.255
input the string will be send:
This is ken

//服务器接受到了信息了

ken@kenlaptop:~/linux_c$ ./broadcase_serv
recv from client: This is ken

阅读(1859) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~