帮我写一个TCP服务端的C语言代码
时间: 2023-09-03 10:26:02 浏览: 99
### 回答1:
好的,这是一个简单的 TCP 服务端代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
#define MAX_CLIENTS 5
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_address;
int opt = 1;
int addrlen = sizeof(server_address);
char buffer[BUFFER_SIZE] = {0};
// 创建 socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置 socket 选项,使服务端可以重新绑定端口
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
// 填充服务端地址信息
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(PORT);
// 绑定 socket 到服务端地址
if (bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听客户端连接请求
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("TCP server listening on port %d\n", PORT);
// 接收客户端连接请求
if ((client_socket = accept(server_socket, (struct sockaddr *) &server_address, (socklen_t *) &addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("Accepted connection from client\n");
// 接收客户端发送的数据
int valread = read(client_socket, buffer, BUFFER_SIZE);
printf("Received message from client
### 回答2:
TCP服务端的C语言代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error opening socket");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 1234;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error on binding");
exit(1);
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
perror("Error on accept");
exit(1);
}
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);
if (n < 0) {
perror("Error reading from socket");
exit(1);
}
printf("Message from client: %s\n", buffer);
n = write(newsockfd, "Server received the message", 26);
if (n < 0) {
perror("Error writing to socket");
exit(1);
}
close(newsockfd);
close(sockfd);
return 0;
}
```
以上是一个简单的TCP服务端的C语言代码,它监听一个特定的端口(在代码中,端口号为1234),接收来自客户端的消息,并将接收到的消息发送回客户端。
### 回答3:
当然,下面是一个简单的TCP服务端的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
char *message = "Hello from server!";
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置套接字选项
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// 绑定套接字到指定的端口
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听连接请求
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// 接受连接请求
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
perror("accept");
exit(EXIT_FAILURE);
}
// 接收客户端发送的数据
valread = read(new_socket, buffer, BUFFER_SIZE);
printf("%s\n", buffer);
// 向客户端发送消息
send(new_socket, message, strlen(message), 0);
printf("Hello message sent\n");
return 0;
}
```
这段代码初始化了一个服务器套接字,并将其绑定到本地的8080端口。它监听连接请求,并当有客户端连接时,接受连接请求。接下来,它从客户端接收数据,并向客户端发送一条消息。在接收到数据后,它会在控制台输出该数据,并在发送消息后打印一条确认消息。
阅读全文