花了一天的时间调试了这个回射服务器,下面贴出源码。
-
/*************************************************************************
-
> File Name: echo_serv.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 listenfd = 0;
-
int connfd = 0;
-
int optport = 0;
-
int optaddr = 0;
-
struct sockaddr_in serveraddr = { 0 };
-
struct sockaddr_in peeraddr = { 0 };
-
char recvbuf[1024] = { 0 };
-
char buff[1024] = { 0 };
-
socklen_t socklen = 0;
-
socklen_t optlen = 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);
-
-
optaddr = 1;
-
optlen = sizeof(optaddr);
-
if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&optaddr, optlen) < 0)
-
{
-
ERR_EXIT("setsockopt SO_REUSEADDR");
-
}
-
-
if(bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
-
{
-
ERR_EXIT("bind");
-
}
-
-
if(listen(listenfd, SOMAXCONN) < 0)
-
{
-
ERR_EXIT("listen");
-
}
-
-
/*
-
optport = 1;
-
optlen = sizeof(optport);
-
if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEPORT, (char *)&optport, optlen) < 0)
-
{
-
ERR_EXIT("setsockopt SO_REUSEPORT");
-
}*/
-
-
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("ip: %s, port: %d\n", buff, peeraddr.sin_port);
-
-
while(1)
-
{
-
if(read(connfd, recvbuf, sizeof(recvbuf)) < 0)
-
{
-
ERR_EXIT("read");
-
}
-
-
printf("%s\n", recvbuf);
-
-
if(write(connfd, recvbuf, strlen(recvbuf)) < 0)
-
{
-
ERR_EXIT("write");
-
}
-
}
-
-
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;
-
}
阅读(1267) | 评论(0) | 转发(0) |