echo-server-client

2021-06-13  本文已影响0人  Jesson3264
/* 
 * @file   client.c
 * @author: jesson3264@163.com
 *
 * @time 2021年6月13日
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
 
#define SERVERIP "192.168.8.103"
#define SERVERPORT 12345
#define MAXBUFFER 256
 
int main(int argc, char** argv)
{  
    int clientFd,ret;
    struct sockaddr_in serveraddr;
    char buf[MAXBUFFER];
    clientFd=socket(AF_INET,SOCK_STREAM,0);//创建socket
    if (clientFd < 0)
    {
        printf("socket error:%s\n",strerror(errno));
        exit(-1);
    }

    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(SERVERPORT);
    inet_pton(AF_INET, SERVERIP, &serveraddr.sin_addr);
    
    ret = connect(clientFd, (struct sockaddr *)&serveraddr,sizeof(serveraddr));//连接到服务器
    if (ret != 0)
    {
        close(clientFd);
        printf("connect error:%s\n",strerror(errno));
        exit(-1);
    }

    while(1)
    {
            bzero(buf,sizeof(buf));
            scanf("%s",buf);
            write(clientFd,buf,sizeof(buf));//写数据
            bzero(buf,sizeof(buf));
            read(clientFd,buf,sizeof(buf));//读数据
            printf("echo:%s\n",buf);
        }
    close(clientFd);
    return (EXIT_SUCCESS);
}

// server
/* 
 * @file:   Server.c
 * @author: jesson3264@163.com
 *
 * 
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVERIP "192.168.8.103"
#define SERVERPORT 12345
#define MAXBUFFER 256

int main(int argc, char **argv)
{
    char readBuf[MAXBUFFER] = {0};
    int serverFd, connfd, ret;
    socklen_t len;
    struct sockaddr_in serveraddr, clientaddr;
    
    char ip[40] = {0};
    serverFd = socket(AF_INET, SOCK_STREAM, 0); //创建socket
    if (serverFd < 0)
    {
        printf("socket error:%s\n", strerror(errno));
        exit(-1);
    }

    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(SERVERPORT);
    //serveraddr.sin_addr.s_addr = INADDR_ANY;

    inet_pton(AF_INET, SERVERIP, &serveraddr.sin_addr);
    //将c语言字节序转换为网络字节序
    ret = bind(serverFd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); //绑定IP和端口
    if (ret != 0)
    {
        close(serverFd);
        printf("bind error:%s\n", strerror(errno));
        exit(-1);
    }

    ret = listen(serverFd, 5); //监听
    if (ret != 0)
    {
        close(serverFd);
        printf("listen error:%s\n", strerror(errno));
        exit(-1);
    }

    len = sizeof(clientaddr);
    bzero(&clientaddr, sizeof(clientaddr));
    while (1)
    {
        connfd = accept(serverFd, (struct sockaddr *)&clientaddr, &len); //接受客户端的连接
        printf("%s 连接到服务器 \n", inet_ntop(AF_INET, &clientaddr.sin_addr, ip, sizeof(ip)));
        if (serverFd < 0)
        {
            printf("accept error : %s\n", strerror(errno));
            continue;
        }

        while ((ret = read(connfd, (void *)readBuf, MAXBUFFER))) //读客户端发送的数据
        {
            fprintf(stdout, "recv:%s\n", readBuf);
            write(connfd, readBuf, MAXBUFFER); //写回客户端;
            bzero(readBuf, MAXBUFFER);
        }

        if (ret == 0)
        {
            printf("客户端关闭连接\n");
        }
        else
        {
            printf("read error:%s\n", strerror(errno));
        }
        close(connfd);
    }
    close(serverFd);
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读