我正在为一个任务编写一个小的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...,它在while的末尾。这不应该继续循环我调用了accept并创建了线程,直到那个线程退出吗?
发布于 2016-03-10 21:51:30
您与AF_UNIX套接字的clientAddr类型不匹配:必须为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;
}https://stackoverflow.com/questions/35914726
复制相似问题