我正在为一个作业编写一个小型UDS服务器,并且觉得我的代码主要在那里,但是当我真正尝试运行它时,我得到的结果绝对没有。
代码由shell脚本测试,该脚本向我的代码的main函数发送几个不同的测试调用。传递给main的唯一内容是log
文件名和UDS path
,然后不同的客户端连接到服务器,这应该只是导致特定的输出日志来检查服务器代码的正确性。
我的主要功能是:
int main( int argc, char * argv[] )
{
if ( argc != 3 )
return usage( argv[0] );
log_fd = fopen(argv[1], "a");
// create a server socket
// domain (i.e., family) is AF_UNIX
// type is SOCK_STREAM
int listenfd = socket(AF_UNIX, SOCK_STREAM, 0);
socklen_t clientLength = sizeof(struct sockaddr_un);
struct sockaddr_un clientAddr;
clientAddr.sa_family = AF_UNIX;
strcpy(clientAddr.sun_path, argv[2]);
pthread_t tid;
// unlink the UDS path)
unlink(argv[2]);
// bind the server socket
bind(listenfd, (SA *)&clientAddr, clientLength);
// listen
listen(listenfd, 1024);
// loop to wait for connections;
// as each connection is accepted,
// launch a new thread that calls
// recv_log_msgs(), which receives
// messages and writes them to the log file
while(1){
printf( "Waiting for a connection on UDS path %s...\n", argv[2] );
int * clientfdp = malloc(sizeof(int));
*clientfdp = accept(listenfd, (SA *) &clientAddr, &clientLength);
pthread_create(&tid, NULL, recv_log_msgs, clientfdp);
}
// when the loop ends, close the listening socket
close(listenfd);
// close the log file
fclose(log_fd);
return 0;
}
其中使用情况为myloggerd <log-file-name> <UDS path>
。
此主函数侦听任何客户端连接,当接受一个客户端连接时,它会在名为recv_log_msgs
的线程例程函数中创建一个新线程。我的代码在这里:
void * recv_log_msgs( void * arg ) //Thread Routine
{
// loops to receive messages from a client;
// when the connection is closed by the client,
// close the socket
int clientfd = *((int *)arg);
char buffer[1500];
memset(buffer, 0, 1500);
int currentPos = 0;
int bytesRec;
int recvng = 1;
while(recvng){
bytesRec = recv(clientfd, buffer, 1500-currentPos, 0);
currentPos += bytesRec;
if(buffer[currentPos - 1] == '\n')
recvng = 0;
}
fprintf(log_fd, "LOGGER %d %s", clientfd, buffer);
close(clientfd);
return NULL;
}
因此,每次线程进入此函数时,数据都会写入日志文件。如果日志文件结果正确,那么我得到100%。如果没有,那我就失败了。
截至目前,当我测试我的解决方案时,根本没有任何事情发生。
我得到一个无限循环的打印件,表示Waiting for connection on the UDS path...
,它位于main的末尾while
内。这不应该继续循环我已经调用accept
并创建了线程,直到该线程退出?
答案 0 :(得分:0)
clientAddr
套接字的AF_UNIX
类型不匹配:必须为struct sockaddr
这对我有用:
int main( int argc, char * argv[] )
{
if ( argc != 3 )
return -1;
// log_fd = fopen(argv[1], "a");
// create a server socket
// domain (i.e., family) is AF_UNIX
// type is SOCK_STREAM
int listenfd = socket(AF_UNIX, SOCK_STREAM, 0);
socklen_t clientLength = sizeof(struct sockaddr_un);
struct sockaddr clientAddr;
clientAddr.sa_family = AF_UNIX;
strcpy(clientAddr.sa_data, argv[2]);
pthread_t tid;
// unlink the UDS path)
unlink(argv[2]);
// bind the server socket
bind(listenfd, (struct sockaddr*)&clientAddr, clientLength);
// listen
listen(listenfd, 1024);
// loop to wait for connections;
// as each connection is accepted,
// launch a new thread that calls
// recv_log_msgs(), which receives
// messages and writes them to the log file
while(1){
printf( "Waiting for a connection on UDS path %s...\n", argv[2] );
int * clientfdp = malloc(sizeof(int));
*clientfdp = accept(listenfd, (struct sockaddr *) &clientAddr, &clientLength);
pthread_create(&tid, NULL, recv_log_msgs, clientfdp);
free(clientfdp);
}
// when the loop ends, close the listening socket
close(listenfd);
// close the log file
// fclose(log_fd);
return 0;
}