標籤:style blog io ar color os 使用 sp 檔案
#include <stdio.h> //標準輸入輸出標頭檔#include <sys/socket.h> //與通訊端相關的函式宣告和結構定義#include <netinet/in.h> //某些結構體聲明、宏定義#include <arpa/inet.h> //某些函式宣告#include <errno.h> //查看錯誤碼/*以上為包含必要的標頭檔,其中幾個標頭檔是幾乎每一個網路程式所必需的,如<sys/socket.h>,<netinet/in.h>等*/int main(int argc,char *argv[]){ printf("=======================================\n"); printf("= My first server program =\n"); printf("=======================================\n"); int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello[]="Hello, The Network! This is my first Server.\n"; if (argc!=2){ fprintf(stderr,"Usage:%s portnumber \a\n",argv[0]); exit(1); //程式出現錯誤結束 }if ((portnumber=atoi(argv[1]))<0){ fprintf(stderr,"Usage:%s portnumber \a\n",argv[0]); exit(1); } //atoi()將字元型轉化為整型 //建立socket描述符(intnet協議簇,流通訊端,使用預設協議) if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){ fprintf(stderr,"Socket error:%s \a\n",strerror(errno)); exit(1); }//填充伺服器的通訊端地址結構sockaddr bzero(&server_addr,sizeof (struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(portnumber); //開始捆綁sockfd描述符 if (bind(sockfd,(struct sockaddr *)(&server_addr),sizeof (struct sockaddr))==-1){ fprintf(stderr,"Bind error:%s \a\n",strerror(errno)); exit(1); } //監聽sockfd描述符if (listen(sockfd,5)==-1){ //fprintf(stderr,"Listen error:%s \a\n",strerror(errno)); exit(1); } while(1) {//阻塞伺服器,直到客戶程式建立串連 sin_size=sizeof (struct sockaddr_in); if ((new_fd=accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size))==-1){ //fprintf(stderr,"Accept error:%s \a\n",strerror(errno)); exit(1); } fprintf(stderr,"Server get connection from %s :\n",inet_ntoa(client_addr.sin_addr)); if (write(new_fd,hello,strlen(hello))==-1){ //fprintf(stderr,"Write error:%s \a\n",strerror(errno)); exit(1); } //通訊結束,繼續迴圈執行 close(new_fd);/*當伺服器處理完這個客戶機請求後,關閉串連通訊端*/ }//關閉監聽通訊端描述符 close(sockfd); exit(0); }
#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <errno.h>#include <netdb.h> /*包括結構hostent(主機環境),獲得主機資訊的幾個函數*/int main(int argc,char *argv[]){ printf("=======================================\n"); printf("= My first client program! =\n"); printf("=======================================\n"); int sockfd,new_fd; struct sockaddr_in server_addr; struct hostent *host; int nbytes,portnumber; char buffer[1024]; if (argc!=3){ fprintf(stderr,"Usage:%s hostname portnumber \a\n",argv[0]); exit(1); }//獲得伺服器位址 if ((host=gethostbyname(argv[1]))==NULL){ fprintf(stderr,"HostName Erro!"); exit(1); } if ((portnumber=atoi(argv[2]))<0){ fprintf(stderr,"Usage:%s hostname portnumber \a\n",argv[0]); exit(1); } //建立用戶端Sockfd描述符 if ((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){ fprintf(stderr,"Socket error:%s \a\n",strerror(errno)); exit(1); }//調用函數connect之前,需要指定伺服器處理序的通訊端地址//填充sockaddr結構 bzero(&server_addr,sizeof (struct sockaddr_in)); server_addr.sin_family=AF_INET; 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){ fprintf(stderr,"Bind error:%s \a\n",strerror(errno)); exit(1); } //進行讀寫資料操作,將資料顯示到標準輸出上 if ((nbytes=read(sockfd,buffer,1024))==-1){ fprintf(stderr,"Read error: %s \n",strerror(errno)); exit(1); } buffer[nbytes]=‘\\‘; printf("收到: %s \n",buffer);//結束通訊//關閉串連 close(sockfd); exit(0); }
Linux伺服器、用戶端