L
L
LiptonOlolo2016-04-06 05:23:10
.NET
LiptonOlolo, 2016-04-06 05:23:10

How to implement the reception and transmission of text through the socket without closing and opening it every time again?

Good morning.
There is something like this code:

public string Send(string Text, bool Read, Form uForm)
        {
            try
            {
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect("localhost", 10000);
                sock.Send(Encoding.UTF8.GetBytes(Text));

                if (Read)
                {
                    byte[] bytes = new byte[2048];
                    int bytesRec = sock.Receive(bytes);
                    sock.Dispose();
                    return Encoding.UTF8.GetString(bytes, 0, bytesRec);
                }
                else
                {
                    sock.Dispose();
                    return null;
                }
            }
            catch
            {
                AlertMessage.Show(uForm, "Серверная часть не доступна.");
                return null;
            }
        }

And as you can see, I can read the response from the server only when I send something to it, I want to maintain a constant connection to it, as well as process responses.
Can anyone suggest how to implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2016-04-06
@Vindicar

Wait, first we will understand the logic of the connection. There are at least two options for working:
A) One-way request-response, or master-slave (what is implemented in the above code). Those. the client (master) sends a request, receives a response from the slave, and either closes the connection or sends another request. This is how HTTP works, for example.
B) random order. Both the client and the server can send a message to the other side at any time, but the other side is not required to respond immediately. This is how IM systems usually work.
For the second option, the logic will be something like this:
1. Establish a connection
2. Wait until there is received data, messages to send, or a stop signal is received.
3. If a stop signal is received, see item 11.
4. Check if there is received data, if not, see item 7.
5. Receive data and parse its format, converting it into messages.
6. Process received messages, for example, generate an event.
7. Check if there are messages to be sent, if not, see item 10.
8. Generate data to be sent.
9. Send data.
10. See point 2
11. Close the connection.
In this case, the data is the string of bytes that is sent through the socket, and the message is the data structures that your program operates on. In the simplest case, it will be the same.
You can implement such logic as follows: you create a separate connection processing thread that performs steps 2-10 in a loop. This thread must provide a method for queuing a message for dispatch, and an event for responding to received messages*.
In this case, your underlying program starts this thread, subscribes to its events, and tugs on its dispatch method as needed. This way you can separate the code for working with the network and the GUI.
* Another service trifle is also useful, such as a command to stop the stream and close the connection, check for errors, and so on. In the simplest program, this can be dispensed with.

K
Konstantin Malyarov, 2016-04-06
@Konstantin18ko

I don't know about C#, but Java has threading. I guess Read is an incoming stream, and it should be looped on while(true), or rather whileRead != 0).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question