简单的客户端,服务端通信
生活随笔
收集整理的這篇文章主要介紹了
简单的客户端,服务端通信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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> #define BUFLEN 10 int main(int argc, char **argv) {int sockfd;int port = 4567;struct sockaddr_in ser_addr;int len;char buf[BUFLEN];/*建立socket*/if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){printf("server socket creation error\n");return -1;}else printf("server socket success\n"); ser_addr.sin_port = htons(port);ser_addr.sin_family = AF_INET;if (inet_aton(argv[1], (struct in_addr *)&ser_addr.sin_addr.s_addr) == 0) {printf("server error\n");return -1;}/*開始連接服務器*/ if(connect(sockfd,(struct sockaddr*)&ser_addr,sizeof(struct sockaddr)) == -1){perror("connect");exit(errno);}elseprintf("conncet success!\n");while(1) {/******接收消息*******/bzero(buf,BUFLEN);len = recv(sockfd,buf,BUFLEN,0);if(len > 0)printf("服務器發來的消息是:%s,共有字節數是: %d\n",buf,len-1);elseprintf("接受消息失敗!\n");/******發送消息*******/ bzero(buf,BUFLEN);printf("請輸入發送給對方的消息:");/*fgets函數:從流中讀取BUFLEN-1個字符*/fgets(buf,BUFLEN,stdin);len = send(sockfd,buf,strlen(buf),0);if(len > 0)printf("消息發送成功,本次共發送的字節數是:%d\n",len); else{printf("聊天終止!\n");break; } }/*關閉連接*/close(sockfd);return 0; } View Codeserver.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> #define BUFLEN 10 int main(int argc,char **argv) { int ser_sockfd,cli_sockfd; int port = 4567; int len; // struct sockaddr_in ser_addr; struct sockaddr_in cli_addr; char buf[BUFLEN];/*建立socket*/ if ((ser_sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){printf("server socket creation error\n");return -1; }else printf("socket create success!\n");/*設置服務器ip*/ bzero(&ser_addr,sizeof(ser_addr)); ser_addr.sin_family = AF_INET;/*設置服務器端口*/ ser_addr.sin_port=htons(port); if (argv[1])ser_addr.sin_addr.s_addr = inet_addr(argv[1]);elseser_addr.sin_addr.s_addr = INADDR_ANY;/*把地址和端口幫定到套接字上*/ if (bind(ser_sockfd,(struct sockaddr*) &ser_addr,sizeof(struct sockaddr_in)) == -1){printf("server binding error\n");return -1; }elseprintf("bind success!\n");/*偵聽本地端口*/ if (listen(ser_sockfd,5) == -1){printf("server listening error\n");return -1; }elseprintf("the server is listening!\n");while(1){printf("*****************聊天開始***************\n"); if ((cli_sockfd = accept(ser_sockfd,(struct sockaddr*)&cli_addr,&len)) == -1){ printf("server acception error\n");return -1; }elseprintf("正在與您聊天的客戶端是:%s: %d\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));while(1){/******發送消息*******/bzero(buf,BUFLEN);/*fgets函數:從流中讀取BUFLEN-1個字符*/fgets(buf,BUFLEN,stdin);len = send(cli_sockfd,buf,strlen(buf),0);if(len > 0)printf("消息發送成功,本次共發送的字節數是:%d\n",len); else{printf("消息發送失敗!\n");break; }/******接收消息*******/bzero(buf,BUFLEN);len = recv(cli_sockfd,buf,BUFLEN,0);if(len > 0)printf("客戶端發來的信息是:%s,共有字節數是: %d\n",buf,len);else{printf("聊天終止!\n");break; }}/*關閉聊天的套接字*/close(cli_sockfd);}/*關閉服務器的套接字*/close(ser_sockfd);return 0; } View Code?
轉載于:https://www.cnblogs.com/qianboping/p/6400689.html
總結
以上是生活随笔為你收集整理的简单的客户端,服务端通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu安装wkhtmltopdf
- 下一篇: 常用安全工具汇总