C语言-Socket

January 15, 2021 C语言 访问: 75 次

在C语言中,提供了Socket相关的库文件,流程和pythonsocket差不多

头文件

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

函数

socket相关的函数

extern int socket (int __domain, int __type, int __protocol)
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
recv (int __fd, void *__buf, size_t __n, int __flags)
extern int accept (int __fd, __SOCKADDR_ARG __addr,
           socklen_t *__restrict __addr_len);
           extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)

socket相关的结构体

struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;            /* Port number.  */
    struct in_addr sin_addr;        /* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
               __SOCKADDR_COMMON_SIZE -
               sizeof (in_port_t) -
               sizeof (struct in_addr)];
  };

数据处理的函数

long 类型的大小端序转变
extern uint32_t ntohl (uint32_t __netlong)
extern uint32_t htonl (uint32_t __hostlong)

short 类型的大小端序转变
extern uint16_t ntohs (uint16_t __netshort)
extern uint16_t htons (uint16_t __hostshort)

extern in_addr_t inet_addr (const char *__cp)//将一个点分十进制的IP转换成一个长整数型数
extern char *inet_ntoa (struct in_addr __in) //将网络地址转换成“.”点隔的字符串格式

服务端

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/syscall.h>
#include<linux/memfd.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<arpa/inet.h>

#define PORT 12345

int main()
{
    setbuf(stdin,0);
    setbuf(stdout,0);
    setbuf(stderr,0);
    int s = socket(AF_INET,SOCK_STREAM,0);
    int return_val=0;
    if(s == -1)
    {
        puts("socket error!");
        exit(0);
    }
    struct sockaddr_in server_info;
    memset(&server_info,0,sizeof(struct sockaddr_in));
    server_info.sin_family = AF_INET;
    server_info.sin_port=htons(PORT);
    server_info.sin_addr.s_addr=inet_addr("0.0.0.0");

    return_val = bind(s,(struct sockaddr *)&server_info,sizeof(struct sockaddr_in));
    if(return_val == -1)
    {
        printf("bind error!");
        exit(0);
    }

    return_val = listen(s,5);
    if(return_val == -1)
    {
        printf("listen error!");
        exit(0);
    }
    struct sockaddr_in client_info;
    memset(&client_info,0,sizeof(struct sockaddr_in));
    int client_info_len=sizeof(struct sockaddr_in);
    int c = accept(s,(struct sockaddr *)&client_info,&client_info_len);
    if(c == -1)
    {
        printf("accept error!");
        exit(0);
    }
    printf("成功接收到一个客户端:%s:%d\n", inet_ntoa(client_info.sin_addr),ntohs(client_info.sin_port));
    char msg[0x50] = "hello,i am server!";
    return_val = send(c,msg,strlen(msg),0);
    if(return_val == -1)
    {
        printf("send error!");
        exit(0);
    }

    char *message = malloc(0x100);
    recv(c,message,0x20,0);
    printf("message:%s\n",message);
    close(c);
    close(s);
    return 0;
}

客户端

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

int main()
{
    struct sockaddr_in server_info,client_info;
    int return_val=0;
    memset(&server_info,0,sizeof(struct sockaddr_in));
    memset(&client_info,0,sizeof(struct sockaddr_in));

    server_info.sin_family=AF_INET;
    server_info.sin_port = htons(12345);
    server_info.sin_addr.s_addr = inet_addr("10.211.55.16");

    client_info.sin_family=AF_INET;
    client_info.sin_port = htons(10086);
    client_info.sin_addr.s_addr = inet_addr("0.0.0.0");


    int s = socket(AF_INET,SOCK_STREAM,0);
    if(s == -1)
    {
        puts("socket error!");
        exit(0);
    }
    
    return_val = bind(s,(struct sockaddr *)&client_info,sizeof(struct sockaddr_in));// set port 
    if(return_val == -1)
    {
        printf("bind error!");
        exit(0);
    }

    return_val = connect(s,(struct sockaddr *)&server_info,sizeof(struct sockaddr_in));
    if(return_val == -1)
    {
        printf("listen error!");
        exit(0);
    }
    printf("connect success!\n");
    char *message = malloc(0x100);
    recv(s,message,0x20,0);
    printf("message: %s\n",message);
    char msg[0x50] = "hello,i am client!";
    send(s,msg,strlen(msg),0);
    close(s);
}

添加新评论