一个简单的客户端与服务器通讯讲解_第1页
一个简单的客户端与服务器通讯讲解_第2页
一个简单的客户端与服务器通讯讲解_第3页
一个简单的客户端与服务器通讯讲解_第4页
一个简单的客户端与服务器通讯讲解_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Linux22007-09-19 21:15:32C/C+LinuxBS2. TCPLinux(socket).socket,linux.2.1 socketint socket(int domain, int type,int protocol)domain:(AF_UNIXAF_INET).AF_UNIXUnix,AF_INETInternet, ( man socket domain PF_*AF_*,glibcposixPFAF,).type:(SOCK_STREAM,SOCK_DGRAM) SOCK_STREAMTCP,. SOCK_DGRAM

2、UDP,.protocol:type,0 socket.,-1,error.2.2 bind, (listen(),) connect(), , bind() :int bind(int sockfd, struct sockaddr *my_addr, int addrlen)sockfd:socket.addrlen:sockaddr.my_addr:sockaddr. sockaddrstruct sockaddrunisgned short as_family;char sa_data14;,(struct sockaddr_in) .sockaddr_instruct sockadd

3、r_inunsigned short sin_family;unsigned short int sin_port;struct in_addr sin_addr;unsigned char sin_zero8;Internetsin_familyAF_INET,sin_addrINADDR_ANY,sin_port.sin_zero8. bindsocket.0,socket2.3 listen, , :,-listen(),-accept() ( ), listen int listen(int sockfd, int backlog);sockfd socket() backlog ?

4、(accept() ) 20,510,-1, errno, listen() bind() , :socket();bind();listen();/* accept() */2.4 accept, accept() ! : (listen() (connect() (accept() accept() !, (send() ( recv() !int accept(int sockfd, struct sockaddr *addr,int *addrlen)sockfd:listen.addr, addrlen,.bind,listenaccept,accept, .accept,. -12

5、.5 connectint connect(int sockfd, struct sockaddr * serv_addr,int addrlen)sockfd:socket.serv_addr:.sin_addaddrlen:serv_addrconnect.0,sockfd -1. telnet socket(), 23( telnet )0? ,connect()- connect() :#include #include int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);sockfd

6、socket() serv_addr IP structsockaddraddrlen sizeof(struct sockaddr)?:#include #include #include #define DEST_IP 0#define DEST_PORT 23main()int sockfd;struct sockaddr_in dest_addr; /* */sockfd = socket(AF_INET, SOCK_STREAM, 0); /* */dest_addr.sin_family = AF_INET; /* host byte order */dest

7、_addr.sin_port = htons(DEST_PORT); /* short, network byte order */dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);bzero(&(dest_addr.sin_zero),; /* zero the rest of the struct */* dont forget to error check the connect()! */connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr);.2.6:so

8、cket:int int, : ( octet, ) , (Network Byte Order), NBO ,( htons() ) (Host Byte Order) NBO, (TM)-struct sockaddr. :struct sockaddr unsigned short sa_family; /* , AF_xxx */char sa_data14; /*14*/;sa_family , AF_INET sa_data struct sockaddr,: struct sockaddr_in (in Internet)struct sockaddr_in short int

9、sin_family; /* */unsigned short int sin_port; /* */struct in_addr sin_addr; /* Internet */unsigned char sin_zero8; /* sockaddr*/; sin_zero (, structsockaddr ) bzero() memset() , sockaddr_insockaddr socket() struct sockaddr *, struct sockaddr_in, sin_family struct sockaddr sa_family AF_INET,sin_port

10、sin_addr (Network Byte Order)!:, struct in_addr sin_addr ? , : struct in_addr, (unions):/* Internet () */struct in_addr unsigned long s_addr;, ina struct sockaddr_in ,ina.sin_addr.s_addr 4 IP () struct in_addr ,4 IP (#define)struct hostentstruct hostent char *h_name;char *h_aliases;int h_addrtype;in

11、t h_length;char *h_addr_list;#define h_addr h_addr_list0struct hostent:h_name ? h_aliases ? -h_addrtype ?; AF_INETh_length ? h_addr_list ? -h_addr - h_addr_list2.7 ,!: short () long () unsigned short h (host), to, n (network), s short: h-to-n-s, htons() (Host to Network Short).,n,h,s, l ,stolh() (Sh

12、ort toLong Host) , ,:htons()-Host to Network Shorthtonl()-Host to Network Longntohs()-Network to Host Shortntohl()-Network to Host Long,: char ? , : 68000 , htonl() IP , ,! Unix !: ,: struct sockaddr_in , sin_addr sin_port ,sin_family ? : sin_addr sin_port IP UDP , sin_family (kernel) , sin_family ,

13、2.8 /* (server.c) */* Name:server.c* Used to study the network programming in Linux OS.* Showing how to use the functions,* like socket,bind,listen,accept and write.* This is the server program.* Author:zeickey* Date:2006/9/16* Copyright (c) 2006,All Rights Reserved!*/#include #include #include #inc

14、lude #include #include #include #include int main(int argc, char *argv)int sockfd,new_fd;struct sockaddr_in server_addr;struct sockaddr_in client_addr;int sin_size,portnumber;char hello=Hello! Are You Fine?n;if(argc!=2)fprintf(stderr,Usage:%s portnumberan,argv0);return 1;if( (portnumber = atoi(argv1

15、) 0 )fprintf(stderr,Usage:%s portnumberan,argv0);return 1;/* socket */if( (sockfd = socket(AF_INET,SOCK_STREAM, 0) = -1 )fprintf(stderr,Socket error:%sna,strerror(errno);return 1;/* sockaddr */bzero(&server_addr, sizeof(struct sockaddr_in);memset(&server_addr, 0, sizeof(struct sockaddr_in);server_ad

16、dr.sin_family = AF_INET;server_addr.sin_addr.s_addr = htonl(INADDR_ANY);server_addr.sin_port = htons(portnumber);/* sockfdlisten */if( bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)=-1 )fprintf(stderr,Bind error:%sna,strerror(errno);return 1;/* sockfd */if( -1 = listen(sockfd,

17、5) )fprintf(stderr,Listen error:%sna,strerror(errno);return 1;while(1)/* , */sin_size=sizeof(struct sockaddr_in);/if( (new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size) = -1)new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size);if( -1 = new_fd )fprintf(stderr,Acce

18、pt error:%sna,strerror(errno);return 1;fprintf(stderr,Server get connection from %sn, inet_ntoa(client_addr.sin_addr);if(write(new_fd,hello,strlen(hello)=-1)fprintf(stderr,Write Error:%sn,strerror(errno);return 1;/* */close(new_fd);/* */close(sockfd);return 0;/* client.c */* Name:client.c* Used to s

19、tudy the network programming in Linux OS.* Showing how to use the functions,* like socket,bind,listen,accept and write.* This is the client program.* Author:zeickey* Date:2006/9/16* Copyright (c) 2006,All Rights Reserved!*/#include #include #include #include #include #include #include int main(int a

20、rgc, char *argv)int sockfd;char buffer1024;struct sockaddr_in server_addr;/struct hostent *host;char *ip;int portnumber,nbytes;if(argc!=3)fprintf(stderr,Usage:%s ip portnumberan,argv0);return 1;/if(host=gethostbyname(argv1)=NULL)printf(agrv1 = %sn,argv1);if( strlen(ip=argv1) 7 )fprintf(stderr,Get Ip address errorn);return 1;if(portnumber=atoi(argv2)h_addr );/server_addr.sin_addr.s_addr = inet_addr(host-h_addr);server_addr.sin_addr.s_addr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论