Linux 网络编程(UDP)

☆樱花仙子☆ 提交于 2020-04-04 16:28:42
  • 客户端代码
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<sys/socket.h>  
#include<sys/types.h>  
#include<unistd.h>  
#include<netinet/in.h>
#include <errno.h> 

int main(int argc, char** argv)
{
    int socketfd = 0;
    struct sockaddr_in server_addr;
    char recvbuf[1024];
    int port = 8001;
    int nbyte;

    if((socketfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0){
        fprintf(stderr, "socket Error:%s\n\a", strerror(errno));   
        exit(1);
    }

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

    int len = sizeof(struct sockaddr_in);

    while(1){
        if(sendto(socketfd, "Hello Fredric", strlen("Hello Fredric"), 0, 
            (struct sockaddr *)&server_addr, len) == -1){

            fprintf(stderr, "sendto Error:%s\n\a", strerror(errno)); 
            close(socketfd); 
            exit(1);
        }

        if((nbyte = recvfrom(socketfd, recvbuf, 1024, 0, (struct sockaddr *)&server_addr, &len)) == -1){
            fprintf(stderr, "recvfrom Error:%s\n\a", strerror(errno));  
            close(socketfd);
            exit(1);
        }

        recvbuf[nbyte] = '\0';
        printf("recv data is:%s\n",recvbuf);
    }

    close(socketfd);

    exit(0);
}
  • 服务器端代码
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<sys/socket.h>  
#include<sys/types.h>  
#include<unistd.h>  
#include<netinet/in.h>
#include <errno.h>

int main(int argc, char** argv){

    int socketfd = 0;
    struct sockaddr_in server_addr;
    char recvbuf[1024];
    char sendbuf[1024];
    int port = 8001;
    int nbyte;

    if((socketfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0){
        fprintf(stderr, "socket Error:%s\n\a", strerror(errno));   
        exit(1);
    }

    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if((bind(socketfd, &server_addr, sizeof(server_addr))) == -1){
        fprintf(stderr, "bind Error:%s\n\a", strerror(errno));   
        exit(1);
    }
    int len = sizeof(struct sockaddr_in);
    while(1){
        if((nbyte = recvfrom(socketfd, recvbuf, 1024, 0, (struct sockaddr*)&server_addr, &len)) == -1){
            fprintf(stderr, "recvfrom Error:%s\n\a", strerror(errno)); 
            close(socketfd);
            exit(1);
        }

        recvbuf[nbyte] = '\0';
        printf("recv data is:%s\n",recvbuf);

        printf("Input:");
        scanf("%s", sendbuf);

        if((nbyte = sendto(socketfd, sendbuf, sizeof(sendbuf), 
        0, (struct sockaddr*)&server_addr, len)) == -1){
            fprintf(stderr, "sendto Error:%s\n\a", strerror(errno)); 
            close(socketfd); 
            exit(1);
        }
    }

    close(socketfd);
    exit(0);
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!