0


Linux下TCP、UDP、UDP广播通信代码及运行

一、网络环境配置

网络通信方式可选择一台主机里的两个虚拟机,也可以两台主机里的两个虚拟机,本文采用方式二。

主机一IP地址为192.168.1.109

主机二IP地址为192.168.1.24

必须使两台主机在一个网段内,并且可以ping通才可进行以下操作。

二、TCP连接

客户端(主机一)代码

TCP_client.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
    unsigned short port = 8000; 
    char *server_ip = "192.168.1.24";   //设置第二个主机ip地址为服务器地址,进行TCP通信
    char send_buf[512] = "hello every!"; //设置发送的内容
    char recv_buf[512] = "";
    int sockfd = 0;
    int err_log = 0;
    struct sockaddr_in server_addr;
    if( argc > 1 ) 
        server_ip = argv[1];
    if( argc > 2 ) 
        port = atoi(argv[2]);
    bzero(&server_addr,sizeof(server_addr)); 
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if(sockfd < 0)
    {
        perror("socket");
        exit(-1);
    }
    err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    if(err_log != 0)
    {
        perror("connect");
        close(sockfd);
        exit(-1);
    }
    send(sockfd, send_buf, strlen(send_buf), 0); 
    recv(sockfd, recv_buf, sizeof(recv_buf), 0); 
    printf("%s\n", recv_buf);
    close(sockfd);
    return 0;
}

服务器端(主机二)代码

TCP_server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
    char recv_buf[2048] = ""; 
    int sockfd = 0; 
    int connfd = 0;
    int err_log = 0;
    struct sockaddr_in my_addr; 
    unsigned short port = 8000; 
    if(argc > 1) 
        port = atoi(argv[1]);
    printf("TCP Server Started at port %d!\n", port);
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if(sockfd < 0)
    {
        perror("socket");
        exit(-1);
    }
    bzero(&my_addr, sizeof(my_addr)); 
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(port);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    printf("Binding server to port %d\n", port);
    err_log = bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr));
    if( err_log != 0)
    {
        perror("binding");
        close(sockfd);
        exit(-1);
    }
    err_log = listen(sockfd, 10);
    if(err_log != 0)
    {
        perror("listen");
        close(sockfd);
        exit(-1);
    }
    printf("Waiting client...\n");
    while(1)
    {
        size_t recv_len = 0;
        struct sockaddr_in client_addr; 
        char cli_ip[INET_ADDRSTRLEN] = ""; 
        socklen_t cliaddr_len = sizeof(client_addr); 
        connfd = accept(sockfd, (struct sockaddr*)&client_addr, &cliaddr_len); 
        if(connfd < 0)
        {
            perror("accept");
            continue;
        }
        inet_ntop(AF_INET, &client_addr.sin_addr, cli_ip, INET_ADDRSTRLEN);
        printf("client ip = %s\n", cli_ip);
        
        while((recv_len = recv(connfd, recv_buf, sizeof(recv_buf), 0)) > 0)
            send(connfd, recv_buf, recv_len, 0);
            printf("%s\n",recv_buf);
        close(connfd); 
        printf("client closed!\n");
    }
    close(sockfd); 
    return 0;
}

客户端输入代码

gcc TCP_client.c -o TCP_client
./TCP_clent

服务器端输入代码

gcc TCP_server.c -o TCP_server
./TCP_server

三、UDP连接

客户端(主机一)代码

UDP_client.c

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

