实现目标:客户端发送一段数据,服务器回显,服务器显示客户端ip地址
服务器代码:
/***************************************************************
File Name: test.c
Author: kinght
Mail: [email protected]
Created Time: Wed 20 Jun 2018 06:58:02 PM PDT
**************************************************************/
#include
#include //connect,send,recv,setsockopt等
#include
#include // sockaddr_in, "man 7 ip" ,htons
#include //poll,pollfd
#include //inet_addr,inet_aton
#include //read,write
#include //gethostbyname
#include //perror
#include
#include //errno
#include // memset
#include
#include
#include
#ifdef HAVE_NETINET_IN_H
#include
#endif
#define ERR_EXIT(m)\
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int main(void)
{
int listenfd;
if((listenfd =socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
{
ERR_EXIT("ERROR");
}
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port = htons(5188);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int on = 1;
if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))< 0)
{
ERR_EXIT("SETSOCKOPT");
}
if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))< 0)
{
ERR_EXIT("error");
}
if(listen(listenfd,SOMAXCONN)<0)
{
ERR_EXIT("listen");
}
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof(peeraddr);
int conn;
conn = accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen);
if(conn < 0)
{
ERR_EXIT("accept");
}
printf("ip= %s,port = %d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
char recvbuf[1024];
while(true)
{
memset(recvbuf,0,sizeof(recvbuf));
int ret = read(conn,recvbuf,sizeof(recvbuf));
if(ret < 0)
continue;
write(conn,recvbuf,ret);
printf(recvbuf,stdout);
}
close(conn);
close(listenfd);
return 0;
}
客户端实现代码
/*************************************************************************
> File Name: client.c
> Author: kinght
> Mail: [email protected]
> Created Time: Wed 20 Jun 2018 06:58:02 PM PDT
************************************************************************/
#include
#include //connect,send,recv,setsockopt等
#include
#include // sockaddr_in, "man 7 ip" ,htons
#include //poll,pollfd
#include //inet_addr,inet_aton
#include //read,write
#include //gethostbyname
#include //perror
#include
#include //errno
#include // memset
#include
#include
#include
#ifdef HAVE_NETINET_IN_H
#include
#endif
#define ERR_EXIT(m)\
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int main(void)
{
int sock;
if((sock =socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
{
ERR_EXIT("ERROR");
}
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port = htons(5188);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))< 0)
{
ERR_EXIT("connect");
}
char sendbuf[1024]={0};
char recvbuf[1024]={0};
while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
{
write(sock,sendbuf,strlen(sendbuf));
read(sock,recvbuf,sizeof(recvbuf));
fputs(recvbuf,stdout);
memset(sendbuf,0,sizeof(sendbuf));
memset(recvbuf,0,sizeof(recvbuf));
}
close(sock);
return 0;
}