TCP併發伺服器例項--多執行緒
1.客戶端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int connect_fd;
int ret;
char snd_buf[1024];
int i;
int port;
int len;
static struct sockaddr_in srv_addr;
//客戶端執行需要給出具體的連線地址和埠
if(argc!=3)
{
printf("Usage: %s server_ip_address port\n",argv[0]);
return 1;
}
//獲得輸入的埠
port=atoi(argv[2]);
//建立套節字用於客戶端的連線
connect_fd=socket(PF_INET,SOCK_STREAM,0);
if(connect_fd<0)
{
perror("cannot create communication socket");
return 1;
}
//填充關於伺服器的套節字資訊
memset(&srv_addr,0,sizeof(srv_addr));
srv_addr.sin_family=AF_INET;
srv_addr.sin_addr.s_addr=inet_addr(argv[1]);
srv_addr.sin_port=htons(port);
//連線指定的伺服器
ret=connect(connect_fd,(struct sockaddr *)&srv_addr,sizeof(srv_addr));
if(ret==-1)
{
perror("cannot connect to the server");
close(connect_fd);
return 1;
}
memset(snd_buf,0,1024);
//使用者輸入資訊後,程式將輸入的資訊通過套接字傳送給伺服器
//然後呼叫read函式從伺服器中讀取傳送來的資訊
//當輸入“@”時,程式退出
while(1)
{
write(STDOUT_FILENO,"input message:",14);
len=read(STDIN_FILENO,snd_buf,1024);
if(len>0)
write(connect_fd,snd_buf,len);
len=read(connect_fd,snd_buf,len);
if(len>0)
printf("Message form server: %s\n",snd_buf);
if(snd_buf[0]=='@')
break;
}
close(connect_fd);
return 0;
}
2.伺服器端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <netdb.h>
#include <pthread.h>
//執行緒執行函式負責讀寫
void *thr_fn(void *arg)
{
int size,j;
char recv_buf[1024];
int *parg=(int *)arg;
int new_fd=*parg;
printf("new_fd=%d\n",new_fd);
while((size=read(new_fd,recv_buf,1024))>0)
{
if(recv_buf[0]=='@')
break;
printf("Message from client(%d): %s\n",size,recv_buf);
for(j=0;j<size;j++)
recv_buf[j]=toupper(recv_buf[j]);
write(new_fd,recv_buf,size);
}
close(new_fd);
return 0;
}
int main(int argc,char *argv[])
{
socklen_t clt_addr_len;
int listen_fd;
int com_fd;
int ret;
int i;
static char recv_buf[1024];
int len;
int port;
pthread_t tid;
struct sockaddr_in clt_addr;
struct sockaddr_in srv_addr;
//伺服器端執行時要給出埠資訊,該埠為監聽埠
if(argc!=2)
{
printf("Usage:%s port\n",argv[0]);
return 1;
}
//獲得輸入的埠
port=atoi(argv[1]);
//建立套接字用於伺服器的監聽
listen_fd=socket(PF_INET,SOCK_STREAM,0);
if(listen_fd<0)
{
perror("cannot create listening socket");
return 1;
}
//填充關於伺服器的套節字資訊
memset(&srv_addr,0,sizeof(srv_addr));
srv_addr.sin_family=AF_INET;
srv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
srv_addr.sin_port=htons(port);
//將伺服器和套節字繫結
ret=bind(listen_fd,(struct sockaddr *)&srv_addr,sizeof(srv_addr));
if(ret==-1)
{
perror("cannot bind server socket");
close(listen_fd);
return 1;
}
//監聽指定埠,連線5個客戶端
ret=listen(listen_fd,5);
if(ret==-1)
{
perror("cannot listen the client connect request");
close(listen_fd);
return 1;
}
//對每個連線來的客戶端建立一個執行緒,單獨與其進行通訊
//首先呼叫read函式讀取客戶端傳送來的資訊
//將其轉換成大寫後傳送回客戶端
//當輸入“@”時,程式退出
while(1)
{
len=sizeof(clt_addr);
com_fd=accept(listen_fd,(struct sockaddr *)&clt_addr,&len);
if(com_fd<0)
{
if(errno==EINTR)
{
continue;
}
else
{
perror("cannot accept client connect request");
close(listen_fd);
return 1;
}
}
printf("com_fd=%d\n",com_fd);//列印建立連線的客戶端產生的套節字
if((pthread_create(&tid,NULL,thr_fn,&com_fd))==-1)
{
perror("pthread_create error");
close(listen_fd);
close(com_fd);
return 1;
}
}
return 0;
}