udp C/S架构编程实验

udp C/S架构编程实验

    • 实验目的
    • 实验内容
    • 实验原理
    • 程序设计
    • 运行结果
    • 实验总结

实验目的

  1. 了解 udp C/S架构
  2. 掌握 udp C/S架构的实现

实验内容

  1. 实现UDP服务器(server)
  2. 实现UDP客户端(client)

实验原理

UDP协议没有建立来凝结特性,所以UDP协议没有自动记录对方IP和端口号的特点,每次发送数据时,必须亲自制定和对方的IP和端口号,只有这样才能把数据发送给对方

  • UDP通信过程:
    • 调用socket创建套接字
    • bind绑定固定的IP和端口号
    • 调用sendto和recvfrom函数,发送和接收数据

程序设计

udp send.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 5555

int main()
{
     
    int sockfd;

    char buffer[1024];

    struct sockaddr_in s_addr;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd <0)
    {
     
	perror("socket error!\n");
	exit(1);
    }

    int opt = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    bzero(&s_addr, sizeof(struct sockaddr_in));
    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(PORT);
    s_addr.sin_addr.s_addr = inet_addr("192.168.182.128");
    
    if(bind(sockfd, (struct sockaddr *)(&s_addr), sizeof(struct sockaddr_in)) < 0)
    {
     
	perror("bind error!");
	exit(1);
    }

    while(1)
    {
     
        bzero(&s_addr, sizeof(struct sockaddr_in));
        s_addr.sin_family = AF_INET;
        s_addr.sin_port = htons(5554);
        s_addr.sin_addr.s_addr = inet_addr("192.168.182.128");
	memset(buffer, 0, sizeof(buffer));
        printf("Please input send message:\n");
	scanf("%s", buffer);
	sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr*)(&s_addr), sizeof(struct sockaddr_in));
	usleep(3);
    }


    return 0;
}

udp recv.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 5554

int main()
{
     
    int sockfd;
    int addr_len;

    char buffer[1024];

    struct sockaddr_in s_addr;
    struct sockaddr_in c_addr;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
     
	perror("socket error!\n");
	exit(1);
    }

    int opt = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    bzero(&s_addr, sizeof(struct sockaddr_in));
    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(PORT);
    s_addr.sin_addr.s_addr = inet_addr("192.168.182.128");
    
    if(bind(sockfd, (struct sockaddr *)(&s_addr), sizeof(struct sockaddr_in)) < 0)
    {
     
	perror("bind error!");
	exit(1);
    }

    while(1)
    {
     
        bzero(&s_addr, sizeof(struct sockaddr_in));
        s_addr.sin_family = AF_INET;
        s_addr.sin_port = htons(5554);
        s_addr.sin_addr.s_addr = inet_addr("192.168.182.128");
	memset(buffer, 0, sizeof(buffer));
	addr_len = sizeof(struct sockaddr_in);
	recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)(&c_addr), &addr_len);
	printf("recv = %s\n", buffer);
	usleep(3);
    }


    return 0;
}

运行结果

root@jsetc-virtual-machine:/0504# ./udps
Please input send message:
hello
Please input send message:
what is your name
Please input send message:
Please input send message:
Please input send message:
Please input send message:
^C
root@jsetc-virtual-machine:/0504# ./udpr
recv = hello
recv = what
recv = is
recv = your
recv = name
^C	

实验总结

UDP的客户端实现和TCP的客户端实现可以说是一模一样的,但是UDP服务器和TCP略有不同!
由于UDP是“无连接”的,所以,服务器端不需要额外创建监听套接字,只需要指定好IP和port,然后监听该地址,等待客户端与之建立连接,即可通信,且UDP服务器不需要并发,也可以说本身就支持并发。

你可能感兴趣的:(Linux学习笔记,socket,udp)