linux下简单的服务器与客户端实现代码

实现目标:客户端发送一段数据,服务器回显,服务器显示客户端ip地址

服务器代码:

/***************************************************************

File Name: test.c
Author: kinght
Mail: [email protected]
Created Time: Wed 20 Jun 2018 06:58:02 PM PDT
**************************************************************/

#include<stdio.h>
#include <sys/socket.h>   //connect,send,recv,setsockopt等
#include <sys/types.h>      

#include <netinet/in.h>     // sockaddr_in, "man 7 ip" ,htons
#include <poll.h>             //poll,pollfd
#include <arpa/inet.h>   //inet_addr,inet_aton
#include <unistd.h>        //read,write
#include <netdb.h>         //gethostbyname

#include <error.h>         //perror
#include <stdio.h>
#include <errno.h>         //errno

#include <string.h>          // memset
#include <string>
#include <iostream>
#include<stdlib.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#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<stdio.h>
#include <sys/socket.h>   //connect,send,recv,setsockopt等
#include <sys/types.h>      

#include <netinet/in.h>     // sockaddr_in, "man 7 ip" ,htons
#include <poll.h>             //poll,pollfd
#include <arpa/inet.h>   //inet_addr,inet_aton
#include <unistd.h>        //read,write
#include <netdb.h>         //gethostbyname

#include <error.h>         //perror
#include <stdio.h>
#include <errno.h>         //errno

#include <string.h>          // memset
#include <string>
#include <iostream>
#include<stdlib.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#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;
}

实现结果:
这里写图片描述

文章来源: linux下简单的服务器与客户端实现代码

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:linux下简单的服务器与客户端实现代码