C语言实现TCP CS模型代码,客户端服务器
服务端将接受客户端发送的数据并将其转换成大写形式,然后再发送给客户端,客户端将发送一个字符串给服务器,并显示来自服务器的响应内容。
服务器
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int socket_fd, conn_fd;
socklen_t cli_len;
struct sockaddr_in serv_addr, cli_addr;
char buffer[BUFFER_SIZE];
// 创建socket
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// 初始化地址和端口
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
// 绑定地址和端口
if (bind(socket_fd, (const struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
// 监听连接
if (listen(socket_fd, 10) == -1) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Server started at port %d ...\n", PORT);
while(1) {
// 接受连接请求
cli_len = sizeof(cli_addr);
conn_fd = accept(socket_fd, (struct sockaddr*)&cli_addr, &cli_len);
if (conn_fd == -1) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
printf("Accepted client connection from %s:%d\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
while(1) {
// 接受客户端发送的数据
memset(buffer, 0, sizeof(buffer));
if (recv(conn_fd, buffer, sizeof(buffer), 0) == -1) {
perror("Receive failed");
exit(EXIT_FAILURE);
}
// 转换为大写形式
for (int i = 0; i < strlen(buffer); i++) {
buffer[i] = toupper(buffer[i]);
}
// 发送数据回客户端
if (send(conn_fd, buffer, strlen(buffer), 0) == -1) {
perror("Send failed");
exit(EXIT_FAILURE);
}
// 输出客户端发来的数据
printf("Received data from %s:%d: %s\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), buffer);
}
// 关闭连接
close(conn_fd);
}
// 关闭监听
close(socket_fd);
return 0;
}
客户端
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int socket_fd;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE];
// 创建socket
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// 初始化地址和端口
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT);
// 连接到服务器
if (connect(socket_fd, (const struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1) {
perror("Connect failed");
exit(EXIT_FAILURE);
}
printf("Connected to server at %s:%d ...\n", inet_ntoa(serv_addr.sin_addr), ntohs(serv_addr.sin_port));
while(1) {
// 从标准输入读取要发送的数据
memset(buffer, 0, sizeof(buffer));
printf("Please enter data to send to server (type \"exit\" to quit):\n");
fgets(buffer, sizeof(buffer), stdin);
// 退出循环
if (strcmp(buffer, "exit\n") == 0) {
close(socket_fd);
exit(EXIT_SUCCESS);
}
// 发送数据到服务器
if (send(socket_fd, buffer, strlen(buffer), 0) == -1) {
perror("Send failed");
exit(EXIT_FAILURE);
}
// 接受服务器返回的数据
memset(buffer, 0, sizeof(buffer));
if (recv(socket_fd, buffer, sizeof(buffer), 0) == -1) {
perror("Receive failed");
exit(EXIT_FAILURE);
}
// 输出服务器返回的数据
printf("Received data from server: %s\n", buffer);
}
return 0;
}
本文来自博客园,作者:开源侠,转载请注明原文链接:https://www.cnblogs.com/cyj22/p/17914109.html
微信公众号:青衫换酒吃