| Navigation:First_Community->Software->Linux development | Goto:New Topic•Setting•Search | |
CopyRight owned by the original author.--(www.MegaEntry.com)
2.1 socket int socket(int domain, int type,int protocol) domain:说明我们网络程序所在的主机采用的通讯协族(AF_UNIX和AF_INET等). AF_UNIX只能够用于单一的Unix系统进程间通信,而AF_INET是针对Internet的,因而可以允许在远程 主机之间通信(当我们 man socket时发现 domain可选项是 PF_*而不是AF_*,因为glibc是posix的实现 所以用PF代替了AF,不过我们都可以使用的).CopyRight owned by the original author.--(www.MegaEntry.com)
type:我们网络程序所采用的通讯协议(SOCK_STREAM,SOCK_DGRAM等) SOCK_STREAM表明我们用的是TCP协议,这样会提供按顺序的,可靠,双向,面向连接的比特流. SOCK_DGRAM 表明我们用的是UDP协议,这样只会提供定长的,不可靠,无连接的通信. protocol:由于我们指定了type,所以这个地方我们一般只要用0来代替就可以了 socket为网络通讯做基本的准备.成功时返回文件描述符,失败时返回-1,看errno可知道出错的详细情况. 2.2 bindMegaEntry - Social networking and discussion site!
int bind(int sockfd, struct sockaddr *my_addr, int addrlen) sockfd:是由socket调用返回的文件描述符. addrlen:是sockaddr结构的长度.CopyRight owned by the original author.--(www.MegaEntry.com)
my_addr:是一个指向sockaddr的指针. 在MegaEntry - Social networking and discussion site!
不过由于系统的兼容性,我们一般不用这个头文件,而使用另外一个结构(struct sockaddr_in) 来代替.在MegaEntry - Social networking and discussion site!
unsigned char sin_zero[8]; 我们主要使用Internet所以sin_family一般为AF_INET,sin_addr设置为INADDR_ANY表示可以 和任何的主机通信,sin_port是我们要监听的端口号.sin_zero[8]是用来填充的. bind将本地的端口同socket返回的文件描述符捆绑在一起.成功是返回0,失败的情况和socket一样 2.3 listen int listen(int sockfd,int backlog)MegaEntry - Social networking and discussion site!
sockfd:是bind后的文件描述符. backlog:设置请求排队的最大长度.当有多个客户端程序和服务端相连时, 使用这个表示可以介绍的排队长度. listen函数将bind的文件描述符变为监听套接字.返回的情况和bind一样.MegaEntry - Social networking and discussion site!
2.4 accept int accept(int sockfd, struct sockaddr *addr,int *addrlen) sockfd:是listen后的文件描述符. addr,addrlen是用来给客户端的程序填写的,服务器端只要传递指针就可以了. bind,listen和accept是服务器端用的函数,accept调用时,服务器端的程序会一直阻塞到有一个 客户程序发出了连接. accept成功时返回最后的服务器端的文件描述符,这个时候服务器端可以向该描述符写信息了. 失败时返回-1CopyRight owned by the original author.--(www.MegaEntry.com)
2.5 connect int connect(int sockfd, struct sockaddr * serv_addr,int addrlen) sockfd:socket返回的文件描述符.CopyRight owned by the original author.--(www.MegaEntry.com)
serv_addr:储存了服务器端的连接信息.其中sin_add是服务端的地址 addrlen:serv_addr的长度 connect函数是客户端用来同服务端连接的.成功时返回0,sockfd是同服务端通讯的文件描述符 失败时返回-1.MegaEntry - Social networking and discussion site!
2.6 实例 服务器端程序CopyRight owned by the original author.--(www.MegaEntry.com)
/******* 服务器程序 (server.c) ************/ #includeMegaEntry - Social networking and discussion site!
#includeCopyRight owned by the original author.--(www.MegaEntry.com)
{ int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello[]= "Hello! Are You Fine? ";MegaEntry - Social networking and discussion site!
if(argc!=2) { fprintf(stderr, "Usage:%s portnumbera ",argv[0]); exit(1); }CopyRight owned by the original author.--(www.MegaEntry.com)
if((portnumber=atoi(argv[1]))<0) { fprintf(stderr, "Usage:%s portnumbera ",argv[0]); exit(1); }CopyRight owned by the original author.--(www.MegaEntry.com)
/* 服务器端开始建立socket描述符 */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr, "Socket error:%s a ",strerror(errno)); exit(1);CopyRight owned by the original author.--(www.MegaEntry.com)
} /* 服务器端填充 sockaddr结构 */ bzero(&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY);CopyRight owned by the original author.--(www.MegaEntry.com)
server_addr.sin_port=htons(portnumber); /* 捆绑sockfd描述符 */ if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr, "Bind error:%s a ",strerror(errno));MegaEntry - Social networking and discussion site!
exit(1); } /* 监听sockfd描述符 */ if(listen(sockfd,5)==-1) {CopyRight owned by the original author.--(www.MegaEntry.com)
fprintf(stderr, "Listen error:%s a ",strerror(errno)); exit(1); } while(1) {CopyRight owned by the original author.--(www.MegaEntry.com)
/* 服务器阻塞,直到客户程序建立连接 */ sin_size=sizeof(struct sockaddr_in); if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1) { fprintf(stderr, "Accept error:%s a ",strerror(errno)); exit(1);MegaEntry - Social networking and discussion site!
} fprintf(stderr, "Server get connection from %s ", inet_ntoa(client_addr.sin_addr)); if(write(new_fd,hello,strlen(hello))==-1) {MegaEntry - Social networking and discussion site!
fprintf(stderr, "Write Error:%s ",strerror(errno)); exit(1); } /* 这个通讯已经结束 */ close(new_fd); /* 循环下一个 */CopyRight owned by the original author.--(www.MegaEntry.com)
} close(sockfd); exit(0); }MegaEntry - Social networking and discussion site!
客户端程序 /******* 客户端程序 client.c ************/ #includeCopyRight owned by the original author.--(www.MegaEntry.com)
#includeCopyRight owned by the original author.--(www.MegaEntry.com)
int main(int argc, char *argv[]) { int sockfd; char buffer[1024]; struct sockaddr_in server_addr; struct hostent *host;CopyRight owned by the original author.--(www.MegaEntry.com)
int portnumber,nbytes; if(argc!=3) { fprintf(stderr, "Usage:%s hostname portnumbera ",argv[0]); exit(1);CopyRight owned by the original author.--(www.MegaEntry.com)
} if((host=gethostbyname(argv[1]))==NULL) { fprintf(stderr, "Gethostname error "); exit(1);MegaEntry - Social networking and discussion site!
} if((portnumber=atoi(argv[2]))<0) { fprintf(stderr, "Usage:%s hostname portnumbera ",argv[0]); exit(1);CopyRight owned by the original author.--(www.MegaEntry.com)
} /* 客户程序开始建立 sockfd描述符 */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr, "Socket Error:%sa ",strerror(errno));CopyRight owned by the original author.--(www.MegaEntry.com)
exit(1); } /* 客户程序填充服务端的资料 */ bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET;CopyRight owned by the original author.--(www.MegaEntry.com)
server_addr.sin_port=htons(portnumber); server_addr.sin_addr=*((struct in_addr *)host->h_addr); /* 客户程序发起连接请求 */ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) {MegaEntry - Social networking and discussion site!
fprintf(stderr, "Connect Error:%sa ",strerror(errno)); exit(1); } /* 连接成功了 */ if((nbytes=read(sockfd,buffer,1024))==-1)CopyRight owned by the original author.--(www.MegaEntry.com)
{ fprintf(stderr, "Read Error:%s ",strerror(errno)); exit(1); } buffer[nbytes]= ' '; printf( "I have received:%s ",buffer);CopyRight owned by the original author.--(www.MegaEntry.com)
/* 结束通讯 */ close(sockfd); exit(0); }MegaEntry - Social networking and discussion site!
MakeFile 这里我们使用GNU 的make实用程序来编译. 关于make的详细说明见 Make 使用介绍 ######### Makefile ########### all:server client server:server.cMegaEntry - Social networking and discussion site!
gcc $^ -o $@ client:client.c gcc $^ -o $@ 运行make后会产生两个程序server(服务器端)和client(客户端) 先运行./server portnumber& (portnumber随便取一个大于1204且不在/etc/services中出现的号码 就用8888好了),然后运行 ./client localhost 8888 看看有什么结果. (你也可以用telnet和netstat试一试.) 上面是一个最简单的网络程序,不过是不是也有点烦.上面有许多函数我们还没有解释. 我会在下一章进行的详细的说明.MegaEntry - Social networking and discussion site!
2.7 总结 总的来说网络程序是由两个部分组成的--客户端和服务器端.它们的建立步骤一般是: 服务器端 socket-->bind-->listen-->acceptCopyRight owned by the original author.--(www.MegaEntry.com)
客户端 socket-->connect