C语言socket编程

参考了以下帖子:
https://www.cnblogs.com/zkfopen/p/9441264.html
https://www.cnblogs.com/leezheng/p/8030011.html

一、TCP通信

// tcp-server.c
#include
#include
#include
#include
#include
#include
#include
#include

#define MAXLINE 4096

int main(int argc, char** argv){
    int  listenfd, connfd;
    struct sockaddr_in  servaddr;
    char  buff[4096];
    int  n;

    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
        printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(6666);

    if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
        printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    if( listen(listenfd, 10) == -1){
        printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    printf("======waiting for client's request======\n");
    while(1){
        if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
            printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
            continue;
        }
        n = recv(connfd, buff, MAXLINE, 0);
        buff[n] = '\0';
        printf("recv msg from client: %s\n", buff);
        close(connfd);
    }
    close(listenfd);
    return 0;
}
// tcp-client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 4096

int main(int argc, char** argv){
    int   sockfd, n;
    char  recvline[4096], sendline[4096];
    struct sockaddr_in  servaddr;

    if( argc != 2){
        printf("usage: ./client \n");
        return 0;
    }

    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
        return 0;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(6666);
    if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
        printf("inet_pton error for %s\n",argv[1]);
        return 0;
    }

    if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
        printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    printf("send msg to server: \n");
    fgets(sendline, 4096, stdin);
    if( send(sockfd, sendline, strlen(sendline), 0) < 0){
        printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
        return 0;
    }
    close(sockfd);
    return 0;
}
all:server client

server:tcp-server.o
    g++ -g -o server tcp-server.o
client:tcp-client.o
    g++ -g -o client tcp-client.o
tcp-server.o:tcp-server.c
    g++ -g -c tcp-server.c
tcp-client.o:tcp-client.c
    g++ -g -c tcp-client.c
clean:all
    rm server client tcp-server.o tcp-client.o

一、UDP通信

// socket udp 服务端
// udp-server.c
#include
#include
#include
#include
#include

int main()
{
    //创建socket对象
    int sockfd=socket(AF_INET,SOCK_DGRAM,0);

    //创建网络通信对象
    struct sockaddr_in addr;
    addr.sin_family =AF_INET;
    addr.sin_port =htons(1324);
    addr.sin_addr.s_addr=inet_addr("127.0.0.1");

    //绑定socket对象与通信链接
    int ret =bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    if(0>ret)
    {
        printf("bind\n");
        return -1;

    }
    struct sockaddr_in cli;
    socklen_t len=sizeof(cli);

    while(1)
    {
        char buf =0;
        recvfrom(sockfd,&buf,sizeof(buf),0,(struct sockaddr*)&cli,&len);
        printf("recv num =%hhd\n",buf);

        buf =66;
        sendto(sockfd,&buf,sizeof(buf),0,(struct sockaddr*)&cli,len);

    }
    close(sockfd);

}
// socket udp 客户端
// udp-client.c
#include
#include
#include
#include
#include

int main()
{
    //创建socket对象
    int sockfd=socket(AF_INET,SOCK_DGRAM,0);

    //创建网络通信对象
    struct sockaddr_in addr;
    addr.sin_family =AF_INET;
    addr.sin_port =htons(1324);
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    while(1)
    {
        printf("请输入一个数字:");
        char buf=0;
        scanf("%hhd",&buf);
        sendto(sockfd,&buf,
        sizeof(buf),0,(struct sockaddr*)&addr,sizeof(addr));

        socklen_t len=sizeof(addr);
        recvfrom(sockfd,&buf,sizeof(buf),0,(struct sockaddr*)&addr,&len);

        if(66 ==buf)
        {
            printf(" server 成功接受\n");
        }
        else
        {
            printf("server 数据丢失\n");
        }

    }
    close(sockfd);

}
all:server client

server:udp-server.o
    g++ -g -o server udp-server.o
client:udp-client.o
    g++ -g -o client udp-client.o
udp-server.o:udp-server.c
    g++ -g -c udp-server.c
udp-client.o:udp-client.c
    g++ -g -c udp-client.c
clean:all
    rm server client udp-server.o udp-client.o

你可能感兴趣的:(C语言socket编程)