AsyncTcpClient | asynchronous variant of TcpClient and TcpListener for .NET | TCP library
kandi X-RAY | AsyncTcpClient Summary
kandi X-RAY | AsyncTcpClient Summary
Building asynchronous solutions with TcpClient and TcpListener is complicated and it is easy to introduce bugs or miss critical features. These classes provide an easy solution for this task. Writing asynchronous TCP/IP clients and servers with these classes only requires the implementation of very basic callbacks. Alternatively, you can implement your connection logic in derived classes. In any case, you just have to read the received data from a buffer and send data back. All socket and stream interaction is hidden from you. This also includes an asynchronous byte buffer that keeps all received bytes as they come in. Applications can dequeue as many bytes as they need without discarding what else was received. Dequeueing returns up to as many bytes as are available when called synchronously, or exactly the requested number of bytes when called asynchronously. The async method can be cancelled. This ensures that the code will never block irrecoverably. A complete example of both implementation styles is provided in the application in this repository. Start reading at Program.cs.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of AsyncTcpClient
AsyncTcpClient Key Features
AsyncTcpClient Examples and Code Snippets
Community Discussions
Trending Discussions on AsyncTcpClient
QUESTION
when I build it, and running server and then run client, that appear a error
error code = 2
, error message = End of file
when I code synchronous tcp server it's work ok;
thanks
full client code
...ANSWER
Answered 2022-Mar-23 at 02:22The server closes the connection after sending the (empty) response. That leads to EOF on the client, naturally. Just handle it.
There's loads of code smells
delete this;
is an abomination, just makeService
shared_from_this
.No need to use shared_ptrs other than that
When you use smart pointers, use them. Don't "convert to raw pointer" just to dereference (so
*m_socket
instead of*m_socket.get()
).In fact, there should be no need to use
new
,delete
orget()
in your codeYou are accessing the
m_request
immediately afterasync_read_until
which is too early,- it is a data race (so Undefined Behaviour)
- it doesn't get the request, because
async_read_until
didn't complete yet.
So move that code into
onRequestReceived
at a minimumIt's pretty unnecessary to use an
istream
to read the line from the request when you already havebytes_transferred
. I'd suggest
QUESTION
I am trying to build a simple messenger. Currently I have written client and server apps that support "mail like functional", that is they lack chat interaction that you have in every instant messenger.
Here is a model I use.Server: The server for every connected client has a dedicated Service
class that provides actual service. An instance of the Service
class has an id.
Client: At particular moment simultaneously starts reading messages from and writing messages to the associated Service
instance.
Tracker: Records current sessions of users by saving their logins and Service
ids in a map. Also records opened chats by saving key-value pairs (chat participant id 1, chat participant id 2). I use logins and ids of users interchangeably because I have a database.
- A user is trying to log in. The server dedicates to the user the
Service
instance with id 1. Then the user identified as Bob. - Bob opens a chat with Ann.
Tracker
records that Bob usesService
1 and that Bob opened the chat with Ann. - A user is trying to log in. The server dedicates to the user the
Service
instance with id 2. Then the user identified as Ann. - Ann opens a chat with Bob.
Tracker
records that Ann usesService
2 and that Ann opened the chat with Bob. - Ann writes a message to Bob.
Service
2 receives the message and asksService
1 to send the message to Bob's chat if Bob has opened the chat with Ann. For that purpose I useTracker
. In our case Bob is in the chat so Bob's client app should read the message fromService
1. OtherwiseService
2 only stores the new message in the database.
When a user opens a chat with somebody the client app simultaneously starts reading and writing messages to the associate Service
instance.
- Bob opens a chat with Ann. Ann opens a chat with Bob.
- Ann sends messages. They are displayed in Bobs chat.
- Bob sends a message. It is not displayed in Ann's chat. Moreover, further Ann's messages are no longer displayed in Bob's chat.
Here is a portion of my server code. I have added some context but you probably want to look at Service::onMessageReceived
, Service::receive_message
, Service::send_to_chat
ANSWER
Answered 2021-Jan-31 at 14:26There's too much code and too little. Too much for the question, and too little to actually suggest improvements. I see an overuse of shared_ptr, threads, In particular it is very weird to run async-operations on their own threads. Let alone detached:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install AsyncTcpClient
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page