在使用 C 编程语言的项目中,您的客户端-服务器通信面临挑战。尽管您尝试通过各种方法解决问题,但您未能成功建立客户端和服务器应用程序之间的连接 检查了所有这些但没有工作“网络连接:确认客户端和服务器都连接到同一网络并且可以相互访问是至关重要的。您可以通过检查它们的 IP 地址并确保它们属于同一网络来做到这一点子网。此外,您可以尝试从客户端计算机 ping 服务器的 IP 地址,以确保它们可以在基本级别上相互通信。
端口设置:仔细检查客户端和服务器是否使用相同的端口号进行通信。如果您安装了防火墙,您可能需要打开您的应用程序正在使用的端口,以允许在两台机器之间传输数据。
套接字编程错误:确保您已在客户端和服务器应用程序的 C 代码中正确实施套接字编程原则。这包括创建套接字,将它们绑定到地址和端口,以及设置必要的通信协议。
错误处理:在您的 C 代码中实施适当的错误处理,以识别您的应用程序执行期间可能发生的任何问题。这可以帮助您查明可能阻止客户端和服务器有效通信的特定问题。
同步:如果您的客户端和服务器应用程序使用多线程,请确保您有适当的同步机制以防止任何可能干扰通信过程的竞争条件或死锁。” 客户代码如下:
// Client program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8080
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, n, user_id, group_id;
struct sockaddr_in serv_addr;
struct hostent *server;
if (argc < 5)
{
fprintf(stderr, "Usage: %s <user_id> <group_id> <file_name> <file_path>\n", argv[0]);
exit(1);
}
user_id = atoi(argv[1]);
group_id = atoi(argv[2]);
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
// Set up the server address structure
server = gethostbyname(SERVER_IP);
if (server == NULL)
{
fprintf(stderr, "ERROR, no such host\n");
exit(1);
}
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr_list, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(SERVER_PORT);
// Connect to the server
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
// Send user ID and group ID to the server
n = write(sockfd, &user_id, sizeof(user_id));
if (n < 0)
error("ERROR writing user ID to socket");
n = write(sockfd, &group_id, sizeof(group_id));
if (n < 0)
error("ERROR writing group ID to socket");
// Send the file name to the server
n = write(sockfd, argv[3], strlen(argv[3]));
if (n < 0)
error("ERROR writing file name to socket");
// Open the file for reading
int fd = open(argv[4], O_RDONLY);
if (fd < 0)
error("ERROR opening file");
// Read the file data and send it to the server
char buffer[256];
while ((n = read(fd, buffer, sizeof(buffer))) > 0)
{
if (write(sockfd, buffer, n) != n)
error("ERROR writing file data to socket");
}
if (n < 0)
error("ERROR reading file");
// Close the file
close(fd);
// Read the server's response
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n", buffer);
// Close the socket
close(sockfd);
return 0;
}
服务器代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8080
// Define constants for the directories
#define MANUFACTURING_DIR "Manufacturing/"
#define DISTRIBUTION_DIR "Distribution/"
#define MANUFACTURING_USER_ID 1001
#define DISTRIBUTION_USER_ID 1002
#define MANUFACTURING_GROUP_ID 2001
#define DISTRIBUTION_GROUP_ID 2002
void error(const char *msg)
{
perror(msg);
exit(1);
}
void *client_handler(void *client_sockfd)
{
printf("Handeling Client");
int sockfd = *((int *)client_sockfd);
char buffer[256];
char file_name[256];
int n, user_id, group_id;
// Read user ID and group ID from the client
n = read(sockfd, &user_id, sizeof(user_id));
if (n < 0)
error("ERROR reading user ID from socket");
n = read(sockfd, &group_id, sizeof(group_id));
if (n < 0)
error("ERROR reading group ID from socket");
// Set the real and effective user and group IDs for the server process
if (setregid(group_id, group_id) < 0)
error("ERROR setting real and effective group IDs");
if (setreuid(user_id, user_id) < 0)
error("ERROR setting real and effective user IDs");
// Read the file name from the client
bzero(file_name, 256);
n = read(sockfd, file_name, 255);
if (n < 0)
error("ERROR reading file name from socket");
// Determine the directory based on the group ID
char directory[256];
if (group_id == MANUFACTURING_GROUP_ID)
{
strcpy(directory, MANUFACTURING_DIR);
}
else if (group_id == DISTRIBUTION_GROUP_ID)
{
strcpy(directory, DISTRIBUTION_DIR);
}
else
{
error("Invalid group ID");
}
// Create the full file path
char file_path[512];
snprintf(file_path, sizeof(file_path), "%s%s", directory, file_name);
// Open the file for writing
int fd = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
error("ERROR opening file");
// Read the file data from the client and write it to the file
while ((n = read(sockfd, buffer, sizeof(buffer))) > 0)
{
if (write(fd, buffer, n) != n)
error("ERROR writing to file");
}
if (n < 0)
error("ERROR reading file data from socket");
// Change the file's owner to the user ID
if (fchown(fd, user_id, -1) < 0)
error("ERROR changing file owner");
// Close the file
close(fd);
// Send a success message to the client
n = write(sockfd, "File transfer successful", 24);
if (n < 0)
error("ERROR writing to socket");
// Reset the real and effective user and group IDs to root (or another privileged user) before exiting
if (setregid(0, 0) < 0)
error("ERROR resetting real and effective group IDs");
if (setreuid(0, 0) < 0)
error("ERROR resetting real and effective user IDs");
// Close the client socket and exit the thread
close(sockfd);
pthread_exit(NULL);
}
int main()
{
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
// Clear the server address structure
bzero((char *)&serv_addr, sizeof(serv_addr));
portno = SERVER_PORT;
// Set up the server address structure
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
serv_addr.sin_port = htons(portno);
// Bind the socket to the server address
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
// Listen for incoming client connections
listen(sockfd, 5);
clilen = sizeof(cli_addr);
while (1)
{
printf("Creating Thread");
// Accept incoming client connections
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
// Create a new thread to handle the client connection
pthread_t client_thread;
printf("Creating Thread");
int thread_status = pthread_create(&client_thread, NULL, client_handler, (void *)&newsockfd);
if (thread_status != 0)
{
fprintf(stderr, "Error creating thread: %d\n", thread_status);
exit(1);
}
// Detach the thread to automatically release resources when the thread exits
pthread_detach(client_thread);
}
// Close the server socket
close(sockfd);
return 0;
}