当前位置:   article > 正文

网络通讯-Socket-TCP-demo(C语言版)_local.sin_port = htons(port);

local.sin_port = htons(port);

流程图

在这里插入图片描述

TCP server 服务端

步骤如图所示
1 创建Socket 指定使用什么协议

 socket_fd = socket(AF_INET,SOCK_STREAM,0);
  • 1

2 将Socket与地址和端口绑定

//set socket address
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(PORT);
local_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(local_addr.sin_zero),8);


//bind socket
ret = bind(socket_fd,(struct  sockaddr*)&local_addr,sizeof(struct sockaddr_in));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3 侦听端口(目前Socket和地址 端口绑定了 但是上层应用必须绑定对应的Socket)

ret = listen(socket_fd,backlog);
  • 1

4 创建新的socket – 每次连接过来以后创建一个新的socket来收发数据 也就是accept socket (之前的socket 一直在侦听 请求连接是数据)

int addr_len = sizeof( struct sockaddr_in );
    //accept an new connection
    accept_fd = accept( socket_fd, (struct sockaddr *)&remote_addr, &addr_len );
  • 1
  • 2
  • 3

5 recv接收数据

 ret = recv( accept_fd ,(void*)in_buf ,MESSAGE_SIZE ,0 );
  • 1

6 send发送数据

send(accept_fd, (void*)in_buf, MESSAGE_SIZE, 0);
  • 1

7 close关闭当前socket

 close(accept_fd);
 close(socket_fd);
  • 1
  • 2

服务端完整代码

//
// Created by zyc on 2022/5/5.
//


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MESSAGE_SIZE 1024
#define PORT 8111
int main(){
    int ret = 0;

    int socket_fd = -1;
    int accept_fd = -1;

    int curpos = 0;
    int backlog = 10;
    int flag = 1;

    char in_buf[MESSAGE_SIZE] = {0,};

    struct sockaddr_in local_addr, remote_addr;

    //create a tcp socket
    socket_fd = socket(AF_INET,SOCK_STREAM,0);

    if (socket_fd==-1){
        perror("create socket error");
        exit(1);
    }

    //set option of socket
    ret = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,
                     &flag,sizeof(flag));
    if ( ret == -1 ){
        perror("setsockopt error");
    }

    //set socket address
    local_addr.sin_family = AF_INET;
    local_addr.sin_port = htons(PORT);
    local_addr.sin_addr.s_addr = INADDR_ANY;
    bzero(&(local_addr.sin_zero),8);


    //bind socket
    ret = bind(socket_fd,(struct  sockaddr*)&local_addr,sizeof(struct sockaddr_in));
    if(ret == -1 ) {
        perror("bind error");
        exit(1);
    }

    ret = listen(socket_fd,backlog);
    if ( ret == -1 ){
        perror("listen error");
        exit(1);
    }

    //loop
    for(;;){
        int addr_len = sizeof( struct sockaddr_in );
        //accept an new connection
        accept_fd = accept( socket_fd, (struct sockaddr *)&remote_addr, &addr_len );
        for(;;){
            memset(in_buf, 0, MESSAGE_SIZE);

            //receive network data and print it
            ret = recv( accept_fd ,(void*)in_buf ,MESSAGE_SIZE ,0 );
            if(ret == 0 ){
                break;
            }
            printf( "receive message:%s\n", in_buf );
            send(accept_fd, (void*)in_buf, MESSAGE_SIZE, 0);
        }
        printf("close client connection...\n");
        close(accept_fd);
    }

    printf("quit server...\n");
    close(socket_fd);

    return 0;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

TCP 客户端

在这里插入图片描述

客户端 类型 但是少了 绑定和侦听
因为在Connect阶段系统会默认分配

但是需要提前输入服务端的地址 代码如下

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);

//inet_addr()函数,将点分十进制IP转换成网络字节序IP

serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

if(connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
    perror("connect");
    return 1;
}
printf("success to connect server...\n");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

客户端完整代码

//
// Created by zyc on 2022/5/5.
//

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

#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERVER_PORT 8111
#define MESSAGE_LENGTH 1024

int main(){
    int ret = -1;
    int socket_fd;

    //server addr
    struct sockaddr_in serverAddr;

    char sendbuf[MESSAGE_LENGTH];
    char recvbuf[MESSAGE_LENGTH];

    int data_len;

    if((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        return 1;
    }

    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);

    //inet_addr()函数,将点分十进制IP转换成网络字节序IP
//    serverAddr.sin_addr.s_addr = inet_addr("39.105.185.198");
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if(connect(socket_fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
    {
        perror("connect");
        return 1;
    }
    printf("success to connect server...\n");

    while(1)
    {
        memset(sendbuf, 0, MESSAGE_LENGTH);

        printf("<<<<send message:");

        gets(sendbuf);
        //printf("\n");
        ret = send(socket_fd, sendbuf, strlen(sendbuf), 0);
        if(ret <= 0 ){
            printf("the connection is disconnection!\n");
            break;
        }

        if(strcmp(sendbuf, "quit") == 0){
            break;
        }

        printf(">>> echo message:");

        recvbuf[0] = '\0';
        data_len = recv(socket_fd, recvbuf, MESSAGE_LENGTH, 0);

        recvbuf[data_len] = '\0';

        printf("%s\n", recvbuf);

    }

    close(socket_fd);

    return 0;

}





  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/144078
推荐阅读
相关标签
  

闽ICP备14008679号