学了个用Linux线程代替select的回射服务器
-
/*************************************************************************
-
> File Name: echo_cli.c
-
> Author: gwjiaoj
-
> Mail: jiaojinawei@163.com
-
> Created Time: Fri 27 Nov 2015 02:15:38 GMT
-
************************************************************************/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <unistd.h>
-
#include <sys/socket.h>
-
#include <sys/types.h>
-
#include <netinet/in.h>
-
#include <string.h>
-
#include <strings.h>
-
#include <pthread.h>
-
-
#define ERR_EXIT(exp) do\
-
{\
-
perror(exp);\
-
exit(EXIT_FAILURE);\
-
} while (0)
-
-
struct para
-
{
-
int clifd;
-
FILE *fp;
-
};
-
-
void *str_cli(void *arg)
-
{
-
struct para *para1 = NULL;
-
int clifd = 0;
-
FILE *fp = NULL;
-
char sendbuf[1500] = { 0 };
-
-
para1 = (struct para*)arg;
-
-
clifd = para1->clifd;
-
fp = para1->fp;
-
-
while(1)
-
{
-
memset(sendbuf, 0 , sizeof(sendbuf));
-
-
if(fgets(sendbuf, sizeof(sendbuf), fp) == NULL)
-
{
-
ERR_EXIT("fgets error");
-
}
-
-
if(write(clifd, sendbuf, strlen(sendbuf)) < 0)
-
{
-
ERR_EXIT("write error");
-
}
-
}
-
-
}
-
int main(int argc, void **argv)
-
{
-
int localfd = 0;
-
struct sockaddr_in serveraddr = { 0 };
-
struct sockaddr_in localaddr = { 0 };
-
socklen_t addrlen = 0;
-
char recvbuf[1024] = { 0 };
-
char buff[1024] = { 0 };
-
struct para para2 = { 0 };
-
pthread_t tid = 0;
-
-
if(2 != argc)
-
{
-
printf("usage: a.out \n");
-
return 0;
-
}
-
-
if((localfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
-
{
-
ERR_EXIT("socket error");
-
}
-
-
memset(&serveraddr, 0, sizeof(serveraddr));
-
serveraddr.sin_family = AF_INET;
-
serveraddr.sin_port = htons(5188);
-
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
-
if(connect(localfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
-
{
-
ERR_EXIT("connect error");
-
}
-
memset(&localaddr, 0, sizeof(localaddr));
-
addrlen = sizeof(localaddr);
-
if(getsockname(localfd, (struct sockaddr *)&localaddr, &addrlen) < 0)
-
{
-
ERR_EXIT("getsockname error");
-
}
-
-
inet_ntop(AF_INET, &localaddr.sin_addr, buff, sizeof(buff));
-
-
printf("ip: %s, port %d\n", buff, ntohs(localaddr.sin_port));
-
-
para2.clifd = localfd;
-
para2.fp = stdin;
-
-
if(pthread_create(&tid, NULL, str_cli, (void *)¶2) != 0)
-
{
-
ERR_EXIT("read error");
-
}
-
-
while(1)
-
{
-
memset(recvbuf, 0, sizeof(recvbuf));
-
if(read(localfd, recvbuf, sizeof(recvbuf)) < 0)
-
{
-
ERR_EXIT("read error");
-
}
-
-
if(fputs(recvbuf, stdout) == EOF)
-
{
-
ERR_EXIT("fputs error");
-
}
-
}
-
-
return 0;
-
}
-
/*************************************************************************
-
> File Name: echo_serv.c
-
> Author: gwjiaoj
-
> Mail: jiaojinawei@163.com
-
> Created Time: Fri 27 Nov 2015 02:15:38 GMT
-
************************************************************************/
-
#include <pthread.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <unistd.h>
-
#include <sys/socket.h>
-
#include <sys/types.h>
-
#include <netinet/in.h>
-
#include <string.h>
-
#include <strings.h>
-
-
#define ERR_EXIT(exp) do\
-
{\
-
perror(exp);\
-
exit(EXIT_FAILURE);\
-
} while (0)
-
-
void * do_it(void *pconnfd)
-
{
-
int recvbuf[1500] = { 0 };
-
int connfd = (int)pconnfd;
-
pthread_detach(pthread_self());
-
printf("%d\n", connfd);
-
for( ; ; )
-
{
-
memset(recvbuf, 0, sizeof(recvbuf));
-
if(read(connfd, recvbuf, sizeof(recvbuf)) < 0)
-
{
-
ERR_EXIT("read");
-
}
-
-
printf("%s\n", recvbuf);
-
-
if(write(connfd, recvbuf, strlen(recvbuf)) < 0)
-
{
-
ERR_EXIT("write");
-
}
-
}
-
-
close(connfd);
-
-
return;
-
}
-
int main(int argc, void **argv)
-
{
-
pthread_t tid = 0;
-
int listenfd = 0;
-
int connfd = 0;
-
struct sockaddr_in serveraddr = { 0 };
-
struct sockaddr_in peeraddr = { 0 };
-
char buff[1024] = { 0 };
-
socklen_t socklen = 0;
-
-
if((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
-
{
-
ERR_EXIT("socket");
-
}
-
-
memset(&serveraddr, 0, sizeof(serveraddr));
-
serveraddr.sin_family = AF_INET;
-
serveraddr.sin_port = htons(5188);
-
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
-
if(bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
-
{
-
ERR_EXIT("bind");
-
}
-
-
if(listen(listenfd, SOMAXCONN) < 0)
-
{
-
ERR_EXIT("listen");
-
}
-
-
while(1)
-
{
-
memset(&peeraddr, 0, sizeof(peeraddr));
-
socklen = sizeof(peeraddr);
-
-
if((connfd = accept(listenfd, (struct sockaddr *)&peeraddr, &socklen)) < 0)
-
{
-
ERR_EXIT("accept");
-
}
-
-
inet_ntop(AF_INET, &peeraddr.sin_addr, buff, sizeof(buff));
-
printf("%d\n", connfd);
-
printf("ip: %s, port: %d\n", buff, peeraddr.sin_port);
-
if(0 != pthread_create(&tid, NULL, do_it, (void *)connfd))
-
{
-
ERR_EXIT("pthread");
-
}
-
}
-
-
return 0;
-
}
阅读(1314) | 评论(0) | 转发(0) |