简体   繁体   English

使用 Winsock 和 dirent 向客户端发送目录列表的问题

[英]Issue using Winsock & dirent to send directory list to client

I'm new to programming, but I'm trying to use C++ to create a TCP server with Winsock which will send a list of all the host's files and directories to the client using dirent.我是编程新手,但我正在尝试使用 C++ 创建一个带有 Winsock 的 TCP 服务器,它将使用 dirent 向客户端发送所有主机文件和目录的列表。 So far the code creates the server, lists all of its directories, and sends the name of only one of them to the client.到目前为止,代码创建了服务器,列出了它的所有目录,并将其中一个目录的名称发送给客户端。 I can't figure out why only one directory name is being sent, despite all of them being listed on the server's computer.我无法弄清楚为什么只发送一个目录名称,尽管它们都列在服务器的计算机上。

The 1st code block creates the socket.第一个代码块创建套接字。 The issue seems to be in the 2nd block问题似乎在第二个街区

#include<io.h>
#include<stdio.h>
#include<winsock2.h>
#include <iostream>
#include <dirent.h>
#include <sys/types.h>
#include <string>
#pragma comment(lib,"ws2_32.lib") 
 using namespace std;
 


int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s , new_socket;
    struct sockaddr_in server , client;
    int c;
    char *message;
 
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
     
    printf("Initialised.\n");
     
    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }
 
    printf("Socket created.\n");
     
    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );
     
    //Bind
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
    }
     
    puts("Bind done");
 
    //Listen to incoming connections
    listen(s , 3);
     
    //Accept and incoming connection
    puts("Waiting for incoming connections...");
     
    c = sizeof(struct sockaddr_in);
    new_socket = accept(s , (struct sockaddr *)&client, &c);
    if (new_socket == INVALID_SOCKET)
    {
        printf("accept failed with error code : %d" , WSAGetLastError());
    }
     
    puts("Connection accepted");

This is what lists & sends the directories.这是列出和发送目录的内容。

//List directory
   DIR *dr;
   struct dirent *en;
   dr = opendir("."); //open all or present directory
   if (dr) {
      while ((en = readdir(dr)) != NULL) {
         printf("%s\n", en->d_name); //print all directory name
      message = ("%s\n", en->d_name); //Problem line?
      }
      closedir(dr); //close all directory
   }

    send(new_socket , message , strlen(message) , 0);
     
    getchar();
 
    closesocket(s);
    WSACleanup();
     
    return 0;
}

I'd really appreciate any help understanding the issue and how to fix it.我真的很感激任何帮助理解这个问题以及如何解决它。

I edited the 2nd block of code shown above, and this ended up working.我编辑了上面显示的第二个代码块,这最终工作了。

    DIR *dr;
   struct dirent *en;
   dr = opendir("."); //open all or present directory
   if (dr) {
      while ((en = readdir(dr)) != NULL) {
         printf("%s\n", en->d_name);
         char buffer[300];
         sprintf(buffer, "%s\n", en->d_name); //saves info to buffer
        send(new_socket, buffer, strlen(buffer), 0); //sends the buffer as a message
      }
      closedir(dr); //close all directory
   }

    send(new_socket , message , strlen(message) , 0);
     
    getchar();
 
    closesocket(s);
    WSACleanup();
     
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM