Tcp多線程伺服器和用戶端程式
伺服器程式:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];void* fun(void* x){ //printf("enter thread!\r\n"); int new_fd=*((int*)x); while(1) { int z=read(new_fd,buf,BUFSIZE);//第 6 步 讀取通訊端 if(z==0){printf("client close !");break;}; buf[z]='\0'; printf("%s\r\n",buf);//列印 };}int newfd[512];int inewfd=0;int main(){ //第 1 步 建立通訊端 int sockfd=socket(AF_INET,SOCK_STREAM,0); //第 2 步 設定地址結構體 struct sockaddr_in svraddr; svraddr.sin_family=AF_INET;//使用 internet 協議 svraddr.sin_port=htons(PORT); inet_aton("0.0.0.0",&svraddr.sin_addr); //第 3 步 綁定 int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr)); if(ret<0){printf("error bind!\r\n");exit(-1);}; //第 4 步 監聽 listen(sockfd,128); while(1) { newfd[inewfd++]=accept(sockfd,NULL,NULL); //第 5 步 接收 pthread_t ntid; pthread_create(&ntid,NULL,fun,(void*)&(newfd[inewfd-1])); }}
注意:
gcc server.c -o server -lpthread
用戶端程式 cli.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];int main(){ //第 1 步 建立一個體通訊端 int sockfd=socket(AF_INET,SOCK_STREAM,0); //第 2 步 設定 addr 結構體 struct sockaddr_in svraddr; svraddr.sin_family=AF_INET;//使用 internet 協議 svraddr.sin_port=htons(PORT); inet_aton("127.0.0.1",&svraddr.sin_addr); //第 3 步 串連伺服器 connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr)); while(1) { scanf("%s",buf); write(sockfd,buf,strlen(buf)); //第 4 步 向通訊端中寫入字串 }}
Udp的伺服器程式和用戶端程式
伺服器程式:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];int main(){ //第 1 步 建立通訊端 int sockfd=socket(AF_INET,SOCK_DGRAM,0); //第 2 步 設定地址結構體 struct sockaddr_in svraddr; svraddr.sin_family=AF_INET;//使用 internet 協議 svraddr.sin_port=htons(PORT); inet_aton("0.0.0.0",&svraddr.sin_addr); //第 3 步 綁定 int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr)); if(ret<0){printf("cannot bind!\r\n");exit(-1);}; while(1) { struct sockaddr_in cli; int len=sizeof(cli); int z=recvfrom(sockfd,buf,BUFSIZE,0,(struct sockaddr*)&cli,&len);//第 6 步 讀取通訊端 buf[z]='\0'; printf("%s\r\n",buf);//列印 }}
用戶端程式 cli.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>#define PORT 8082#define BUFSIZE 512char buf[BUFSIZE+1];int main(){ //第 1 步 建立一個體通訊端 int sockfd=socket(AF_INET,SOCK_DGRAM,0); //第 2 步 設定 addr 結構體 struct sockaddr_in svraddr; svraddr.sin_family=AF_INET;//使用 internet 協議 svraddr.sin_port=htons(PORT); inet_aton("127.0.0.1",&svraddr.sin_addr); //第 3 步 串連伺服器 //connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr)); while(1) { scanf("%s",buf); sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&svraddr,sizeof(svraddr)); //第 4 步 向通訊端中寫入字串 }}