0

I found these 2 working code example to receive data from android:

where seems that is suggested use two separated sockets in each side (2 on sender and 2 on receptor). One to handle text data and other to handle image data respectively.

I also found this C# code (see Client code), where after an adaptation i was able to receive text and image using only 1 socket:

var _strdata = String.Empty;

// ...

// then on 2º while loop i adapted to also receive text

if (read <= 0)
    break;
bytesRead += read;
_strdata += Encoding.UTF8.GetString(_bindata, 0, read);

// and after

if (_strdata.Contains("<|data|>"))
    receivedMessage(client, _strdata); // text
else if (_strdata.Contains("<|screen|>"))
    receivedImage(client, _bindata); // image

Then i ask: How i can have a hybrid Delphi code to receive text and image with only one socket connection?

FLASHCODER
  • 1
  • 7
  • 24
  • You already know the answer - simply delimit the socket data so that you know where text ends and image begins, and vice versa, similar to what you have shown. What is the actual problem you are having with that? Be specific. Just make sure you are buffering the socket data, since sends and reads are not 1:1 in TCP. And don't treat image data as strings. – Remy Lebeau Nov 18 '22 at 20:09
  • @RemyLebeau, then the code to image (that's more near of C# code), also is able to receive text? for example `Buffer: TMemoryStream;`, i must get text from **`Buffer`** in `TSock_Thread.ClientExecute`, after both `while` loops, right? – FLASHCODER Nov 18 '22 at 20:41
  • Perform a read on the socket, put any raw bytes received in the buffer, scan the buffer for the next delimiter, if found then extract bytes from the buffer up to that delimiter and process those bytes as needed, then repeat until disconnected. – Remy Lebeau Nov 18 '22 at 21:13
  • @RemyLebeau, could show a code based on C# example linked above and following my logic please? > `if (_strdata.Contains("<|data|>")) receivedMessage(client, _strdata); // text else if (_strdata.Contains("<|screen|>")) receivedImage(client, _bindata); // image` – FLASHCODER Nov 18 '22 at 22:31
  • First, the C# code shown is not well-suited to this task. And second, StackOverflow is not a code writing service. We are here to answer technical questions, not write whole programs for people. If are you having problems with your code, please [edit] your question to show that code, and explain the problems. And BTW, your assumption that the other examples are using "*two separated sockets in each side (2 on sender and 2 on receptor)*" is wrong, neither example is doing that. – Remy Lebeau Nov 19 '22 at 00:08
  • 1
    That being said, is there a reason why you are still using the legacy and *deprecated* `TServerSocket` to begin with? Why not use a modern socket library, like Indy (which is pre-installed in Delphi), or ICS, etc? This task would be so much easier to write using one of them. Especially Indy, which provides methods for reading/sending all kinds of different data types (yes, I'm a little biased towards Indy, since I work on it). – Remy Lebeau Nov 19 '22 at 00:12
  • @RemyLebeau, then if i understood, after both `while` loops is need convert [**`Buffer`** to string](https://stackoverflow.com/questions/732666/converting-tmemorystream-to-string-in-delphi-2009) to know type (same logic that i made on C# code above). Otherwise delphi code on linked question is restrict to handle only 1 type of data (like i imagined). – FLASHCODER Nov 19 '22 at 12:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249745/discussion-between-remy-lebeau-and-coringa). – Remy Lebeau Nov 19 '22 at 16:44

0 Answers0