2021-05-26

#include <stdlib.h>#include <errno.h>#include <string.h>#include <netdb.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <unistd.h>#include <stdio.h>#include <time.h>#define SERVPORT 3333#define SERVIP “192.168.199.128”#define MAXDATASIZE 100 /*每次最大数据传输量 */int main(int argc, char *argv[]){//获取时间time_t t;//将t声明为时间变量struct tm *p;//struct tm是一个结构体,声明一个结构体指针time(&t);p=localtime(&t);//获得当地的时间 int sockfd, recvbytes;char buf[MAXDATASIZE]; //用于发送数据的缓冲区struct hostent host; //用于接收服务主机信息struct sockaddr_in serv_addr;if (argc < 2) {fprintf(stderr,“Please enter the server’s hostname!\n”);exit(1);}//获取服务器主机信息if((host=gethostbyname(“192.168.199.128”))NULL) {herror(“gethostbyname error!”);exit(1);}//建立客户端三元组if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror(“socket create error!”);exit(1);}serv_addr.sin_family=AF_INET;serv_addr.sin_port=htons(SERVPORT);serv_addr.sin_addr = *((struct in_addr *)host->h_addr);bzero(&(serv_addr.sin_zero),8);//向服务器发送的信息printf(“收到: %s\n”,buf);printf("%d-%d-%d %d:%d:%d\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);close(sockfd);}
2.
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#include <arpa/inet.h>#include <sys/types.h>#include <unistd.h>#define SERVPORT 3333 /*服务器监听端口号 /#define BACKLOG 10 / 最大同时连接请求数 */int main(){ int sockfd,client_fd; /sock_fd:监听 socket;client_fd:数据传输 socket /struct sockaddr_in my_addr; / 本机地址信息 /struct sockaddr_in remote_addr; / 客户端地址信息 /int sin_size;if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror(“socket 创建失败!”); exit(1);}my_addr.sin_family=AF_INET;my_addr.sin_port=htons(SERVPORT);/ htons()把 16 位值从主机字节序转换成网络字节序/my_addr.sin_addr.s_addr = INADDR_ANY;bzero(&(my_addr.sin_zero),8); /*保持与 struct sockaddr 同样大小 */if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1){perror(“bind 出错!”);exit(1);}if (listen(sockfd, BACKLOG) == -1) {perror(“listen 出错!”);exit(1);}while(1) {sin_size = sizeof(struct sockaddr_in);if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size))
-1) {perror(“accept error”);continue;}printf(“收到一个连接来自: %s\n”, inet_ntoa(remote_addr.sin_addr));if (!fork()) { /
子进程代码段 */if (send(client_fd,“接收成功”, 26, 0) == -1)perror(“send 出错!”);close(client_fd);exit(0);}close(client_fd);}}
3.
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <netdb.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <arpa/inet.h> #include <unistd.h>#define SERVPORT 3333#define SERVIP “192.168.199.128”#define MAXDATASIZE 100int main(int argc, char *argv[]){ int sockfd, recvbytes; char buf[MAXDATASIZE]; //用于发送数据的缓冲区 struct hostent *host; //用来接收服务主机信息 struct sockaddr_in serv_addr; if (argc < 2){ fprintf(stderr,“Please enter the username!\n”); exit(1); } //获取服务器主机信息 if((host=gethostbyname(SERVIP)) == NULL) { herror(“gethostbyname error!”); exit(1); } //建立客户端三元组 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ perror(“socket create error!”); exit(1); } serv_addr.sin_family=AF_INET; serv_addr.sin_port=htons(SERVPORT); serv_addr.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(serv_addr.sin_zero),8); //置字节字符串my_addr.sin_zero的前n个字节为零且包括‘\0’ //向服务器发送连接 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1){ perror(“connect error!”); exit(1); } //发送数据给服务器 if (send(sockfd, argv[1], 26, 0) == -1){ perror(“send error!”); exit(0); } //接收服务器发送的信息 if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1){ perror(“receive error!”); exit(1); } buf[recvbytes] = ‘\0’; //输出服务器发送的信息 printf(“information: %s\n”,buf); close(sockfd);}
4.
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#include <arpa/inet.h> #include <unistd.h>#define MAXDATASIZE 100#define SERVPORT 3333 #define BACKLOG 10int main(){ int sockfd,client_fd,recvbytes; char buf[MAXDATASIZE]; //用于接收数据的缓冲区 char user[] = “liu”; //定义用户名 struct sockaddr_in my_addr; //存储客户端地址 struct sockaddr_in remote_addr; //存储服务端地址 sockfd = socket(AF_INET, SOCK_STREAM, 0) ; //建立三元组 my_addr.sin_family = AF_INET; my_addr.sin_port = htons(SERVPORT); //获取端口号 my_addr.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY,转换过来就是0.0.0.0,泛指本机的意思,也就是表示本机的IP bzero(&(my_addr.sin_zero),8); //给sockfd绑定端口 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1){ perror(“bind error!”); exit(1); } //sockfd设立未监听端口 if (listen(sockfd, BACKLOG) == -1) { perror(“listen error!”); exit(1); } printf(“wait for accept…\n”); while(1){ int sin_size = sizeof(struct sockaddr_in); //等待客户端连接 if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) { perror(“accept error”); continue; } /每次建立完整的socket连接之后,fork一个子进程来处理与客户端的会话,父进程继续等待其他客户端的连接/ if (!fork()) { printf(“accept a connection from : %s\n”, inet_ntoa(remote_addr.sin_addr)); sleep(10); //设置睡眠用来放大客户端同时访问的时间差 //接收客户端信息 if ((recvbytes = recv(client_fd, buf, MAXDATASIZE, 0)) ==-1) { perror(“receive error!”); exit(1); } buf[recvbytes] = ‘\0’; //执行服务器业务逻辑并反馈信息给客户端 if(strcmp(buf, user) == 0){ if (send(client_fd, “Hello,You are connected!”, 26, 0) == -1){ perror(“send error!”); exit(1); } }else{ if (send(client_fd, “Sorry,you are not the user!”, 26, 0) == -1){ perror(“send error!”); exit(1);

上一篇:Linux下对TF卡进行分区


下一篇:LinuxC应用开发学习笔记(十六)--Socket套接字