linux下多執行緒tcp伺服器 客戶端程式

2021-06-12 01:55:57 字數 4517 閱讀 9731

/* 編譯命令:gcc -wall -g -o server server.c -lpthread */

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define port 8458

#define max_listen 20

typedef struct msgheadmh;

pthread_mutex_t mutex;

pthread_attr_t attr;

char *get_time_string(char *sbuf)

char *getfilename(char *filename)

int tcp_recv(int fd,int len, char *strrecv)

else if (iret == 0)

irecvlen = recv(fd,p,len,0);

if (irecvlen > 0) }

return count; 

} void *recv_msg(void *clfd)

,sfilename[64] = ;

int fd = *((int *)clfd); 

file *fp = null; 

printf("clfd=%d\n",fd); 

imhl = sizeof(mh);

/* 開啟乙個檔案用於存放sql 語句 */

pthread_mutex_lock(&mutex);

getfilename(sfilename);

pthread_mutex_unlock(&mutex);

printf("filename:%s\n",sfilename);

if ( 0 > strlen(sfilename))

fp = fopen(sfilename,"w+");

if (!fp)

fprintf(fp,"%s\n","set termout on");

fprintf(fp,"%s\n","set feed off");

while(1)

ibodylen = 0;

msghead = (mh *)recvbuf;

itype = (int)msghead->type;

//printf("type :%d\n",itype);

if (2 == itype)

ibodylen += msghead->bl[0] * 256 * 256 * 256;

ibodylen += msghead->bl[1] * 256 * 256;

ibodylen += msghead->bl[2] * 256;

ibodylen += msghead->bl[3];

//printf("bodylen=%d\n",ibodylen);

memset(recvbuf,0x00,sizeof(recvbuf));

iret = tcp_recv(fd,ibodylen,recvbuf);

if (iret == ibodylen)

else

fp = fopen(sfilename,"w+");

fprintf(fp,"%s\n","set termout on");

fprintf(fp,"%s\n","set feed off");

fprintf(fp,"%s\n",recvbuf);

ireccount++;}}

} return 0;

}void sig_handler(int signo)

}int setup_sig_handlers(void)

int main()

memset(&(seraddr),0x00,sizeof(struct sockaddr_in));

memset(&(cliaddr),0x00,sizeof(struct sockaddr_in));

seraddr.sin_family = af_inet;

seraddr.sin_port = htons(port);

seraddr.sin_addr.s_addr = htonl(inaddr_any);

if ( 0  > bind(serfd,(struct sockaddr *)&seraddr,isize))

if ( 0 > setsockopt(serfd,sol_socket,so_rcvbuf,&bufsize,sizeof(bufsize)))

if (0 > listen(serfd,max_listen))

while(1)

if (count < 256)

else

}pthread_mutex_destroy(&mutex);

pthread_attr_destroy(&attr);

return 0;

}/* 客戶端 */

/* 編譯命令  gcc -wall -g -o client client.c */

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

typedef struct msgheadmh;

int con_serv()

;int sndbufsize = 4 * 1024 * 1024;

tcpfd = socket(af_inet,sock_stream,0);

if(tcpfd < 0)

if ( 0 > setsockopt(tcpfd, sol_socket, so_sndbuf, (char *)&sndbufsize, sizeof(sndbufsize)))

memset(&(seraddr),0x00,sizeof(seraddr));

memset(sip,0x00,sizeof(sip));

strcpy(sip,"127.0.0.1");

seraddr.sin_family = af_inet;

seraddr.sin_port = htons(iport);

seraddr.sin_addr.s_addr = inet_addr(sip);

for (icount = 0; icount < 3; icount++)

else } 

return -1; 

} void tcp_send(int tcpfd,int num)

;int i = 0,iret = -1;

char buf[128] = ,*p = null;

char msg[1024] = ;

int ilen = 128;

mh msghead,*mshe = null;

for (i = 0; i < num; i++)

else if ( 0 == iret)

else

else }

} }printf("tcpfd:%d already send %d record,",tcpfd,num);

printf("send diconnect request\n");

mshe = (mh *)msg;

memset(msg,0x00,sizeof(msg));

mshe->bl[0] = (unsigned char)0;

mshe->bl[1] = (unsigned char)0;

mshe->bl[2] = (unsigned char)0;

mshe->bl[3] = (unsigned char)0;

mshe->type = (unsigned char)2;

ilen = sizeof(mh);

iret = send(tcpfd,msg,ilen,0);

if ( iret != ilen)

return;

}void *execute(void *num)

else

//sleep(10);

close(tcpfd);

printf("\nclient exit!\n\n");

return null;

}int  main(int argc,char **argv)

else

}num = atoi(argv[1]);

if (0 >= num || 65536 < num)

}printf("send %d record\n",num);

sendrec = num;

for (i = 0; i < 5; i++)

for (i = 0; i < 5; i++)

return 0;  

}

QT多執行緒伺服器Tcp

最近寫乙個專案用到了伺服器開啟多執行緒,閱部落格無數,奈何沒有理解精髓,致晚上夢見自己寫出來了。次日,奮筆疾書,埋頭苦學,最後終於寫出來了。現在總結一下。伺服器多執行緒需要用到 qtcpserver qtcpsocket qthread qtcpserver 最主要的乙個incomingconnec...

執行緒TCP伺服器

include include include include include include include include define portnumber 3333 void read msg void arg void write msg void arg int main int arg...

linux 多執行緒聊天伺服器

自己寫的linux多執行緒聊天伺服器,可以實現併發,通過鍊錶新增人數。include include include include include include include include include include include typedef struct client typed...