file - Server not able to properly read/open a filename sent by client in C -


I am interacting with client / server in Seek. What I'm trying to do, the client request is to read the file on the server, the server returns a buffer of the file content, and the client prints the file.

However I was able to send file buffer to client for the server & amp; To successfully print the client, I think the server can not get the file name sent successfully by the client.

Here's what I mean:

  /// This is the composition of the message that sends the forward and forward text message {int code; // readfile or writefile indicates just 1 or 2 for the int size; Four buffer [256]; // This file will be named when the customer sends the readfile request};  

Works:

  four * filename = "test.c"; Infile = Open (file name, O_RDONLY); // file and send everything back to the customer  

does not work:

  while (read (sockfd, & Amp; msg, sizeof (int) * 2)> 0) {if (msg.code == 1) {// a read operation FFFA file, filesystem, rr; Integer length; Four outputs [256]; Size_t RB = 0; While (rb  

I have edited my show to show the current status, and still is not working. Before that, I had a strakpy that was improperly kept. filename

In the code you posted, Not started Your compiler warned you about this. If it does not happen, then you are not applying it correctly; With GCC , do at least GCC-O-Wall .

If you use strcpy, you must allocate memory for the file name . This step is not necessary in a simple program; It becomes useful by making a copy if you remember reading the file name after going through the client. The strdup function combines memory allocation with a string copy, it is appropriate.

You must check the return value of all system calls. Return value of read tells you how many bytes were read. In order to read , call (FD, Buff, N) is allowed to return less than n bytes if the OS feels like this if you have less bytes than expected If you receive, call read in the loop Yes, many programs that call read are called in a loop, this is a basic Unix / POSIX idiom The Fred function does for you, if you can fit it. The code Msg.size is missing for validity of

msg.code and . Since you've assigned 256 bytes to msg.buffer , you have to limit msg.size to 255.

Yes, assignment msg.buffer [msg.size] = '\ 0' is required, because open to '\ 0' Characters at the end of the name are required


I thought maybe strcpy was appropriate

whenever you were around the indicator (Which is the highest time in C), you need to think carefully what you are doing and where are you pointing out, and whether there is enough space that you want to do. C is an apology; Throwing signals are dangerous as throwing arrows. Drag the picture! C Programmers are of two types: those that are underlined on the wildboard or painted on the picture in other medium; And who draw in their head (the third type is still trying to figure out why to print their program, why is 1 + 1 printing 3 ).


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -