socket例項C語言:一個簡單的聊天程式
阿新 • • 發佈:2018-12-22
我們老師讓寫一個簡單的聊天軟體,並且實現不同機子之間的通訊,我用的是SOCKET程式設計。不廢話多說了,先附上程式碼:
伺服器端server.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <unistd.h> #include <sys/time.h> #define BUFLEN 1024 #define PORT 6666 #define LISTNUM 20 int main() { int sockfd, newfd; struct sockaddr_in s_addr, c_addr; char buf[BUFLEN]; socklen_t len; unsigned int port, listnum; fd_set rfds; struct timeval tv; int retval,maxfd; /*建立socket*/ if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1){ perror("socket"); exit(errno); }else printf("socket create success!\n"); memset(&s_addr,0,sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(PORT); s_addr.sin_addr.s_addr = htons(INADDR_ANY); /*把地址和埠幫定到套接字上*/ if((bind(sockfd, (struct sockaddr*) &s_addr,sizeof(struct sockaddr))) == -1){ perror("bind"); exit(errno); }else printf("bind success!\n"); /*偵聽本地埠*/ if(listen(sockfd,listnum) == -1){ perror("listen"); exit(errno); }else printf("the server is listening!\n"); while(1){ printf("*****************聊天開始***************\n"); len = sizeof(struct sockaddr); if((newfd = accept(sockfd,(struct sockaddr*) &c_addr, &len)) == -1){ perror("accept"); exit(errno); }else printf("正在與您聊天的客戶端是:%s: %d\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port)); while(1){ FD_ZERO(&rfds); FD_SET(0, &rfds); maxfd = 0; FD_SET(newfd, &rfds); /*找出檔案描述符集合中最大的檔案描述符*/ if(maxfd < newfd) maxfd = newfd; /*設定超時時間*/ tv.tv_sec = 6; tv.tv_usec = 0; /*等待聊天*/ retval = select(maxfd+1, &rfds, NULL, NULL, &tv); if(retval == -1){ printf("select出錯,與該客戶端連線的程式將退出\n"); break; }else if(retval == 0){ printf("waiting...\n"); continue; }else{ /*使用者輸入資訊了*/ if(FD_ISSET(0, &rfds)){ /******傳送訊息*******/ memset(buf,0,sizeof(buf)); /*fgets函式:從流中讀取BUFLEN-1個字元*/ fgets(buf,BUFLEN,stdin); /*打印發送的訊息*/ //fputs(buf,stdout); if(!strncasecmp(buf,"quit",4)){ printf("server 請求終止聊天!\n"); break; } len = send(newfd,buf,strlen(buf),0); if(len > 0) printf("\t訊息傳送成功:%s\n",buf); else{ printf("訊息傳送失敗!\n"); break; } } /*客戶端發來了訊息*/ if(FD_ISSET(newfd, &rfds)){ /******接收訊息*******/ memset(buf,0,sizeof(buf)); /*fgets函式:從流中讀取BUFLEN-1個字元*/ len = recv(newfd,buf,BUFLEN,0); if(len > 0) printf("客戶端發來的資訊是:%s\n",buf); else{ if(len < 0 ) printf("接受訊息失敗!\n"); else printf("客戶端退出了,聊天終止!\n"); break; } } } } /*關閉聊天的套接字*/ close(newfd); /*是否退出伺服器*/ printf("伺服器是否退出程式:y->是;n->否? "); bzero(buf, BUFLEN); fgets(buf,BUFLEN, stdin); if(!strncasecmp(buf,"y",1)){ printf("server 退出!\n"); break; } } /*關閉伺服器的套接字*/ close(sockfd); return 0; }
客戶端client.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <unistd.h> #include <sys/time.h> #define BUFLEN 1024 #define PORT 6666 int main(int argc, char **argv) { int sockfd; struct sockaddr_in s_addr; socklen_t len; unsigned int port; char buf[BUFLEN]; fd_set rfds; struct timeval tv; int retval, maxfd; /*建立socket*/ if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){ perror("socket"); exit(errno); }else printf("socket create success!\n"); /*設定伺服器ip*/ memset(&s_addr,0,sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons(PORT); if (inet_aton(argv[1], (struct in_addr *)&s_addr.sin_addr.s_addr) == 0) { perror(argv[1]); exit(errno); } /*開始連線伺服器*/ if(connect(sockfd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr)) == -1){ perror("connect"); exit(errno); }else printf("conncet success!\n"); while(1){ FD_ZERO(&rfds); FD_SET(0, &rfds); maxfd = 0; FD_SET(sockfd, &rfds); if(maxfd < sockfd) maxfd = sockfd; tv.tv_sec = 6; tv.tv_usec = 0; retval = select(maxfd+1, &rfds, NULL, NULL, &tv); if(retval == -1){ printf("select出錯,客戶端程式退出\n"); break; }else if(retval == 0){ printf("waiting...\n"); continue; }else{ /*伺服器發來了訊息*/ if(FD_ISSET(sockfd,&rfds)){ /******接收訊息*******/ bzero(buf,BUFLEN); len = recv(sockfd,buf,BUFLEN,0); if(len > 0) printf("伺服器發來的訊息是:%s\n",buf); else{ if(len < 0 ) printf("接受訊息失敗!\n"); else printf("伺服器退出了,聊天終止!\n"); break; } } /*使用者輸入資訊了,開始處理資訊併發送*/ if(FD_ISSET(0, &rfds)){ /******傳送訊息*******/ bzero(buf,BUFLEN); fgets(buf,BUFLEN,stdin); if(!strncasecmp(buf,"quit",4)){ printf("client 請求終止聊天!\n"); break; } len = send(sockfd,buf,strlen(buf),0); if(len > 0) printf("\t訊息傳送成功:%s\n",buf); else{ printf("訊息傳送失敗!\n"); break; } } } } /*關閉連線*/ close(sockfd); return 0; }
在程式的服務端輸入
[email protected]:~/Desktop/1234$ ./server
在程式的客戶端輸入
[email protected]:~/Desktop/1234$ ./client 192.168.100.80
當然這都是我在我的機子上截的圖,客戶端我們可以放到任何一臺機子上,只要保證都能在同一個區域網下,連到我的IP地址就行。開啟服務端後,在開啟客戶端,我們就可以聊天了:
server端:
waiting... waiting... waiting... nihal 訊息傳送成功:nihal 客戶端發來的資訊是:zhuzhu hehe 訊息傳送成功:hehe 客戶端發來的資訊是:haha 客戶端退出了,聊天終止! 伺服器是否退出程式:y->是;n->否? y server端退出。
client端:
waiting... waiting... waiting... 伺服器發來的訊息是:nihal zhuzhu 訊息傳送成功:zhuzhu 伺服器發來的訊息是:hehe haha 訊息傳送成功:haha