任务详情
基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现:
- time服务器的客户端服务器,提交程序运行截图
- echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid 你的学号 姓名 echo :”返回给客户端
- 服务器部署到华为云服务器,客户端用Ubuntu虚拟机。
- 要用多线程或者多进程实现,至少连接两个客户端。
- 把服务器部署到试验箱。(加分项)
查看系统ip
time服务器的客户端服务器,提交程序运行截图
timeserver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXBUF 1000
#define PORT 1203
int main(int argc, char const *argv[])
{
int sock = 0;
int n = 0;
int reuse = 1;
int cli_len = sizeof(struct sockaddr);
char strbuf[MAXBUF] = {0};
struct sockaddr_in addr, cli;
bzero(&addr, sizeof(addr));
addr.sin_family = PF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
perror("socket");
return -1;
}
setsockopt(sock , SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
return -1;
}
while(1){
memset(strbuf, 0, MAXBUF);
n = recvfrom(sock, strbuf, MAXBUF, 0, (struct sockaddr *)&cli, &cli_len);
if(n<0){
perror("recvfrom");
return -1;
}else{
printf("pid is %d\n",getpid()); //process pid
printf(" time is %s\n", strbuf);
}
}
return 0;
}
timeclient.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXBUF 1000
#define PORT 1203
int main(int argc, char const *argv[])
{
int sock = 0;
int n = 0;
int reuse = 1;
struct sockaddr_in srvsock;
char timebuf[MAXBUF] = {0};
if (argc != 2)
{
printf("Usage:%s ServerIP\n", argv[0]);
return -1;
}
bzero(&srvsock, sizeof(srvsock));
srvsock.sin_family = PF_INET;
srvsock.sin_addr.s_addr = inet_addr(argv[1]);
srvsock.sin_port = htons(PORT);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock<0){
perror("socket");
return -1;
}
while(1){
sleep(1);
time_t t=time(NULL);
strcpy(timebuf,ctime(&t));
if ((n = sendto(sock, timebuf, strlen(timebuf), 0, (struct sockaddr *) &srvsock, sizeof(struct sockaddr))) < 0)
{
perror("sendto");
return -1;
}else{
printf("time is %s\n", timebuf);
}
}
}
运行截图
echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid 你的学号 姓名 echo :”返回给客户端
echoserver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXBUF 1000
#define PORT 1203
int main(int argc, char const *argv[])
{
int sock = 0;
int n = 0;
int reuse = 1;
int cli_len = sizeof(struct sockaddr);
char strbuf[MAXBUF] = {0};
struct sockaddr_in addr, cli;
bzero(&addr, sizeof(addr));
addr.sin_family = PF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
perror("socket");
return -1;
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
return -1;
}
while(1){
memset(strbuf, 0, MAXBUF);
n = recvfrom(sock, strbuf, MAXBUF, 0, (struct sockaddr *)&cli, &cli_len);
if(n<0){
perror("recvfrom");
return -1;
}else{
printf("pid:%d \n echo:\n",getpid());
printf("收到来自%s(端口%d)的消息: %s\n",inet_ntoa(cli.sin_addr), PORT, strbuf);
}
}
return 0;
}
echoclient.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAXBUF 1000
#define PORT 1203
int main(int argc, char const *argv[])
{
int sock = 0;
int n = 0;
int reuse = 1;
struct sockaddr_in srvsock;
char strbuf[MAXBUF] = {0};
if (argc != 2)
{
printf("Usage:%s ServerIP\n", argv[0]);
return -1;
}
bzero(&srvsock, sizeof(srvsock));
srvsock.sin_family = PF_INET;
srvsock.sin_addr.s_addr = inet_addr(argv[1]);
srvsock.sin_port = htons(PORT);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock<0){
perror("socket");
return -1;
}
while(1){
memset(strbuf, 0, MAXBUF);
fgets(strbuf, MAXBUF, stdin);
if ((n = sendto(sock, strbuf, strlen(strbuf), 0, (struct sockaddr *) &srvsock, sizeof(struct sockaddr))) < 0)
{
perror("sendto");
return -1;
}else{
printf("send to %s(port=%d) len %d:%s\n", argv[1], PORT, n, strbuf);
}
}
}