Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28810
  • 博文数量: 17
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-18 15:15
文章分类
文章存档

2010年(17)

我的朋友
最近访客

分类: C/C++

2010-05-13 13:16:25

服务器端代码:

 


/*
* udp-s.c
* Author: Alvin
* 基于UDP的服务器端代码
* 功能:服务器将客户端发来的内容倒序后发回到客户端
*
*/

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

#define MYPORT 6768
#define BUFSIZE 100

int main(int argc, char *argv[])
{
         int sockfd;
         int new_fd,numbytes,ret;
         struct sockaddr_in udp_addr;
         struct sockaddr_in their_addr;
         int their_addr_len=sizeof(their_addr);
         int sin_size,con_fd,pid;
         char buf[BUFSIZE];
         char send[BUFSIZE];

         sockfd=socket(AF_INET,SOCK_DGRAM,0);
         int i;
         if(sockfd<0)
         {
            printf("error in socket");
            exit(1);
        }

         udp_addr.sin_family=AF_INET;
        udp_addr.sin_port=htons(MYPORT);
        udp_addr.sin_addr.s_addr=htonl(INADDR_ANY);

        ret=bind(sockfd,(struct sockaddr *)&udp_addr,sizeof(udp_addr));
        if(ret<0)
         {
      printf("error in binding\n");
      exit(1);
    }
         while(1)
         {
                   ret = recvfrom(sockfd,buf,BUFSIZE,0,(struct sockaddr *)&their_addr,&their_addr_len);
                   if(ret<0)
                   {
                            printf("error recive\n");
                            exit(1);
                   }
                   buf[ret]='\0';
                   printf("UDP server recive:%s\n",buf);

                   for (i=0;i<ret;i++)
                            send[i]=buf[ret-1-i];
                   send[ret]='\0';
                   sendto(sockfd,send,ret,0,(struct sockaddr *)&their_addr,sizeof(their_addr));
                   printf("UDP server sent: %s\n",send);
             sleep(1);
         }
         return 0;
}




客户端代码:


/*
* udp-c.c
* Author: Alvin
* 基于UDP的客户端代码
* 功能:向服务器发送内容,并接收来自服务端的回复
*
*/



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

#define MYPORT 6768
#define BUFSIZE 20

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        printf("\nUsing: ./udp-c IP string\n");
        printf("\tThe length of the string should not bigger than 20.\n\n");
        exit(0);
    }

     int sockfd;
         int new_fd,numbytes,ret;
         struct sockaddr_in udp_addr;
         struct sockaddr_in their_addr;
         int their_addr_len=sizeof(their_addr);
         int sin_size,con_fd,pid;
         int buflen;
         char recive[BUFSIZE];

        sockfd=socket(AF_INET,SOCK_DGRAM,0);
         if(sockfd<0)
         {
            printf("error in socket");
            exit(1);
        }

         udp_addr.sin_family=AF_INET;
        udp_addr.sin_port=htons(MYPORT);
        udp_addr.sin_addr.s_addr=inet_addr(argv[1]);
         buflen=strlen(argv[2]);
         while(1)
         {
                   ret = sendto(sockfd,argv[2],buflen,0,(struct sockaddr *)&udp_addr,sizeof(udp_addr));
                   if(ret<0)
                   {
                            printf("error recive\n");
                            exit(1);
                   }
                   printf("UDP client sent:%s\n",argv[2]);
                   sleep(1);

                   ret = recvfrom(sockfd,recive,BUFSIZE,0,(struct sockaddr *)&their_addr,&their_addr_len);
                   if(ret<0)
                   {
                            printf("error recive\n");
                            exit(1);
                   }
                   recive[ret]='\0';

                   printf("UDP client recive:%s\n",recive);
 
        }

       return 0;
}


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