Experiment 0x1:UDP套接字编程

0x0 说明

实验一:TCP套接字编程或UDP套接字编程

此篇为第二部分:UDP套接字编程

记录实验课代码

代码环境:

win10

VS2019 远程连接 ubuntu20

进行linux编程

0x1 要求

要求:实现一个基于UDP协议的对等用户通信程序,要求完成以下功能。

  • 通信程序:
    • 从命令行读入对等端的IP地址;
    • 与对等端实现通话和数据的屏幕回显.

0x2 实现

实现一个基于UDP协议的对等用户通信程序

在这里插入图片描述

0x3 源码

1- UDP服务端源码

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 2333
#define MAXDATASIZE 100
int main() {
	int sockfd;
	sockaddr_in server;
	sockaddr_in client;
	

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd == -1) {
		perror("socket() error!");
		_exit(1);
	}

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

	int flag = bind(sockfd, (sockaddr*)&server, sizeof(server));
	if (flag == -1) {
		perror("bind() error!");
		_exit(1);
	}
	socklen_t len = sizeof(sockaddr_in);
	int num;
	while (1) {
		char buf[MAXDATASIZE];
		num = recvfrom(sockfd, buf, MAXDATASIZE, 0, (sockaddr*)&client, &len);
		if (num < 0) {
			perror("recvfrom() error!");
			_exit(1);
		}
		buf[num] = '\0';
		printf("You got a message <%s> from cient.\n",buf);
		printf("It's ip is %s, port is %d.\n\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
		sendto(sockfd, buf, strlen(buf), 0, (sockaddr*)&client, len);
		if (!strcmp(buf, "bye")) {
			printf("had Disconnected, ip:%s\n", inet_ntoa(client.sin_addr));
			break;
		}
	}
	close(sockfd);

	return 0;
}

2- UDP客户端源码

UDP客户端源码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define PORT 2333
#define MAXDATASIZE 100
int main(int argc, char* argv[]) {
	int sockfd;

	hostent* he;
	sockaddr_in server, peer;
	if (argc != 2) {
		printf("Usage:%s<IP Adress>\n", argv[0]);
		_exit(1);
	}
	he = gethostbyname(argv[1]);
	if (he == NULL) {
		perror("gethostbyname() error!\n");
		_exit(1);
	}
	sockfd = socket(AF_INET, SOCK_DGRAM , 0);
	if (sockfd == -1) {
		perror("socket() error!\n");
		_exit(1);
	}
	bzero(&server, sizeof(sockaddr_in));
	server.sin_family = AF_INET;
	server.sin_port = htons(PORT);
	server.sin_addr = *(in_addr*)(he->h_addr_list[0]);


	while (1) {
		char buf[MAXDATASIZE] = { 0 };
		printf("please input: ");
		gets(buf);
		if (strlen(buf) > 100) {
			printf("input too long! must less 100 bytes!\n");
			_exit(1);
		}
		sendto(sockfd, buf, strlen(buf), 0, (sockaddr*)&server, sizeof(sockaddr_in));
		socklen_t len = sizeof(sockaddr_in);
		int num = 0;
		num = recvfrom(sockfd, buf, MAXDATASIZE, 0, (sockaddr*)&peer, &len);
		if (num == -1) {
			perror("recvfrom() error!\n");
			_exit(1);
		}
		if (len != sizeof(sockaddr_in) || memcmp((void*)&server, (void*)&peer, len) != 0) {
			printf("Receive message from other server.\n");
			continue;
		}
		buf[num] = '\0';
		printf("Server Message: %s.\n\n", buf);
		if (!strcmp(buf, "bye")) {
			printf("had Disconnected!\n");
			break;
		}
	}
	close(sockfd);


	return 0;
}
Logo

更多推荐