编写一个C语言基于TCP协议的网络通信程序,要求服务器通过套接字连接后,要求输入用户名,判断用户名正确后,向客户端发送连接正确的字符串,在服务器显示客户端的IP地址和端口。
时间: 2023-12-31 08:03:28 浏览: 73
好的,我会为您提供一个简单的C语言基于TCP协议的网络通信程序。
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAX_CLIENTS 5
int main(int argc, char *argv[]) {
int server_socket, client_socket, max_sd, activity, i;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
// 创建TCP套接字
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址和端口
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// 绑定套接字到指定地址和端口
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听客户端连接
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d\n", PORT);
// 接受客户端连接并处理请求
while (1) {
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(server_socket, &read_fds);
max_sd = server_socket;
// 添加活动套接字到集合中
for (i = 0; i < MAX_CLIENTS; i++) {
client_socket = client_sockets[i];
if (client_socket > 0) {
FD_SET(client_socket, &read_fds);
}
if (client_socket > max_sd) {
max_sd = client_socket;
}
}
// 等待活动套接字
activity = select(max_sd + 1, &read_fds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
perror("select error");
}
// 如果是新的连接请求,接受连接并添加到客户端套接字数组
if (FD_ISSET(server_socket, &read_fds)) {
int addrlen = sizeof(client_addr);
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, (socklen_t *)&addrlen)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("New client connected from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// 向客户端发送连接成功消息
char *message = "Connection established. Please enter your username:\n";
if (send(client_socket, message, strlen(message), 0) != strlen(message)) {
perror("send failed");
}
// 添加新的套接字到客户端套接字数组
for (i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] == 0) {
client_sockets[i] = client_socket;
break;
}
}
}
// 处理客户端请求
for (i = 0; i < MAX_CLIENTS; i++) {
client_socket = client_sockets[i];
if (FD_ISSET(client_socket, &read_fds)) {
int valread;
if ((valread = read(client_socket, buffer, 1024)) == 0) {
// 客户端关闭连接
printf("Client disconnected from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
close(client_socket);
client_sockets[i] = 0;
} else {
// 处理客户端请求
buffer[valread - 1] = '\0'; // 去掉末尾的换行符
printf("%s:%d - %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buffer);
// 判断用户名是否正确并发送结果
if (strcmp(buffer, "username") == 0) {
char *message = "Username is correct.\n";
if (send(client_socket, message, strlen(message), 0) != strlen(message)) {
perror("send failed");
}
} else {
char *message = "Username is incorrect.\n";
if (send(client_socket, message, strlen(message), 0) != strlen(message)) {
perror("send failed");
}
}
}
}
}
}
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8888
int main(int argc, char *argv[]) {
int server_socket;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建TCP套接字
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址和端口
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
perror("inet_pton failed");
exit(EXIT_FAILURE);
}
// 连接服务器
if (connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}
// 接收连接成功消息并输入用户名
int valread;
if ((valread = read(server_socket, buffer, 1024)) > 0) {
printf("%s", buffer);
fgets(buffer, 1024, stdin);
if (send(server_socket, buffer, strlen(buffer), 0) != strlen(buffer)) {
perror("send failed");
}
}
// 接收用户名判断结果并输出
if ((valread = read(server_socket, buffer, 1024)) > 0) {
printf("%s", buffer);
}
close(server_socket);
return 0;
}
```
运行服务器端程序后,运行客户端程序并输入用户名,即可看到服务器端输出客户端的IP地址和端口,并发送用户名判断结果。
阅读全文