Linux C UDP Socket实现客户与服务器简单通信

在linux平台下用c语言udp协议实现客户与服务器的简单通信与封装的实现

一、服务器端

1、新建CreateServer.c和CreateServer.h文件,服务器的创建代码在此实现,程序中封装了创建TcpSocket和UdpSocket服务器的方法,可直接调用函数创建服务器,客户端的业务则单独处理,方便了逻辑设计与代码移植。

CreateServer.h代码如下:

#ifndef CREATESERVER_H
#define CREATESERVER_H

int create_tcp_server(int);
int create_udp_server(int);

#endif // CREATESERVER_H

CreateServer.c代码如下:

#include 
#include 
#include 
#define LISTEN_SIZE 20

int start_server(int port, int type){
    //建立服务器套接字
    int ss = socket(AF_INET, type, 0);
    if(ss < 0){
        printf("create socket error\n");
        return -1;
    }

    //设置服务器地址
    struct sockaddr_in server_addr; //服务器地址结构
    bzero(&server_addr, sizeof(struct sockaddr_in)); //清零
    server_addr.sin_family = AF_INET; //协议族
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //ip地址
    server_addr.sin_port = htons(port); //端口
    //绑定地址结构到套接字描述符
    if(bind(ss, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){
        printf("bind error\n");
        return -1;
    }
    //TCP
    if(SOCK_STREAM == type){
        //设置侦听
        if(listen(ss, LISTEN_SIZE) < 0){
            printf("listen error\n");
            return -1;
        }
        printf("tcp server start\n");
    }
    else
        printf("udp server start\n");
    return ss;
}

int create_tcp_server(int port){
    start_server(port, SOCK_STREAM);
}

int create_udp_server(int port){
    start_server(port, SOCK_DGRAM);
}

main.c代码如下:

#include 
#include 
#include 
#include 
#define PORT 8888
#define BUFF_LEN 1024

void udpserv_echo(int ss, struct sockaddr* client){
    int n;
    char buff[BUFF_LEN];
    socklen_t len;
    while(1){
        bzero(buff, BUFF_LEN);
        len = sizeof(*client);
        n = recvfrom(ss, buff, BUFF_LEN, 0, client, &len);
        sendto(ss, buff, n, 0, client, len);
        printf("%s\n", buff);
    }
}

int main(){
    int ss = create_udp_server(PORT);
    if(-1 == ss)
        exit(-1);

    //回显处理程序
    struct sockaddr_in addr_clie;
    udpserv_echo(ss, (struct sockaddr*)&addr_clie);
    close(ss);
    return 0;
}

二、客户端
客户端Socket.h和Socket.c封装了创建TcpSocket和UdpSocket的代码,可直接调用函数连接到服务器。

Socket.h代码如下:

#ifndef SOCKET_H
#define SOCKET_H
int connect_tcp(char*, int);
int connect_udp(char*, int);
#endif // SOCKET_H

Socket.c代码如下:

#include 
#include 
#include 
int connectsock(char* server_ip, int server_port, int type){
    int sock_fd = socket(AF_INET, type, 0);
    if(-1 == sock_fd){
        printf("create socket error\n");
        return -1;
    }
    struct sockaddr_in server_addr;
    //设置服务器地址
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(server_port);
    inet_pton(AF_INET, server_ip, &server_addr.sin_addr);

    //连接服务器
    if(-1 == connect(sock_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in))){
        printf("connect server error\n");
        return -1;
    }

    printf("connect server success\n");
    return sock_fd;
}
int connect_tcp(char* server_ip, int server_port){
    return connectsock(server_ip, server_port, SOCK_STREAM);
}
int connect_udp(char* server_ip, int server_port){
    return connectsock(server_ip, server_port, SOCK_DGRAM);
}

main.c代码如下:

#include 
#include 

#define IP "127.0.0.1"
#define PORT 8888
#define BUFF_LEN 1024

void udpclie_echo(int s){
    char buff[BUFF_LEN];
    while(1){
        bzero(buff, BUFF_LEN);
        scanf("%s", buff);
        write(s, buff, BUFF_LEN);
        bzero(buff, BUFF_LEN);
        read(s, buff, BUFF_LEN);
        printf("recved:%s\n",buff);
    }
}

int main(){
    int s = connect_udp(IP, PORT);
    if(-1 == s)
        exit(-1);
    udpclie_echo(s);
    close(s);
    return 0;
}

源码下载地址:
https://github.com/Wushaoling/Linux-C-Socket/tree/master/UDP

你可能感兴趣的:(Linux,操作系统实践)