C语言网络编程实践(TCP套接字编程实例)

C语言网络编程实践涉及到使用C语言以及相关库来实现网络通信。

图片[1]-C语言网络编程实践(TCP套接字编程实例)-不念博客

下面是一个简单的TCP套接字编程实例,展示了一个简单的客户端-服务器模型:

服务器端(server.c)

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int server_fd, client_fd, addr_len;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(server_addr.sin_zero), '<pre class="wp-block-zibllblock-enlighter"><code class="gl" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int server_fd, client_fd, addr_len;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(server_addr.sin_zero), '\0', 8);
// 绑定地址到套接字
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
// 监听客户端连接
if (listen(server_fd, 10) == -1) {
perror("listen");
exit(1);
}
printf("Server is listening on port 8080...\n");
addr_len = sizeof(struct sockaddr_in);
// 接受客户端连接
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
perror("accept");
exit(1);
}
printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr));
// 接收客户端消息
recv(client_fd, buffer, sizeof(buffer), 0);
printf("Client said: %s\n", buffer);
// 向客户端发送消息
send(client_fd, "Hello, Client!", 15, 0);
// 关闭套接字
close(client_fd);
close(server_fd);
return 0;
}</code></pre>', 8);
// 绑定地址到套接字
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
// 监听客户端连接
if (listen(server_fd, 10) == -1) {
perror("listen");
exit(1);
}
printf("Server is listening on port 8080...\n");
addr_len = sizeof(struct sockaddr_in);
// 接受客户端连接
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
perror("accept");
exit(1);
}
printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr));
// 接收客户端消息
recv(client_fd, buffer, sizeof(buffer), 0);
printf("Client said: %s\n", buffer);
// 向客户端发送消息
send(client_fd, "Hello, Client!", 15, 0);
// 关闭套接字
close(client_fd);
close(server_fd);
return 0;
}
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main() {
    int server_fd, client_fd, addr_len;
    struct sockaddr_in server_addr, client_addr;
    char buffer[1024];

    // 创建套接字
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    // 配置服务器地址
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    memset(&(server_addr.sin_zero), '<pre class="wp-block-zibllblock-enlighter"><code class="gl" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int server_fd, client_fd, addr_len;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(server_addr.sin_zero), '\0', 8);
// 绑定地址到套接字
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
// 监听客户端连接
if (listen(server_fd, 10) == -1) {
perror("listen");
exit(1);
}
printf("Server is listening on port 8080...\n");
addr_len = sizeof(struct sockaddr_in);
// 接受客户端连接
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
perror("accept");
exit(1);
}
printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr));
// 接收客户端消息
recv(client_fd, buffer, sizeof(buffer), 0);
printf("Client said: %s\n", buffer);
// 向客户端发送消息
send(client_fd, "Hello, Client!", 15, 0);
// 关闭套接字
close(client_fd);
close(server_fd);
return 0;
}</code></pre>', 8);

    // 绑定地址到套接字
    if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    // 监听客户端连接
    if (listen(server_fd, 10) == -1) {
        perror("listen");
        exit(1);
    }

    printf("Server is listening on port 8080...\n");

    addr_len = sizeof(struct sockaddr_in);

    // 接受客户端连接
    if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
        perror("accept");
        exit(1);
    }

    printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr));

    // 接收客户端消息
    recv(client_fd, buffer, sizeof(buffer), 0);
    printf("Client said: %s\n", buffer);

    // 向客户端发送消息
    send(client_fd, "Hello, Client!", 15, 0);

    // 关闭套接字
    close(client_fd);
    close(server_fd);

    return 0;
}
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> int main() { int server_fd, client_fd, addr_len; struct sockaddr_in server_addr, client_addr; char buffer[1024]; // 创建套接字 if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } // 配置服务器地址 server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = INADDR_ANY; memset(&(server_addr.sin_zero), '
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int server_fd, client_fd, addr_len;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(server_addr.sin_zero), '\0', 8);
// 绑定地址到套接字
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
// 监听客户端连接
if (listen(server_fd, 10) == -1) {
perror("listen");
exit(1);
}
printf("Server is listening on port 8080...\n");
addr_len = sizeof(struct sockaddr_in);
// 接受客户端连接
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
perror("accept");
exit(1);
}
printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr));
// 接收客户端消息
recv(client_fd, buffer, sizeof(buffer), 0);
printf("Client said: %s\n", buffer);
// 向客户端发送消息
send(client_fd, "Hello, Client!", 15, 0);
// 关闭套接字
close(client_fd);
close(server_fd);
return 0;
}
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int server_fd, client_fd, addr_len;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(server_addr.sin_zero), '\0', 8);
// 绑定地址到套接字
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
// 监听客户端连接
if (listen(server_fd, 10) == -1) {
perror("listen");
exit(1);
}
printf("Server is listening on port 8080...\n");
addr_len = sizeof(struct sockaddr_in);
// 接受客户端连接
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
perror("accept");
exit(1);
}
printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr));
// 接收客户端消息
recv(client_fd, buffer, sizeof(buffer), 0);
printf("Client said: %s\n", buffer);
// 向客户端发送消息
send(client_fd, "Hello, Client!", 15, 0);
// 关闭套接字
close(client_fd);
close(server_fd);
return 0;
}
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> int main() { int server_fd, client_fd, addr_len; struct sockaddr_in server_addr, client_addr; char buffer[1024]; // 创建套接字 if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } // 配置服务器地址 server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = INADDR_ANY; memset(&(server_addr.sin_zero), '\0', 8); // 绑定地址到套接字 if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } // 监听客户端连接 if (listen(server_fd, 10) == -1) { perror("listen"); exit(1); } printf("Server is listening on port 8080...\n"); addr_len = sizeof(struct sockaddr_in); // 接受客户端连接 if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) { perror("accept"); exit(1); } printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr)); // 接收客户端消息 recv(client_fd, buffer, sizeof(buffer), 0); printf("Client said: %s\n", buffer); // 向客户端发送消息 send(client_fd, "Hello, Client!", 15, 0); // 关闭套接字 close(client_fd); close(server_fd); return 0; }
', 8); // 绑定地址到套接字 if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } // 监听客户端连接 if (listen(server_fd, 10) == -1) { perror("listen"); exit(1); } printf("Server is listening on port 8080...\n"); addr_len = sizeof(struct sockaddr_in); // 接受客户端连接 if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) { perror("accept"); exit(1); } printf("Server received a connection from %s\n", inet_ntoa(client_addr.sin_addr)); // 接收客户端消息 recv(client_fd, buffer, sizeof(buffer), 0); printf("Client said: %s\n", buffer); // 向客户端发送消息 send(client_fd, "Hello, Client!", 15, 0); // 关闭套接字 close(client_fd); close(server_fd); return 0; }

客户端(client.c)

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建套接字
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(server_addr.sin_zero), '<pre class="wp-block-zibllblock-enlighter"><code class="gl" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建套接字
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(server_addr.sin_zero), '\0', 8);
// 连接到服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
// 向服务器发送消息
send(client_fd, "Hello, Server!", 15, 0);
// 接收服务器回复
recv(client_fd, buffer, sizeof(buffer), 0);
</code></pre>', 8);
// 连接到服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
// 向服务器发送消息
send(client_fd, "Hello, Server!", 15, 0);
// 接收服务器回复
recv(client_fd, buffer, sizeof(buffer), 0);
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main() {
    int client_fd;
    struct sockaddr_in server_addr;
    char buffer[1024];

    // 创建套接字
    if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    // 配置服务器地址
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(&(server_addr.sin_zero), '<pre class="wp-block-zibllblock-enlighter"><code class="gl" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建套接字
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(server_addr.sin_zero), '\0', 8);
// 连接到服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
// 向服务器发送消息
send(client_fd, "Hello, Server!", 15, 0);
// 接收服务器回复
recv(client_fd, buffer, sizeof(buffer), 0);
</code></pre>', 8);

    // 连接到服务器
    if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        perror("connect");
        exit(1);
    }

    // 向服务器发送消息
    send(client_fd, "Hello, Server!", 15, 0);

    // 接收服务器回复
    recv(client_fd, buffer, sizeof(buffer), 0);
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> int main() { int client_fd; struct sockaddr_in server_addr; char buffer[1024]; // 创建套接字 if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } // 配置服务器地址 server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); memset(&(server_addr.sin_zero), '
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建套接字
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(server_addr.sin_zero), '\0', 8);
// 连接到服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
// 向服务器发送消息
send(client_fd, "Hello, Server!", 15, 0);
// 接收服务器回复
recv(client_fd, buffer, sizeof(buffer), 0);
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建套接字
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 配置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(server_addr.sin_zero), '\0', 8);
// 连接到服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
// 向服务器发送消息
send(client_fd, "Hello, Server!", 15, 0);
// 接收服务器回复
recv(client_fd, buffer, sizeof(buffer), 0);
#include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <unistd.h> int main() { int client_fd; struct sockaddr_in server_addr; char buffer[1024]; // 创建套接字 if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } // 配置服务器地址 server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); memset(&(server_addr.sin_zero), '\0', 8); // 连接到服务器 if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } // 向服务器发送消息 send(client_fd, "Hello, Server!", 15, 0); // 接收服务器回复 recv(client_fd, buffer, sizeof(buffer), 0);
', 8); // 连接到服务器 if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } // 向服务器发送消息 send(client_fd, "Hello, Server!", 15, 0); // 接收服务器回复 recv(client_fd, buffer, sizeof(buffer), 0);
© 版权声明
THE END