關於linux下的TCP 的socket server和client,我的理解
阿新 • • 發佈:2019-02-18
CSDN的部落格中有這樣一篇部落格點選開啟連結,是關於TCP的socket程式設計的,我想基於我的理解解釋一下,畢竟原文只給出了純粹的程式碼,
我們知道上圖是它們之間的工作模式,基本程式設計的規則也是按照上面來的
下面是伺服器端的程式碼,但插入的程式碼本應該是C的,但是CSDN中插入的程式碼選項卻沒有C的,比較讓人困擾
客戶端的程式碼如下#include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <errno.h> #include <strings.h> #include <ctype.h> char host_name[20]; int port = 8000; int main() { struct sockaddr_in sin,pin; //乙太網套接字的地址結構,是協議族AF_INET對應的結構 int sock_descriptor,temp_sock_descriptor,address_size; int i , len , on=1; char buf[16384]; sock_descriptor = socket(AF_INET,SOCK_STREAM,0);//通過socket(int domain,int type,int protocol)的函式原型建立AF_INIT協議族的流//型別的socket,當然type除了流socket的之外還有SOCKET_RAW,據說這種型別的socket甚至可以偽裝資料包,如果閱讀這篇文章的大神對其很瞭解的話,希望能夠私信//發給我關於它的資料或網址。 bzero(&sin,sizeof(sin));//將變數sin置0 sin.sin_family = AF_INET; //指定協議族 sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port);//地址結構的埠地址,port是主機位元組序,通過htons()進行位元組序轉換成網路位元組序 if(bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin)) == -1)//將sin的地址和socket檔案描述符繫結到一起,繫結是資料接收和發//送的前提 { perror("call to bind"); exit(1); } if(listen(sock_descriptor,100) == -1)//監聽用來初始化伺服器可連線佇列,因為一次只能處理一個連線請求,當收到多個請求,將會儲存在佇列中,///先到先得 { perror("call to listem"); exit(1); } printf("Accpting connections...\n"); while(1) { address_size = sizeof(pin); temp_sock_descriptor = accept(sock_descriptor,(struct sockaddr *)&pin,&address_size);//前面提到,當收到多個連線請求時//後面未處理請求會被排在佇列中,而accept()返回一個新的socket檔案描述符來表示來自客戶端的連線 if(temp_sock_descriptor == -1) { perror("call to accept"); exit(1); } if(recv(temp_sock_descriptor,buf,16384,0) == -1)//該描述符是接收端的套接字,buf是用來儲存接收到的資料,16384是buf的長度 { perror("call to recv"); exit(1); } inet_ntop(AF_INET,&pin.sin_addr,host_name,sizeof(host_name)); printf("received from client(%s):%s\n",host_name,buf); len = strlen(buf); for(i = 0 ; i < len ; i++) { buf[i] = toupper(buf[i]); } if(send(temp_sock_descriptor,buf,len+1,0) == -1) { perror("call to send"); exit(1); } close(temp_sock_descriptor); } }
總之,大致過程應該是這樣子的,伺服器端在經過建立socket,繫結和監聽之後,便一直等待客戶端建立socket之後所發出的連線請求,客戶端寫資料,伺服器端收到資料,或者伺服器端傳送資料,客戶端讀取資料。我對linux的網路程式設計還在皮毛階段,有大神知道的比我多的,還望不吝賜教。拜託了,atohayorosikune#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <errno.h> #include <strings.h> #include <ctype.h> char * host_name = "127.0.0.1"; int port = 8000; int main(int argc , char * argv[]) { char buf[8192]; //char message[256]; int socket_descriptor; struct sockaddr_in pin; char * str ="A default test string"; if(argc < 2) { printf("we will send a default test string.\n"); } else { str = argv[1]; if(argc == 3) { host_name = argv[2]; } } bzero(&pin,sizeof(pin)); pin.sin_family = AF_INET; inet_pton(AF_INET,host_name,&pin.sin_addr); pin.sin_port = htons(port); if((socket_descriptor = socket(AF_INET,SOCK_STREAM,0)) == -1)//與伺服器端相對應 { perror("error opening socket \n"); exit(1); } if(connect(socket_descriptor,(struct sockaddr * )&pin,sizeof(pin)) == -1)客戶端在建立起套接字之後,不再需要進行地址繫結,可通過connect函式直接連線伺服器 { perror("error connecting to socket \n"); exit(1); } printf("sending message %s to server ..\n",str); if( write(socket_descriptor,str,strlen(str)+1) == -1 )//將string寫入socket描述符中 { perror("error in send \n"); exit(1); } printf("..sent message ...wait for message..\n"); if( read(socket_descriptor,buf,8192) == -1 )//從socket描述符中將8192個字元讀到buf中 { perror("error in receiving response from server \n"); exit(1); } printf("\nResponse from server:\n\n%s\n",buf); close(socket_descriptor); return 1; }