网络编程小例

//server.c
 1#include <stdlib.h>
 2#include <stdio.h>
 3#include <errno.h>
 4#include <string.h>
 5#include <unistd.h>
 6#include <netdb.h>
 7#include <sys/socket.h>
 8#include <netinet/in.h>
 9#include <sys/types.h>
10#include <arpa/inet.h>
11
12int main(int argc, char *argv[])
13{
14        int sockfd,new_fd;
15        struct sockaddr_in server_addr;
16        struct sockaddr_in client_addr;
17        int sin_size,portnumber;
18        char hello[]="Hello! Are You Fine?\n";
19
20        if(argc!=2)
21        {
22                fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
23                exit(1);
24        }

25
26        if((portnumber=atoi(argv[1]))<0)
27        {
28                fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
29                exit(1);
30        }

31
32        /* 服务器端开始建立socket描述符 */
33        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1
34        {
35                fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
36                exit(1);
37        }

38
39        /* 服务器端填充 sockaddr结构  */
40        bzero(&server_addr,sizeof(struct sockaddr_in));
41        server_addr.sin_family=AF_INET;
42        server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
43        server_addr.sin_port=htons(portnumber);
44
45        /* 捆绑sockfd描述符  */
46        if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
47        {
48                fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
49                exit(1);
50        }

51
52        /* 监听sockfd描述符  */
53        if(listen(sockfd,5)==-1)
54        {
55                fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
56                exit(1);
57        }

58
59        while(1)
60        {
61                /* 服务器阻塞,直到客户程序建立连接  */
62                sin_size=sizeof(struct sockaddr_in);
63                if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
64                {
65                        fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
66                        exit(1);
67                }

68
69                fprintf(stderr,"Server get connection from %s\n",
70                inet_ntoa(client_addr.sin_addr));
71                if(write(new_fd,hello,strlen(hello))==-1)
72                {
73                        fprintf(stderr,"Write Error:%s\n",strerror(errno));
74                        exit(1);
75                }

76                /* 这个通讯已经结束     */
77                close(new_fd);
78                /* 循环下一个     */ 
79        }

80        close(sockfd);
81        exit(0);
82}



//client.c
 1#include <stdlib.h>
 2#include <stdio.h>
 3#include <errno.h>
 4#include <string.h>
 5#include <unistd.h>
 6#include <netdb.h>
 7#include <sys/socket.h>
 8#include <netinet/in.h>
 9#include <sys/types.h>
10#include <arpa/inet.h>
11
12int main(int argc, char *argv[])
13{
14        int sockfd;
15        char buffer[1024];
16        struct sockaddr_in server_addr;
17        struct hostent *host;
18        int portnumber,nbytes;
19
20        if(argc!=3)
21        {
22                fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
23                exit(1);
24        }

25
26        if((host=gethostbyname(argv[1]))==NULL)
27        {
28                fprintf(stderr,"Gethostname error\n");
29                exit(1);
30        }

31
32        if((portnumber=atoi(argv[2]))<0)
33        {
34                fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
35                exit(1);
36        }

37
38        /* 客户程序开始建立 sockfd描述符  */
39        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
40        {
41                fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
42                exit(1);
43        }

44
45        /* 客户程序填充服务端的资料       */
46        bzero(&server_addr,sizeof(server_addr));
47        server_addr.sin_family=AF_INET;
48        server_addr.sin_port=htons(portnumber);
49        server_addr.sin_addr=*((struct in_addr *)host->h_addr);
50
51        /* 客户程序发起连接请求         */
52        if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
53        {
54                fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
55                exit(1);
56        }

57
58        /* 连接成功了           */
59        if((nbytes=read(sockfd,buffer,1024))==-1)
60        {
61                fprintf(stderr,"Read Error:%s\n",strerror(errno));
62                exit(1);
63        }

64        buffer[nbytes]='\0';
65        printf("I have received:%s\n",buffer);
66        /* 结束通讯     */
67        close(sockfd);
68        exit(0);
69}
posted on 2010-05-12 10:01 zhongguoa 阅读(317) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理