生成基於C语言的tcp和udp通信服務器代碼
时间: 2023-10-14 16:29:11 浏览: 151
C语言实现TCP和UDP通信
4星 · 用户满意度95%
很高兴为您解答此问题。下面是一个使用C语言编写的TCP和UDP通信服务器代码示例:
TCP服务器代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int listen_sock, conn_sock;
struct sockaddr_in server_addr, client_addr;
char buf[1024];
int bytes_read;
// 创建套接字
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock == -1) {
perror("socket() failed");
exit(1);
}
// 设置地址重用
int reuse = 1;
if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
perror("setsockopt() failed");
exit(1);
}
// 绑定地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);
if (bind(listen_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind() failed");
exit(1);
}
// 开始监听
if (listen(listen_sock, 5) == -1) {
perror("listen() failed");
exit(1);
}
printf("Waiting for connections...\n");
// 接受连接
socklen_t client_addr_len = sizeof(client_addr);
conn_sock = accept(listen_sock, (struct sockaddr *)&client_addr, &client_addr_len);
if (conn_sock == -1) {
perror("accept() failed");
exit(1);
}
printf("Connection from %s:%d established\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// 接收数据并发送回复
while ((bytes_read = recv(conn_sock, buf, sizeof(buf), 0)) > 0) {
printf("Received %d bytes from client\n", bytes_read);
send(conn_sock, buf, bytes_read, 0);
}
if (bytes_read == 0) {
printf("Connection closed by client\n");
} else {
perror("recv() failed");
}
close(conn_sock);
close(listen_sock);
return 0;
}
```
UDP服务器代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in server_addr, client_addr;
char buf[1024];
int bytes_read;
// 创建套接字
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
perror("socket() failed");
exit(1);
}
// 设置地址重用
int reuse = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
perror("setsockopt() failed");
exit(1);
}
// 绑定地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind() failed");
exit(1);
}
printf("Listening on port %d...\n", ntohs(server_addr.sin_port));
// 循环处理请求
while (1) {
socklen_t client_addr_len = sizeof(client_addr);
bytes_read = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&client_addr, &client_addr_len);
if (bytes_read == -1) {
perror("recvfrom() failed");
continue;
}
printf("Received %d bytes from %s:%d\n", bytes_read, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
printf("Message: %.*s\n", bytes_read, buf);
// 发送回复
sendto(sock, buf, bytes_read, 0, (struct sockaddr *)&client_addr, client_addr_len);
printf("Reply sent\n");
}
close(sock);
return 0;
}
```
希望这些代码可以帮助到您。
阅读全文