C语言实现服务器,客户端windows网络编程


编译环境

我是在Windows10 + gcc8.1.0环境下写的,编辑器用的是vsCode。

服务器端

基于TCP实现web服务器,在win下socket的交互步骤为:

  1. WSAStartup进行初始化
  2. socket创建套接字
  3. bind绑定
  4. listen监听
  5. accept接受请求
  6. send/recv发送或接受数据
  7. closesocket关闭socket
  8. WSACleanup最终关闭
代码如下:

#include 
#include 
#include 

void sys_err(const char *str)
{
    fprintf(stderr, "%s", str);
    exit(1);
}
int main(void)
{
    WSADATA wsadata;
    int err = WSAStartup(MAKEWORD(2, 2), &wsadata);  //初始化ws2_32.dll动态连接库
    if(err) 
        sys_err("open socket error\n");
    
    
    /*  成功:返回新套接字对应文件描述符
        失败:返回INVALID_SOCKET*/
    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(serverSocket == INVALID_SOCKET)  
        sys_err("serversocket error\n");
    
    struct sockaddr_in server_addr;
    server_addr.sin_family = PF_INET;
    server_addr.sin_port = htons(9527);
    server_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);   /*inet_addr("123.23.12.1")*/
    /*成功 0
      失败 -1*/
    if(bind(serverSocket, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
        sys_err("bind error\n");

    /*同时进行3次握手的客户端数量
      成功 0
      失败 -1*/
    if(listen(serverSocket, 128) == -1)
        sys_err("listen error\n");
    
    struct sockaddr_in client_addr;
    int client_addr_len;
    /*阻塞等待客户端建立连接
      成功:返回一个与客户端连接的socket文件描述符*/
    SOCKET connectSocket = accept(serverSocket, (struct sockaddr *)&client_addr, &client_addr_len);
    if(connectSocket == INVALID_SOCKET)
        sys_err("sonnectSocket error\n");


    printf("client: ip:%s port: %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

    char buf[512];
    while(1)
    {
        recv(connectSocket, buf, sizeof(buf), 0);
        send(connectSocket, buf, sizeof(buf), 0);
    }
    printf("success\n");
    closesocket(serverSocket);
    closesocket(connectSocket);
    WSACleanup();   //释放ws2_32.dll动态链接库
    return 0;
}

客户端

客户端与服务器交互步骤要简单一些

  1. WSAStartup进行初始化
  2. socket创建套接字
  3. connect建立连接
  4. send/recv发送或接受数据
  5. closesocket关闭socket
  6. WSACleanup最终关闭

#include 
#include 
#include 
int main(void)
{
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);  //初始化ws2_32.dll动态连接库

    SOCKET clientSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(39700);
    server_addr.sin_addr.S_un.S_addr = inet_addr("47.111.1.169");
    connect(clientSocket, (const struct sockaddr *)&server_addr, sizeof(server_addr));
    
    char buf[512];
    while(1)
    {
        scanf("%s", buf);
        send(clientSocket, buf, sizeof(buf), 0);
        //recv(clientSocket, buf, sizeof(buf), 0);
        //printf("%s\n", buf);
    }
    
    closesocket(clientSocket);
    WSACleanup();
    return 0;
}

注意:编译命令需要加上-lwsock32

gcc server.c -o server.exe -lwsock32
gcc client.c -o client.exe -lwsock32
好了(跑)

声明:Trikker的小破站|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - C语言实现服务器,客户端windows网络编程


分享一些学习成果