gethostbyname函数的使用例子 下面这个例子使用gethostbuname获得主机相关信息(ip,name等等) #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <netdb.h> #include <errno.h> #include <netinet/in.h> #include <stdlib.h> int main(int argc,char **argv) { struct hostent *h; if(argc!=2) { fprintf(stderr,"Usage of name!/n"); exit(1); } h=gethostbyname(argv[1]); printf("Host name:%s/n",h->h_name); printf("IP Address:%s/n",inet_ntoa(*((struct in_addr *)h->h_addr))); return 0; } 具有连接的通信(TCP) TCP是面向连接的通信,服务端和客户端之间需要建立连接 服务端通过监听客户端了解连接情况,保证具有顺序,无差错的通信 注意: 1.不是客户端一次发送多少数量的数据,服务器端一次就接受多少数据, TCP协议给高层的数据数量,是TCP协议的事情,但是整体来说,保证到 服务器端的数据是完整按照顺序到达的 2.服务器程序中 if((newfd=accept(sockfd,(struct sockaddr *)&their_addr,&sock_len))==-1) fatal("accept"); accept函数返回的是一个新的socket描述符 以后的通信都是针对这个新的socket描述符操作,而不是以前那个sockfd 3.多线程编程中函数pthread_create的第三个参数的原型是void* fun(void *) Server端程序 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h>#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <netinet/in.h> #include <sys/types.h> #include <arpa/inet.h> #include <sys/socket.h> #include <pthread.h> #define PORT 5000 #define BUFLEN 100 int sockfd; int sock_len; int read_num; int newfd; char readbuf[BUFLEN]; char writebuf[BUFLEN]; void fatal(char *mes) { perror(mes); exit(1); } void* send_message() { while(1) { gets(writebuf); if(write(newfd,writebuf,BUFLEN)==-1) fatal("write"); } } int main() { pthread_t pit; struct sockaddr_in my_addr; struct sockaddr_in their_addr; if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) fatal("socket"); my_addr.sin_family=AF_INET; my_addr.sin_port=htons(PORT); my_addr.sin_addr.s_addr=INADDR_ANY; bzero(&(my_addr.sin_zero),0); if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1) fatal("bind"); if(listen(sockfd,5)==-1) fatal("listen"); sock_len=sizeof(struct sockaddr); if((newfd=accept(sockfd,(struct sockaddr *)&their_addr,&sock_len))==-1) fatal("accept"); if(pthread_create(&pit,NULL,send_message,NULL)==-1) fatal("pthread_createa"); while(1) { if((read_num=read(newfd,readbuf,BUFLEN))==-1) fatal("read"); readbuf[read_num]='/n'; readbuf[read_num+1]='/0'; printf("%s send message:%s/n",inet_ntoa(their_addr.sin_addr), readbuf); } close(sockfd); pthread_join(pit,NULL); exit(0); } Client端程序 #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <pthread.h> #define PORT 5000 #define BUFLEN 100 int sockfd; int read_num; char readbuf[BUFLEN]; char writebuf[BUFLEN]; void fatal(char *mes) { perror(mes); exit(1); } void* send_message() { while(1) { gets(writebuf); if(write(sockfd,writebuf,BUFLEN)==-1) fatal("write"); } } int main() { struct sockaddr_in my_addr; struct sockaddr_in their_addr; pthread_t pit; if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) fatal("socket"); my_addr.sin_family=AF_INET; my_addr.sin_port=htons(PORT); my_addr.sin_addr.s_addr=INADDR_ANY; bzero(&(my_addr.sin_zero),0); their_addr.sin_family=AF_INET; their_addr.sin_port=htons(PORT); their_addr.sin_addr.s_addr=inet_addr("172.18.137.156"); bzero(&(their_addr),0); if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1) fatal("bind"); if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1) fatal("connect"); if(pthread_create(&pit,NULL,send_message,NULL)==-1) fatal("pthrea_create"); while(1) { if((read_num=read(sockfd,readbuf,BUFLEN))==-1) fatal("read"); readbuf[read_num]='/n'; readbuf[read_num+1]='/0'; printf("server send message:%s/n",readbuf); } pthread_join(pit,NULL); close(sockfd); exit(0); } 无连接的通信(UDP) 无连接的通信使用的是运输层的协议:UDP 程序采用C/S模型,但Server端并不需要监听Client端的连接请求 只需要在默认设置的端口读取数据即可,发送和读取的时报文,既是 客户端发送什么数据,服务器端就完整的读取这段数据 注意: 1。UDP读取数据于TCP的差别 2.留意recvfrom函数,其中第六个个参数必须是&socklen 而且事先必须经过socklen=sizeof(struct sockaddr);处理 Server程序 3.their_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); 这句必须主义 linux中的sockaddr_in于windows中SOCKADDR_IN的区别 4.printf("get packet from %s./n",inet_ntoa(their_addr.sin_addr)); #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #define PORT 5000 #define MAXBUFLEN 100 int main() { int sockfd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int addr_len; int numberget; char recvbuf[MAXBUFLEN]; char sendbuf[MAXBUFLEN]; //get a socket desciptor if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) { perror("socket"); exit(1); } my_addr.sin_family=AF_INET; my_addr.sin_port=htons(PORT); my_addr.sin_addr.s_addr=INADDR_ANY; bzero(&(my_addr.sin_zero),8); their_addr.sin_family=AF_INET; their_addr.sin_port=htons(PORT); their_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); bzero(&(their_addr.sin_zero),8); if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1) { perror("bind"); exit(1); } while(1) { scanf("%s",sendbuf); if(sendto(sockfd,sendbuf,100,0, (struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1) { perror("sendto"); exit(1); } else { printf("send success!/n"); } if((numberget=recvfrom(sockfd,recvbuf,100,0, (struct sockaddr *)&their_addr,&addr_len))==-1) { perror("recvfrom"); exit(1); } printf("get packet from %s/n",inet_ntoa(their_addr.sin_addr.s_addr)); printf("packet is %d datas long./n",numberget); recvbuf[numberget]='/0'; printf("buf data is %s/n",recvbuf); } close(sockfd); exit(0); } Client端程序 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #define MYPORT 5000 #define MAXBUFLEN 100 int main() { int sockfd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int addr_len; int numberget; char recvbuf[MAXBUFLEN]; char sendbuf[MAXBUFLEN]; //get a socket descriptor if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) { perror("socket"); exit(1); } //set host info my_addr.sin_family=AF_INET; my_addr.sin_port=htons(MYPORT); my_addr.sin_addr.s_addr=INADDR_ANY; //set the rest of the struct sockaddr_in zero bzero(&(my_addr.sin_zero),8); //bind if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1) { perror("bind"); exit(1); } //take care of this sentence be carefull! addr_len=sizeof(struct sockaddr); while(1) { if((numberget=recvfrom(sockfd,recvbuf,100,0, (struct sockaddr *)&their_addr,&addr_len))==-1) { perror("recvfrom"); exit(1); } //output recv buf and the address of the talker printf("get packet from %s./n",inet_ntoa(their_addr.sin_addr)); printf("packet is %d datas long./n",numberget); recvbuf[numberget]='/0'; printf("buf data is %s/n",recvbuf); // scanf("%s",sendbuf); scanf("%s",sendbuf); if(sendto(sockfd,sendbuf,sizeof(sendbuf),0, (struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1) { perror("sendto"); exit(1); } } close(sockfd); exit(0); }