|
程序没什么东西,比较简单,就是想把看的一些东西都用上。客户端程序还要修改,连接超时没设置。 服务器: 1/**//** 2* sock编程测试,多线程,server端 3**/ 4#include <stdio.h> 5#include <stdlib.h> 6#include <unistd.h> 7#include <string.h> 8#include <sys/socket.h> 9#include <arpa/inet.h> 10#include <netinet/in.h> 11#include <pthread.h> 12 13#define MAX_LENGTH 1024 // 缓冲区最大长度 14#define MAX_THREAD 2 // 定义允许的最大线程数 15#define MAX_LISTEN 20 // 定义最大允许监听连接数 16 17pthread_mutex_t p_lock; 18char buf[MAX_LENGTH]; 19char data[MAX_LENGTH]; 20int thread_count = 0; //当前线程数量 21/**//** 22* 线程处理函数 23* 向来源信息发送数据 24**/ 25void *deal(void *arg) 26{ 27 int c_fd = *((int *)arg); 28 char buf_s[MAX_LENGTH]; 29 snprintf(buf_s,MAX_LENGTH,"thread id is : %u\n",pthread_self()); 30 pthread_mutex_lock(&p_lock); 31 if(write(c_fd,buf_s,strlen(buf_s)) < 0) 32 { 33 printf("write sock(%u) error,thread id is :",c_fd,pthread_self()); 34 } 35 sleep(5); 36 thread_count--; 37 pthread_mutex_unlock(&p_lock); 38} 39 40int main(int argc,char* argv[]) 41{ 42 int port; // 监听的端口 43 int sock_fd; //sock描述符 44 int i,*connect_fd; 45 pthread_t thread_id; 46 struct sockaddr_in server; 47 struct sockaddr_in client; 48 socklen_t len; 49 /**//** 50 * 参数个数不正确 51 **/ 52 if( argc != 2 ) 53 { 54 printf("\nUsage:%s [port]\n",argv[0]); 55 exit(1); 56 } 57 port = atoi(argv[1]); 58 59 if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0) 60 { 61 printf("create sock error.\n"); 62 exit(1); 63 } 64 // 开始填充server数据0 65 memset(&server,0,sizeof(server)); 66 server.sin_family = AF_INET; 67 server.sin_addr.s_addr = htonl(INADDR_ANY); 68 server.sin_port = htons(port); 69 if(bind(sock_fd,(struct sockaddr *)&server,sizeof(server)) < 0) 70 { 71 printf("bind sock error.\n"); 72 exit(1); 73 } 74 if(listen(sock_fd,MAX_LISTEN) < 0) 75 { 76 printf("listen sock error.\n"); 77 exit(1); 78 } 79 printf("\n starting listen\n"); 80 while(1) 81 { 82 len = sizeof(client); 83 connect_fd = new int; 84 if((*connect_fd = accept(sock_fd,(struct sockaddr *)&client,&len)) < 0) 85 { 86 printf("accept sock error.\n"); 87 exit(1); 88 } 89 printf("\n connect from %s, port %d.\n",inet_ntop(AF_INET,&client.sin_addr,buf,MAX_LENGTH),ntohs(client.sin_port)); 90 // 创建线程去处理这个连接 91 pthread_mutex_lock(&p_lock); 92 // 检测线程数量,如果达到最大线程数量的话,3秒内如果无法继续创建新线程,则放弃当前连接 93 for(i=0;i<3;i++) 94 { 95 printf("thread_count : %d\n",thread_count); 96 if(thread_count < MAX_THREAD) 97 break; 98 else 99 { 100 printf("第 %d 次检测数量已满\n",i+1); 101 sleep(1); 102 } 103 } 104 if(thread_count >= MAX_THREAD) 105 { 106 printf("give up connect, ID %d\n",*connect_fd); 107 pthread_mutex_unlock(&p_lock); 108 continue; 109 } 110 if(pthread_create(&thread_id,NULL,deal,connect_fd) != 0) 111 { 112 printf("thread create error.\n"); 113 exit(1); 114 } 115 printf("create thread %u\n",thread_id); 116 thread_count++; 117 pthread_mutex_unlock(&p_lock); 118 } 119} 120 客户端: 1/**//** 2* sock编程测试,多线程,client端 3**/ 4#include <stdio.h> 5#include <stdlib.h> 6#include <string.h> 7#include <unistd.h> 8#include <errno.h> 9#include <sys/socket.h> 10#include <arpa/inet.h> 11#include <netinet/in.h> 12 13#define MAX_LENGTH 1024 14char buf[MAX_LENGTH]; 15 16int main(int argc,char* argv[]) 17{ 18 // 检查参数 19 int sock_fd,port,n; 20 struct sockaddr_in server; 21 if(argc != 3) 22 { 23 printf("Usage:%s [ip] [port]\n",argv[0]); 24 exit(1); 25 } 26 port = atoi(argv[2]); 27 if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) < 0) 28 { 29 printf("sock error.\n"); 30 exit(1); 31 } 32 bzero(&server,sizeof(server)); 33 server.sin_family = AF_INET; 34 server.sin_port = htons(port); 35 if(inet_pton(AF_INET,argv[1],&server.sin_addr) <= 0) 36 { 37 printf("set address error.\n"); 38 exit(1); 39 } 40 if(connect(sock_fd,(struct sockaddr *)&server,sizeof(server)) < 0) 41 { 42 printf("connect error:%s\n",strerror(errno)); 43 exit(1); 44 } 45 if((n = read(sock_fd,buf,MAX_LENGTH)) > 0) 46 { 47 printf("receive data : %s\n",buf); 48 } 49 else 50 { 51 printf("read data error\n"); 52 exit(1); 53 } 54 close(sock_fd); 55} 56
|