Reputation: 1614
Server:
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");
}
}
Client:
#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';
}
}
Essentially, the server side recieves everything correctly (I have debugged it), however the client must not be reading correctly-
Here are some examples from my console on the client side:
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??**
I am writing back
outToClient.writeBytes("Error with command: " + capitalizedSentence + "\n" );
When I get the above. Hopefully someone is better versed in C.
Upvotes: 0
Views: 206
Reputation: 33317
One bug (not sure is there are more): You do:
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);
Try:
recv_data[bytes_recieved - 1] = '\0';
bytes_recieved = recv(sock,recv_data,1023,0);
Also, unless all messages are 1023 bytes long. You may have to add the \0
at the end of the string, instead of the end of buffer.
Finally, you should have a look at the manual for the usage of read. http://linux.die.net/man/2/recv.
It is possible, that you are not using the flag you intend to use. You can see a way of determining the number of bytes available in the buffer here: https://stackoverflow.com/a/3054519/828193
Hope that helps.
Upvotes: 1