Linux平台C语言Socket编程练习之UDP套接字

作者: 分类: 学习记录 评论: 暂无 时间: 2016-11-25 浏览: 1,126 次

0x00 要求

实现一个基于UDP协议的服务器-客户端程序,要求完成以下功能:

服务端

接收客户的连接请求,并发送欢迎信息,显示客户的IP地址和端口号;
 
循环接收接收客户传来的字符串,反转后传递给客户; 

客户端

从命令行读入服务器的IP地址;并连接到服务器;
 
循环从命令行读入一行字符串,并传递给服务器,由服务器对字符串反转,并将结果返回客户程序,如果用户输入的是quit,则关闭连接;
 
客户程序显示反转后的字符串;

0x01 代码

服务端

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

#define PORT 1234
#define MAXDATASIZE 100

int main()
{
    int sockfd;
    socklen_t client_len;
    ssize_t recv_num,send_num;
    struct sockaddr_in server;
    struct sockaddr_in client;
    char recv_buf[MAXDATASIZE];
    char send_buf[MAXDATASIZE];
    char response[MAXDATASIZE];
    char wel_msg[]="[!] Welcome,I can return your input,you can input 'quit' to exit:)";

    if((sockfd = socket(AF_INET,SOCK_DGRAM,0))==-1)
    {
        perror("socket() failed.");
        exit(1);
    }

    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
    server.sin_addr.s_addr=htonl(INADDR_ANY);

    client_len=sizeof(client);

    if((bind(sockfd,(struct sockaddr *)&server,sizeof(server)))==-1)
    {
        perror("bind() failed.");
        exit(1);
    }

    if((recvfrom(sockfd,response,MAXDATASIZE,0,(struct sockaddr *)&client,&client_len))==-1)
    {
        perror("recvfrom() failed.");
        exit(1);
    }

    printf("[!] You got a connection: message <%s> client ip <%s> port <%d>\n",response,inet_ntoa(client.sin_addr),ntohs(client.sin_port));


    if((sendto(sockfd,wel_msg,sizeof(wel_msg),0,(const struct sockaddr *)&client,client_len))==-1)
    {
        perror("sendto() failed");
        exit(1);
    }

    while(1)
    {
        if((recv_num=recvfrom(sockfd,recv_buf,MAXDATASIZE,0,(struct sockaddr *)&client,&client_len))==-1)
        {
            perror("recvfrom() failed.");
            exit(1);
        }

        recv_buf[recv_num]='\0';
        printf("[>] Receive client inputed string:%s\n",recv_buf);
        printf("[*] Return client inputed string:%s\n",recv_buf);

        strcpy(send_buf,recv_buf);

        if((send_num=(sendto(sockfd,send_buf,sizeof(send_buf),0,(struct sockaddr *)&client,client_len)))==-1)
        {
            perror("sendto() failed");
            exit(1);
        }

        if(!strcmp(recv_buf,"quit"))
        {
            printf("[!] Client request to exit.\n");
            close(sockfd);
            exit(1);
        }
    }
    close(sockfd);
    return 0;
}

客户端

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

#define PORT 1234
#define MAXDATASIZE 100

int main(int argc,char *argv[])
{
    int sockfd;
    ssize_t recv_num,send_num;
    socklen_t server_len;
    char recv_buf[MAXDATASIZE];
    char send_buf[MAXDATASIZE];
    char request[]="hello";
    struct hostent *remote_host;
    struct sockaddr_in server;

    if(argc!=2)
    {
        printf("Usage:%s <IP Address>\n",argv[0]);
        exit(1);
    }

    if((remote_host=gethostbyname(argv[1]))==NULL)
    {
        printf("gethostbyname() failed.");
        exit(1);
    }

    if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
    {
        perror("socket() failed.");
        exit(1);
    }

    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
    server.sin_addr=*((struct in_addr *)remote_host->h_addr);
    server_len=sizeof(server);

    if(sendto(sockfd,request,sizeof(request),0,(struct sockaddr *)&server,sizeof(server))==-1)
    {
        perror("sendto() failed.");
        exit(1);
    }

    if((recv_num=recvfrom(sockfd,recv_buf,MAXDATASIZE,0,(struct sockaddr*)&server,&server_len))==-1)
    {
        perror("recvfrom() failed.");
        exit(1);
    }

    printf("%s\n",recv_buf);

    while(1)
    {
        printf("[*] Please input ASCII string:");
        scanf("%s",send_buf);
        send_buf[strlen(send_buf)]='\0';

        if((send_num=sendto(sockfd,send_buf,strlen(send_buf),0,(struct sockaddr *)&server,sizeof(server)))==-1)
        {
            perror("sendto() failed.");
            exit(1);
        }

        if((recv_num=recvfrom(sockfd,recv_buf,MAXDATASIZE,0,(struct sockaddr*)&server,&server_len))==-1)
        {
            perror("recvfrom() failed.");
            exit(1);
        }

        printf("[>] Server return string:%s\n",recv_buf);

        if(!strcmp(recv_buf,"quit"))
        {
            printf("[!] You inputed 'quit' to exit.\n");
            close(sockfd);
            exit(1);
        }
    }
    close(sockfd);
    return 0;
}

0x02 演示

服务端

UDP 服务端.gif

客服端

UDP 客户端.gif

0x03 心得

UDP使用recvfrom函数接收数据,使用sendto函数发送数据,注意它们的传输的参数。

转载请注明来自4ido10n博客的Linux平台C语言Socket编程练习之UDP套接字,否则保留追究文章版权的权利!

订阅本站(RSS)

添加新评论