int main (int argc, char *argv[])
{
    int sockfd = 0;
    struct sockaddr_in server_addr;
    unsigned short port = 8000;
    char *ser_ip = "192.168.1.24";

    if(argc >1)
        ser_ip = argv[1];
    if(argc >2)
        port = atoi(argv[2]);
    
    sockfd = socket (AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        perror("socket error!");
        exit(-1);
    }

    bzero (&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    inet_pton(AF_INET, ser_ip,&server_addr.sin_addr);

    printf("ready send data to UDP server %s:%d!\n",ser_ip,port);
    while (1)
    {
        char send_buf[2048] = "";
        int len = 0;

        fgets(send_buf, sizeof(send_buf),stdin);
        send_buf[strlen(send_buf)-1] ='\0';

        sendto(sockfd, send_buf, strlen(send_buf),0,(struct sockaddr*)&server_addr,sizeof(server_addr));

        len = recvfrom(sockfd, send_buf, sizeof(send_buf),0,NULL,NULL);
        printf("%s\n",send_buf);
    }
    close(sockfd);
    return 0;

}

服务器端(主机二)代码

UDP_server.c

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

int main(int argc, char *argv[])
{
    int sockfd = 0;
    int error_log = 0;
    struct sockaddr_in bind_addr;
    unsigned short port = 8000;
    
    if (argc > 1)
        port = atoi(argv[1]);
    printf("UDP server Started!\n");

    sockfd = socket(AF_INET, SOCK_DGRAM,0);
    if (sockfd < 0)
    {
        perror("socket error");
        exit(-1);
    }

    bzero(&bind_addr, sizeof(bind_addr));
    bind_addr.sin_family = AF_INET;
    bind_addr.sin_port = htons(port);
    bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    printf("Binding server to port %d\n", port);
    error_log = bind(sockfd, (struct  sockaddr *)&bind_addr, sizeof(bind_addr));
      if (error_log  != 0)
    {
        perror("bind error");
        close(sockfd);
        exit(-1);
    }

    printf("waiting data from other client...\n");
    while(1)
    {
        char recv_buf[1024] = "";
        char cli_ip[INET_ADDRSTRLEN] = "";
        int recv_len =0;
        struct sockaddr_in client_addr;
        socklen_t client_addr_len = sizeof(client_addr);

        recv_len = recvfrom(sockfd, recv_buf,sizeof(recv_buf), 0 ,(struct sockaddr*)&client_addr,&client_addr_len);

        inet_ntop(AF_INET, &client_addr.sin_addr,cli_ip,INET_ADDRSTRLEN);
        printf("clinet ip = %s\n", cli_ip);
        printf("recver message :%s\n",recv_buf);
        sendto(sockfd, recv_buf,recv_len,0,(struct sockaddr*)&client_addr,&client_addr_len);
    }
    close(sockfd);
      return 0;
  
}

客户端输入代码

gcc UDP_client.c -o UDP_client
./UDP_client
hello

服务器端输入代码

gcc UDP_server.c -o UDP_server
./UDP_server

四、UDP广播

发送端(主机一)代码

send.c

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

int main(){
    char msg[128] = "I am broadCast message from server!";
    
    struct sockaddr_in saddr, caddr;
    socklen_t slen, clen;
    int lfd, ret;

    lfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(lfd < 0){
        perror("socket create error");
        exit(1);
    }
    int flag = 1;
    setsockopt(lfd, SOL_SOCKET, SO_BROADCAST | SO_REUSEADDR, &flag, sizeof(flag) );

    caddr.sin_family = AF_INET;
    caddr.sin_port = htons(4001);
    caddr.sin_addr.s_addr = inet_addr("192.168.1.255");
    clen = sizeof(caddr);
    
        int n = sendto(lfd, msg, strlen(msg), 0, (struct sockaddr *)&caddr, clen);
        if(n  < 0){
            perror("sendto Error:");
            exit(1);
        }
        close(lfd);
    return 0;
}

接收端(主机二)代码

recv.c

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<arpa/inet.h>
int main(){
    int sockListen;
    if((sockListen = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
        printf("socket fail\n");
        return -1;
    }
    int set = 1;
    setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(int));
    struct sockaddr_in recvAddr;
    memset(&recvAddr, 0, sizeof(struct sockaddr_in));
    recvAddr.sin_family = AF_INET;
    recvAddr.sin_port = htons(4001);
    recvAddr.sin_addr.s_addr = INADDR_ANY;
    
    if(bind(sockListen, (struct sockaddr *)&recvAddr, sizeof(struct sockaddr)) == -1){
        printf("bind fail\n");
        return -1;
    }
    int recvbytes;
    char recvbuf[128];
    int addrLen = sizeof(struct sockaddr_in);
    if((recvbytes = recvfrom(sockListen, recvbuf, 128, 0,
        (struct sockaddr *)&recvAddr, &addrLen)) != -1){
        recvbuf[recvbytes] = '\0';
        printf("receive a broadCast messgse:%s\n", recvbuf);
    }else{
        printf("recvfrom fail\n");
    }
    close(sockListen);
    return 0;
}

发送端输入代码

gcc send.c -o send
./send

接收端输入代码

gcc recv.c -o recv
./recv

标签: udp linux tcp/ip

本文转载自: https://blog.csdn.net/m0_47171654/article/details/125279858
版权归原作者 JonathanQian- 所有, 如有侵权,请联系我们删除。

“Linux下TCP、UDP、UDP广播通信代码及运行”的评论:

还没有评论