Hello! Help in this matter. How can I put in the same message both the path of the file I am looking for and the size of the block to separate it into 2 variables on the server? the path of the file being searched for and the size of the bol is read from texbox2 and textbox3

// А это отпрвка этого сообщения на сервер send(my_socket, buff, (n-1), 0); 
  • 3
    Is it possible to pass str directly to a function? - skegg
  • And what a mistake? What is the actual question, what exactly does not come out? - VladD
  • I support, @mikillskegg, is it true, what's stopping you? on server: std :: vector <char> Buffer; SIZE_T len; // len = strlen (msg); Buffer.resize (sizeof (SIZE_T) + len); :: mmemove (& Buffer.at (sizeof (SIZE_T)), msg, len); (SIZE_T ) & Buffer.front () = block_size; send (my_socket, & * Buffer.begin (), sizeof (SIZE_T) + len, 0); on the client: block_size = (SIZE_T ) pRecvBuffer sprintf (MsgBuffer, "%. * s", int (RecvSize - sizeof (SIZE_T)), ((SIZE_T *) pRecvBuffer) + 1); But since this is already a package, it is therefore necessary to ensure that its size is not less than sizeof( SIZE_T ) . - mega

2 answers 2

Try this:

 std::string addr = marshal_as<std::string>(textBox2->Text); send(my_socket, addr.c_str(), addr.length(), 0); 

In general, keeping the UI control code and network operations in one file (not to mention the fact that in one method) is not comme il faut. Divide everything by 2, and better - 3 levels: UI, responsible for text boxes and all that (this is usually called View, UI Layer or Presentation); network operations responsible for sockets and everything around them (this is your Model); well, the logic of the program that binds these two parts together and makes it work (called the loud name Business Logic). Good?

  • @VladD, why do we need an intermediate std :: string? - avp
  • @avp: for auto-clear memory. if there was a char* , it is impossible to free it to simply delete it - the memory is allocated to the figs it knows where, not necessarily on heap. Here are some details: codeproject.com/Articles/19579/… - VladD
  • @VladD, but why is it cleaned at all? I am completely new to M $, but IMHO should be a way to get the address of the beginning of an array of characters in textBox2-> Text. Probably some kind of marshal_context should be used for this. - avp
  • one
    @avp: not so simple! Objects in .net have the right to move in memory. For example, garbage collection is often accompanied by memory compaction, that is, the movement of objects. So you need to either pin'it or copy. Moreover, the layout of strings in .NET is different: embedded zeros are allowed. - VladD
  • one
    @avp: marshal_context is exactly what is needed to free up memory. I did not use it, because the code is more complicated. - VladD

Google buffers, boost serialize, xml, json are a lot of options in fact. That is, in fact, you just have to figure out how to store your structure of two fields in a row (as the simplest) and just send / receive this string over the network. Well and parser / parser write for this.

Client:

 std::string sFileName = "..."; int nBlockSize = ...; std::stringstream output; output << sFilename <<" "<<nBlockSize; std::string sToSend = output.str(); send(my_socket, sToSend.c_str(), sToSend.size(), 0); 

Server:

 std::string sReceived(buffer,received_len); //buffer содержит то что мы прочитали из соккета. received_len - сколько прочитали из сокета). std::stringstream input(sReceive); std::string sFileName; int nBlockSize = 0; input >> sFileName; input >> nBlockSize; 

Something like this. I am writing very roughly now, but the meaning is approximately clear, I hope.

  • There are a lot of words instead of one sprintf () on the client (by the way, we immediately get the size of the data sent) and sscanf () on the server. In fact, of course, we must bear in mind that there may be spaces in the file name. The format can be made apparently like this: block size file-name-ending-zero The entry remains the same, to read in the server, we call strtol (), and then strcpy () in the variable for the file name, although you can immediately fopen () from the buffer, 1 byte after the end of strtol (). -- One more thing. One recv () on the server in the "bad" network may not work ... (as always, there is not enough space in the comments) - avp
  • 1) Format is the details. In short: sprintf is unsafe. Well, in more detail about sprintf vs snprintf vs strstream vs stringstream can be read from Sutter in Exceptional C ++ Style (it seems tasks 2 and 3, now it is not at hand). 1a) How good is it that in C ++ there are threads and there is no need to use any kind of strtol and other strcpy. Life immediately seems easier;) 2) The fact that recv should be in a loop is understandable. It's not even a bad or good network. But I deliberately left it behind the brackets. - Andrey Buran
  • @Andrey Buran, 1) in this case, the format (the order of the file name and block size) is not details, but a way to reduce the complexity of the program reading the file name with spaces, etc. characters. 2) There are also streams in memory on C (open_memstream (), etc.), but in this case it does not simplify the program. 3) snprintf vs sprintf - in fact, snprintf does not save , you really should always check the consistency of the input data and the size of buffers. Let me explain: with the snprintf, the error is simply "driven deep into", the program does not crash, but the whole complex does not work. What's better? Boost question. (again there is little space ...) - avp
  • 1) The main thing is to maintain a balance between the complexity of the program and the complexity of its perception by the programmer. But in principle, of course, I do not mind. 2) Well, we're still talking about the pros, if I'm not mistaken. Yes, and using these streams are still more difficult than any strtol `s 3) Yes, and just about that you can read in these chapters of the book I mentioned. There everything is very versed in detail. I recommend (to everyone, not only to you). - Andrey Buran
  • @Andrey Buran, 1) the main thing in this matter is the KISS principle (you just need not to forget - but not easier than necessary ). 2) and yet, if the C ++ compiler compiles - then the program is cross? 3) not familiar. And what (from the programs) did Sutter write? - avp