Chinaunix首页 | 论坛 | 博客
  • 博客访问: 432388
  • 博文数量: 55
  • 博客积分: 2235
  • 博客等级: 大尉
  • 技术积分: 625
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-14 13:34
文章分类
文章存档

2012年(2)

2011年(26)

2010年(1)

2009年(20)

2008年(6)

我的朋友

分类: LINUX

2008-07-21 17:46:31

 
UDP编程的服务器端一般步骤是:

1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt();* 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();
4、循环接收数据,用函数recvfrom();
5、关闭网络连接;


UDP编程的客户端一般步骤是:

1、创建一个socket,用函数socket();
2、设置socket属性,用函数setsockopt();* 可选
3、绑定IP地址、端口等信息到socket上,用函数bind();* 可选
4、设置对方的IP地址和端口等属性;
5、发送数据,用函数sendto();
6、关闭网络连接;

 
 
UDP的服务端:
 

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


#define MAXLINE 80
int port = 8000;

int main(void)
{
  struct sockaddr_in sin;
  struct sockaddr_in rin;
  int sock_fd;
  int address_size;
  char buf[MAXLINE];
  char str[INET_ADDRSTRLEN];
  int i;
  int len;
  int n;

  bzero(&sin, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons(port);


  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (-1 == sock_fd)
  {
    perror("call to socket");
    exit(1);
  }
  n = bind(sock_fd, (struct sockaddr *)&sin, sizeof(sin));
  if (-1 == n)
  {
    perror("call to bind");
    exit(1);
  }

  while(1)
  {
    address_size = sizeof(rin);
    n = recvfrom(sock_fd, buf, MAXLINE, 0, (struct sockaddr *)&rin, &address_size);
    if (-1 == n)
    {
      perror("call to recvfrom.\n");
      exit(1);
    }
    printf("you ip is %s at port %d:%s\n",
            inet_ntop(AF_INET, &rin.sin_addr,str,sizeof(str)),
            ntohs(rin.sin_port),buf);
    len = strlen(buf);
    for (i = 0; i < len; i++)
    {
      buf[i] = toupper(buf[i]);
    }
    n = sendto(sock_fd, buf, len+1, 0, (struct sockaddr *)&rin, address_size);
    if (-1 == n)
    {
      perror("call to sendto\n");
      exit(1);
    }
  }
}

 

UDP客户端:

 

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

#define MAXLINE 80
int port = 8000;

int main(int argc, char *argv[])
{

  struct sockaddr_in pin;
  struct sockaddr_in rin;
  int sock_fd;
  char buf[MAXLINE];
  char str[MAXLINE];
  char sip[INET_ADDRSTRLEN];
  int n;
  int address_size;

  bzero(&pin, sizeof(pin));
  pin.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &pin.sin_addr);
  pin.sin_port = htons(port);
  
  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (-1 == sock_fd)
  {
    perror("call to socket");
    exit(1);
  }
  while(NULL != fgets(str,MAXLINE, stdin))
  {
    sendto(sock_fd, str, strlen(str) + 1, 0, (struct sockaddr *)&pin, sizeof(pin));
    if (-1 == n)
    {
      perror("call to sendto.\n");
      exit(1);
    }
    
    address_size = sizeof(rin);
    n = recvfrom(sock_fd, buf, MAXLINE, 0, (struct sockaddr *)&rin, &address_size);
    if (-1 == n)
    {
      perror("call to recvfrom.\n");
      exit(1);
    }
    else
    {
      printf("Response from %s port %d:%s\n",
            inet_ntop(AF_INET, &rin.sin_addr, sip, sizeof(sip)),
            ntohs(rin.sin_port),buf);
    }
  }
  close(sock_fd);
  if (-1 == n)
  {
    perror("call to close.\n");
    exit(1);
  }
  return 0;
}

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