2013-02-19 108 views
0

服務器:Ç - 客戶端/ JAVA - 服務器 - 客戶端閱讀奇怪的事情

public static void getListOfFiles(String path, DataOutputStream outToClient) throws IOException 
    { 
     // Directory path here 
      //String path = "."; 

      String files; 
     try 
     { 
       File folder = new File(path); 
       File[] listOfFiles = folder.listFiles(); 
       String sendOver = ""; 

       for (int i = 0; i < listOfFiles.length; i++) 
       { 


        files = listOfFiles[i].getAbsolutePath(); 
        sendOver = sendOver + "!" + files; 


       } 
       outToClient.writeBytes(sendOver + "\n"); 
     } 
     catch (Exception e) 
     { 
      outToClient.writeBytes("There was an error with the path, please try again. \n"); 
     } 

    } 

    public static void getDate(DataOutputStream outToClient) throws IOException 
    { 

     outToClient.writeBytes(Calendar.getInstance().getTime().toString() + '\n'); 
    } 

    public static void getUsers(DataOutputStream outToClient) throws IOException 
    { 
     outToClient.writeBytes("User logged in: "+ System.getProperty("user.name") + "\n"); 
    } 


} 

客戶:

#include <sys/socket.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 


int main() 

{ 

    int sock, bytes_recieved; 
    char send_data[1024],recv_data[1024]; 
    struct hostent *host; 
    struct sockaddr_in server_addr; 
    bytes_recieved = 1024; 
    host = gethostbyname("localhost"); 

    sock = socket(AF_INET, SOCK_STREAM,0); 

    server_addr.sin_family = AF_INET; 
    server_addr.sin_port = htons(3324); 
    server_addr.sin_addr = *((struct in_addr *)host->h_addr); 
    bzero(&(server_addr.sin_zero),8); 

    connect(sock, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)); 
    char *temp = '\n'; 

    while(1) 
    { 
     printf("Enter Command...\n"); 
     gets(send_data); 
     strcat(send_data, "\n"); 
     send(sock,send_data,strlen(send_data), 0); 

     if(send_data[0] == 'Q' && send_data[1] == 'U' && send_data[2] == 'I' && send_data[3] == 'T') 
     { 
      printf("Quiting..."); 
      break; 
     } 


     //printf("\nSend Data :"); 

     recv_data[bytes_recieved] = '\0'; 
     bytes_recieved = recv(sock,recv_data,1024,0); 
     //fflush(stdin); 
     printf("\nRecieved data = %s" , recv_data); 
     recv_data[bytes_recieved] = '\0'; 

    } 
} 

從本質上講,服務器端臨危一切正常(我已經調試它),但是客戶不得法正確讀取

下面是我的控制檯上的客戶端的一些例子:

test 

Recieved data = Error wEnter Command...**<---- What The??** 


Recieved data = ith command: TEST_c.dylibEnter Command... **<---- What The??** 


Recieved data = Error with command: dylibEnter Command... **<---- What The??** 


Recieved data = Error with command: Enter Command... **<---- What The??** 

我寫回

outToClient.writeBytes("Error with command: " + capitalizedSentence + "\n"); 

當我得到上面。希望有人在C.更好地熟悉

回答

1

一個錯誤(不知道的是,有更多): 你這樣做:

bytes_recieved = 1024; 
char send_data[1024],recv_data[1024]; 
recv_data[bytes_recieved] = '\0'; // <--- HERE YOU ARE WRITING OUT OF BOUNDS 
bytes_recieved = recv(sock,recv_data,1024,0); 

嘗試:

recv_data[bytes_recieved - 1] = '\0'; 
bytes_recieved = recv(sock,recv_data,1023,0); 

而且,除非所有的消息都是1023字節長。您可能需要在字符串的末尾添加\0,而不是緩衝區的末尾。

最後,你應該看一下閱讀使用手冊。 http://linux.die.net/man/2/recv

有可能您沒有使用您打算使用的標誌。你可以看到一種確定緩衝區中可用字節數的方法:https://stackoverflow.com/a/3054519/828193

希望有幫助。

+0

你是對的,這是一個錯誤。然而,它並沒有解決這個問題..我看看ioctl(...);看起來如果我按兩次Enter鍵,第二次讀取就會得到正確的結果 – Aziz 2013-02-19 21:46:09