任何人都可以看到为什么我的日志文件在这里一直空着?

时间:2016-03-10 13:06:46

标签: c multithreading sockets

我正在编写一些代码来为分配创建一个小型UDS服务器。客户端将与承载某些消息的服务器建立特定的一系列连接。基本上,如果这些消息按顺序出现在我们的日志文件中,那么我们都已设置好。但是每次运行时我的日志文件都会显示为空白,我不确定为什么会这样。

这是我的代码:

Sub CopyTable()

Dim colNo, lastRow As Integer
Dim ws1, ws3 As Worksheet

Set ws1 = Sheets("Sheet1") ' assign the worksheet variables
Set ws3 = Sheets("Sheet3")

colNo = Application.Match(ws1.Range("T1"), ws1.Range("A1:R1")) ' work out which column the required table starts in

lastRow = ws1.Cells(10000, colNo).End(xlUp).Row ' work out the last row of the table

ws1.Range(ws1.Cells(1, colNo), ws1.Cells(lastRow, colNo + 4)).Copy ws3.Range("A1") ' copy the table

End Sub

对日志的写入发生在顶部函数// forward declarations int error_msg( char * msg ); int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals FILE * log_fd; // opened by main() but accessible by each thread typedef struct sockaddr SA; 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; } 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 socklen_t clientLength = sizeof(struct sockaddr_un); struct sockaddr_un clientAddr; clientAddr.sun_family = AF_UNIX; strcpy(clientAddr.sun_path, argv[2]); pthread_t tid; int listenfd = socket(AF_UNIX, SOCK_STREAM, 0); // 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); printf("%d",errno); return 0; } // when the loop ends, close the listening socket close(listenfd); // close the log file fclose(log_fd); return 0; } 中。我对C很新,而且我经常会因为文件处理方式的具体问题而被绊倒,或者认为存储器阵列太高了,就像那样。鉴于我的代码,我真的不明白为什么没有应该写。我甚至试图通过在客户端文件描述符上调用recv_log_msgs来正确地“读取”数据吗?我正确使用缓冲区吗?

1 个答案:

答案 0 :(得分:0)

if(buffer[currentPos - 1] == '\n')无效。是否找到换行符取决于您获得的块大小。

循环永不中断。您需要在收到的块中搜索所有字符。现在,这只是检查一个char,以及任意选择的一个char。