Linux之网络编程:时间服务器

时间:2022-02-14 17:18:57

基于TCP-服务器

1,创建一个socket套接字

int socket(int domain,int type,int protocol)

domain:IP地址族,AF_INET(IPv4)、AF_INET6(IPv6)、AF_LOCAL/AF_UNIX(本地)

type:套接字,流式套接字(SOCK_STRAM),数字报套接字(SOCK_DGRAM),原始套接字(SOCK_RAM)

protocol:置0

2,邦定socket套接字和服务器相关资料

int bind(int sockfd,const struct sockaddr *addr,socklen_t addrlen)

sockfd:套接字描述符

addr:指向服务器sockaddr的指针

addrlen:参数addr的长度

成功为0,失败非0

3,设置允许客户端的最大连接数

int listen(int sockfd,int backlog)

backlog:连接数量

4、用来等待客户端的连接请求

int accept(int sockfd,struct sockaddr *addr,socklen_t *addrlen)

addr:指向客户端的sockaddr的指针

addrlen:参数addr的长度

例如:

服务器端time_server.c

 1 /*************************************************************************
2 > File Name: sock_server.c
3 > Author: xu
4 > Mail: eeexu123@163.com
5 > Created Time: 2016年10月13日 星期四 16时13分24秒
6 ************************************************************************/
7
8 #include<stdio.h>
9 #include<sys/types.h>
10 #include<sys/socket.h>
11 #include<string.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include<unistd.h>
15 #include<time.h>
16 #include<stdlib.h>
17 #define MAX_LISTEN_QUE 5
18 #define BUFFER_SIZE 128
19
20 int main(int argc,char *argv[])
21 {
22 int sockfd,listenfd;
23 int bytes,opt=1;
24 time_t timp;
25 char buf[BUFFER_SIZE];
26 struct sockaddr_in server,client;
27 /*客户端程序开始建立sockfd描述符*/
28 listenfd = socket(AF_INET,SOCK_STREAM,0);
29 if(listenfd < 0)
30 {
31 perror("socket");
32 exit(1);
33 }
34 /*使用套接字选项,避免出错*/
35 if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)) != 0)
36 {
37 perror("setsockopt");
38 exit(1);
39 }
40 /*服务器填充sockaddr结构*/
41 bzero(&server,sizeof(server)); //对结构体清零
42 server.sin_family = AF_INET; //IPv4地址族
43 server.sin_addr.s_addr = ntohl(INADDR_ANY); //服务器IP址址
44 server.sin_port = ntohs(8888); //端口号
45
46 /*邦定sockfd描述符到IP地址*/
47 int len = sizeof(struct sockaddr);
48 if(bind(listenfd,(struct sockaddr *)&server,len) != 0)
49 {
50 perror("bind");
51 exit(1);
52 }
53
54 /*设置允许连接的最大客户数*/
55 if(listen(listenfd,MAX_LISTEN_QUE) != 0)
56 {
57 perror("listen");
58 exit(1);
59 }
60 while(1)
61 {
62 /*服务器阻塞,直到客户程序建立连接*/
63 sockfd = accept(listenfd,(struct sockaddr *)&client,&len);
64 if(sockfd < 0)
65 {
66 perror("accept");
67 exit(1);
68 }
69 //提取时间
70 timp = time(NULL);
71 snprintf(buf,BUFFER_SIZE,"%s",ctime(&timp));
72 //向客户端发送时间
73 bytes = write(sockfd,buf,strlen(buf));
74 if(bytes < 0)
75 {
76 perror("write");
77 exit(1);
78 }
79
80 printf("buf is %s and buf size %d\n",buf,bytes);
81 printf("IP:0X%x,PORT:%d\n",htonl(client.sin_addr.s_addr),htons(client.sin_port));
82 //结束本次连接
83 close(sockfd);
84 }
85 close(listenfd);
86 return 0;
87 }

 

客户端time_client.c

 1 /*************************************************************************
2 > File Name: sock_client.c
3 > Author: xu
4 > Mail: eeexu123@163.com
5 > Created Time: 2016年10月13日 星期四 16时13分37秒
6 ************************************************************************/
7
8 #include<stdio.h>
9 #include<sys/types.h>
10 #include<sys/socket.h>
11 #include<string.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14 #include<unistd.h>
15 #include<netdb.h>
16 #include<time.h>
17 #include<stdlib.h>
18 #define MAX_LISTEN_QUE 5
19 #define BUFFER_SIZE 128
20
21 int main(int argc,char *argv[])
22 {
23 int sockfd,bytes;
24 int buf[BUFFER_SIZE];
25 struct sockaddr_in server;
26
27
28 /*客户端程序开始建立sockfd描述符*/
29 sockfd = socket(AF_INET,SOCK_STREAM,0);
30 if(sockfd < 0)
31 {
32 perror("socket");
33 exit(1);
34 }
35 /*客户端程序填充服务端资料*/
36 bzero(&server,sizeof(server)); //初始化,置0
37 server.sin_family = AF_INET; //IPv4
38 server.sin_addr.s_addr = inet_addr("127.0.0.1"); //IP址址
39 server.sin_port = htons(8888);
40 /*客户端程序发起连接请求*/
41 if(connect(sockfd,(struct sockaddr *)&server,sizeof(server)) < 0)
42 {
43 perror("connetc");
44 exit(1);
45 }
46 /*读取服务端时间*/
47 bytes = read(sockfd,buf,BUFFER_SIZE);
48 if(bytes < 0)
49 {
50 perror("read");
51 exit(1);
52 }
53 if(0 == bytes)
54 {
55 printf("server close\n");
56 exit(1);
57 }
58 printf("read bytes is :%d\n",bytes);
59 printf("Time is :%s\n",buf);
60
61 close(sockfd);
62 return 0;
63 }