花了点时间写了个线程版的回射服务器,下面记录代码和遇到的问题和解决的办法
-
/*************************************************************************
-
> 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;
-
}
-
/*************************************************************************
-
> 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>
-
-
#define ERR_EXIT(exp) do\
-
{\
-
perror(exp);\
-
exit(EXIT_FAILURE);\
-
} while (0)
-
-
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 sendbuf[1024] = { 0 };
-
char buff[1024] = { 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));
-
-
while(1)
-
{
-
if(fgets(sendbuf, sizeof(sendbuf), stdin) == NULL)
-
{
-
ERR_EXIT("fgets error");
-
}
-
-
if(write(localfd, sendbuf, strlen(sendbuf)) < 0)
-
{
-
ERR_EXIT("write error");
-
}
-
-
if(read(localfd, recvbuf, sizeof(recvbuf)) < 0)
-
{
-
ERR_EXIT("read error");
-
}
-
-
if(fputs(recvbuf, stdout) == EOF)
-
{
-
ERR_EXIT("fputs error");
-
}
-
}
-
-
return 0;
-
}
在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create'
主要是以下两种原因:
1、#include
请确认头文件是否添加
2、-lpthread 编译选项,即在编译时需添加额外的编译选项,如使用gcc编译echo_serv.c文件,命令正确应该如下:
gcc echo_serv.c -o echo_serv -lpthread
请注意后面的-lpthread,特别是-后边的'l',link = 'l'
阅读(1210) | 评论(0) | 转发(0) |