unix网络编程代码(1)

从本次博客开始,本人开始展示在学习unix网络编程过程中实现的程序。unix网络编程不会教读者做具体的网络编程项目,而是教读者理解网络编程。《计算机网络》这本书仅仅是在讲计算机网络的一些基本概念,《linux程序设计》这本书是在将一些linux系统调用,其中包含一部分socket接口,但是仅仅是将一些api的用法,并没有讲解每一个系统调用背后包含的《计算机网络》的理论知识。而《unix网络编程》则是将linux系统和调用和计算机网络的理论知识结合起来讲,读者读了十分清楚。《unix网络编程》在讲解一些代码示例的时候讨论了在网络编程中所需考虑的其他的问题,比如进程、信号、线程等等,在代码中给出了恰当的解决方案。读者在模型的基础上加以修改,就能写出一个基于网络编程demo。不得不说,《unix网络编程》是一本理论和实践相结合的好书。

 

第一个实例是一个获取时间的程序,使用tcp连接通讯。

服务端代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <sys/socket.h>
 4 #include <sys/types.h>
 5 #include <arpa/inet.h>
 6 #include <netinet/in.h>
 7 #include <strings.h>
 8 #include <string.h>
 9 #include <unistd.h>
10 #include <errno.h>
11 #include <time.h>
12 
13 #define MAXLINE 4095
14 
15 int main ()
16 {
17     int listenfd,connfd;
18     struct sockaddr_in servaddr,cliaddr;
19     char buf[4095];
20     time_t ticks;
21 
22     /*create a socket*/
23     if ((listenfd = socket (AF_INET,SOCK_STREAM,0)) < 0) {
24         perror ("socket");
25         exit (1);
26     }
27 
28     bzero (&servaddr,sizeof (servaddr));
29     bzero (&cliaddr,sizeof (cliaddr));
30 
31     /*set ip information*/
32     servaddr.sin_family = AF_INET;
33     servaddr.sin_port = htons (9347);
34     servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
35 
36     if ( (bind (listenfd,
37         (struct sockaddr *) &servaddr,
38         sizeof (servaddr))) < 0) {
39         perror ("bind");
40         exit (1);
41     }
42 
43     listen (listenfd,20);
44     while (1) {
45         int len = sizeof (cliaddr);
46         connfd = accept (listenfd,(struct sockaddr*)&cliaddr,&len);
47         if (connfd < 0) {
48             perror ("accept");
49             exit (1);
50         } 
51 
52         printf ("a client has connected\n");
53 
54         ticks = time (NULL);
55         snprintf (buf,sizeof (buf),"%.24s\r\n",ctime (&ticks));
56         write (connfd,buf,strlen (buf));
57         close (connfd);
58     } 
59 
60     return 0;
61 }

 

客户端代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <sys/socket.h>
 4 #include <sys/types.h>
 5 #include <arpa/inet.h>
 6 #include <netinet/in.h>
 7 #include <strings.h>
 8 #include <unistd.h>
 9 #include <errno.h>
10 
11 #define MAXLINE 4095
12 int main () 
13 {
14     int sockfd;
15     int n;
16     char recvline[MAXLINE + 1];
17     struct sockaddr_in serveraddr;
18 
19     /*create a socket*/
20     if ((sockfd = socket (AF_INET,SOCK_STREAM,0)) < 0) {
21         perror ("socket");
22         exit (1);
23     }
24 
25     bzero (&serveraddr,sizeof (serveraddr));
26     
27     /*set ip and port information*/
28     serveraddr.sin_family = AF_INET;
29     serveraddr.sin_port = htons (9347);
30     if (inet_pton (AF_INET,"115.28.75.192",&serveraddr.sin_addr) < 0) {
31         perror ("inet_pton");
32         exit (1);
33     }
34 
35     if (connect (sockfd,
36         (struct sockaddr*) &serveraddr,
37         sizeof (serveraddr)) < 0) {
38         
39         perror ("connect");
40         exit (1);
41     }
42 
43     while ((n = read (sockfd,recvline,MAXLINE)) > 0) {
44         recvline[n] = '\0';
45         printf ("%s\n",recvline);
46     }
47 
48     if (n < 0) {
49         printf ("read error\n");
50     }
51     
52     exit (0);
53 }

刚刚接触网络编程,代码中有很多不完善的地方,请多指教。

posted @ 2016-02-05 10:28  纪老猴子  阅读(593)  评论(0编辑  收藏  举报