Socket TCP【簡單聊天例項】
阿新 • • 發佈:2018-12-24
Server端:
#include <iostream> #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> #include <pthread.h> #include <unistd.h> #include <arpa/inet.h> #define MYPORT 3490 //定義埠 #define BACKLOG 10 #define MAXDATASIZE 1024 int sockfd,new_fd; pthread_t accthread,recthread; void* recmessage(void*){ //接收客戶端資訊函式 while(1){ int numbytes; char buf[MAXDATASIZE]; if((numbytes = recv(new_fd,buf,MAXDATASIZE,0))==-1){ perror("recv"); exit(1); } buf[numbytes]='\0'; if(strcmp(buf,"exit")==0){ //若收到的是exit字元,則代表退出通訊 printf("Client is closed\n"); close(new_fd); close(sockfd); exit(1); } printf("client:%s\n",buf); }/*while*/ } void* acceptconnect(void*){ //接受客戶端連線請求函式 struct sockaddr_in their_addr; socklen_t sin_size = sizeof(struct sockaddr_in); // if (( accept_fd = accept(socket_fd, (struct sockaddr *) &remote_addr, &sin_size)) == -1 ) if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1){ perror("accept"); exit(1); } printf("server:got connection from %s\n",inet_ntoa(their_addr.sin_addr)); /*建立子執行緒,用於接收資訊*/ if((pthread_create(&recthread,NULL,recmessage,NULL)) != 0){ printf("create thread error!\r\n"); exit(1); } } int main(void){ struct sockaddr_in my_addr; /*建立套接字*/ if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){ perror("socket"); exit(1); } /*初始化sockaddr_in結構體相關引數*/ my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero),8); /*繫結埠與套接字*/ if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) == -1){ perror("bind"); exit(1); } /*監聽客戶端套接字*/ if(listen(sockfd,BACKLOG)== -1){ perror("listen"); exit(1); } printf("listening...\n"); /*建立子執行緒,用於接收資訊*/ if((pthread_create(&accthread,NULL,acceptconnect,NULL))!=0){ printf("create thread error!\n"); exit(1); } while(1){ char msg[MAXDATASIZE]; scanf("%s",msg); if(send(new_fd,msg,strlen(msg),0) == -1){ //傳送資訊,與客戶端交流 perror("send"); exit(1); } if(strcmp(msg,"exit") ==0){ printf("byebye\n"); close(new_fd); close(sockfd); exit(1); } }/*while*/ return 0; }/*main*/
Client端:
#include <iostream> #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> #include <pthread.h> #include <netdb.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 3490 #define BACKLOG 10 #define MAXDATASIZE 1024 #define SERVERIP "127.0.0.1" int sockfd; pthread_t recthread; /*接收資訊函式*/ void* recmessage(void*){ while(1){ int numbytes; char buf[MAXDATASIZE]; if((numbytes = recv(sockfd,buf,MAXDATASIZE,0))==-1){ perror("recv"); exit(1); } buf[numbytes]='\0'; if(strcmp(buf,"exit")==0){ printf("Server is closed\n"); close(sockfd); exit(1); } printf("Server:%s\n",buf); }/*while*/ } int main(){ struct hostent *he; /*獲取主機IP地址*/ struct sockaddr_in their_addr; /*獲取主機IP地址*/ if((he = gethostbyname(SERVERIP)) == NULL){ herror("gethostbyname"); exit(1); } /*建立套接字*/ if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){ perror("socket"); exit(1); } /*初始化sockaddr_in結構體*/ their_addr.sin_family = AF_INET; their_addr.sin_port = htons(PORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero),8); /*向伺服器傳送連線請求*/ if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1){ perror("connect"); exit(1); } /*建立子執行緒,用於接收資訊*/ if((pthread_create(&recthread,NULL,recmessage,NULL))!= 0){ printf("create thread error!\r\n"); exit(1); } /*傳送資訊。接收發送資訊用的是同一埠,都 是sockfd*/ while(1){ char msg[MAXDATASIZE]; scanf("%s",msg); if(send(sockfd,msg,strlen(msg),0) == -1){ perror("send"); exit(1); } if(strcmp(msg,"exit") ==0){ printf("byebye\n"); close(sockfd); exit(1); } }/*while*/ return 0; }