天天看点

苏嵌实训-嵌入式linux C 第 10 天

项目名称 【苏嵌实训-嵌入式linux C 第10天】
今日进度以及任务 学习网络基础以及TCP连接
本日任务完成情况 已完成老师布置任务
–本日开发中出现的问题汇总 -无-
本日未解决问题
本日开发收获 了解了网络基础
-其他- -无-

作业:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define PORT 33333

int main()
{
    int servfd;
    int cliefd;

    int opt;
    int cl_len;

    char buff[1024];
    
    struct sockaddr_in servaddr;
    struct sockaddr_in clieaddr;

    servfd = socket(AF_INET, SOCK_STREAM, 0);
    if(servfd == -1)
    {
        perror("socket error:");
        exit(1);
    }

    printf("socket success!\n");

    bzero(&servaddr, sizeof(struct sockaddr_in));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    opt = 1;
    setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    if(bind(servfd, (struct sockaddr *)(&servaddr), sizeof(struct sockaddr_in)) < 0)
    {
        perror("bind error:");
        exit(1);
    }

    printf("bind success!\n");

    if(listen(servfd, 3) < 0)
    {
        perror("listen error:");
        exit(1);
    }

    printf("listen success!\n");

    while(1){

        memset(buff, 0, sizeof(buff));

        bzero(&clieaddr, sizeof(struct sockaddr_in));
        cl_len = sizeof(struct sockaddr_in);

        printf("accepting ........ \n");

        cliefd = accept(servfd, (struct sockaddr *)(&clieaddr), &cl_len);
        if(cliefd == -1)
        {
            perror("accept error:");
            exit(1);
        }

        printf("ip: %s  port: %d\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port));

        read(cliefd, buff, sizeof(buff));

        printf("recv = %s\n", buff);

        usleep(2);

        write(cliefd, buff, strlen(buff));
    }

    return 0;
}


           
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define PORT 33333

int main()
{
    int cliefd;

    char buff[1024];

    struct sockaddr_in servaddr;

    cliefd = socket(AF_INET, SOCK_STREAM, 0);
    if(cliefd == -1)
    {
        perror("socket error:");
        exit(1);
    }

    printf("socket success!\n");

    bzero(&servaddr, sizeof(struct sockaddr_in));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if(connect(cliefd, (struct sockaddr *)(&servaddr), sizeof(struct sockaddr_in)) < 0)
    {
        perror("connection error:");
        exit(1);
    }

    printf("connect success!\n");

    write(cliefd, "hello world!", 13);

    memset(buff, 0, sizeof(buff));

    read(cliefd, buff, sizeof(buff));

    printf("recv = %s\n", buff);

    return 0;
}

           

运行结果:

[[email protected] note]$ ./server 
socket success!
bind success!
listen success!
accepting ........ 
ip: 127.0.0.1  port: 46084
recv = hello world!
accepting ........ 
ip: 127.0.0.1  port: 46086
recv = hello world!
accepting ........ 
ip: 127.0.0.1  port: 46088
recv = hello world!
accepting ........ 
ip: 127.0.0.1  port: 46090
recv = hello world!
accepting ........ 


           
[[email protected] note]$ ./client 
socket success!
connect success!
recv = hello world!
[[email protected] note]$ ./client 
socket success!
connect success!
recv = hello world!
[[email protected] note]$ ./client 
socket success!
connect success!
recv = hello world!
[[email protected] note]$ ./client 
socket success!
connect success!
recv = hello world!