tcpclient | 基于多线程的非阻塞 socket 编程 | Socket library
kandi X-RAY | tcpclient Summary
kandi X-RAY | tcpclient Summary
基于多线程的非阻塞 socket 编程
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 tcpclient
tcpclient Key Features
tcpclient Examples and Code Snippets
Community Discussions
Trending Discussions on tcpclient
QUESTION
I am experimenting with Proxies in java networking. I have read the documentation regarding them and am currently testing ProxySelector.
I notice 2 types of behaviour of this ProxySelector when using with HttpURLConnection & when using with Socket class
When using with HttpUrlConnection with this code
...ANSWER
Answered 2022-Mar-20 at 02:23This seems to be a bug in the JDK: JDK-7141231
Despite java.net.SocksSocketImpl
in theory supporting proxy failover; in reality this does apparently not work because after the first failed connection attempt the socket is closed, but that same closed socket is used for any subsequent connection attemps, which therefore fail with "Socket closed" (which you are seeing).
The reason why changing the proxy type to HTTP "works" is because it performs a direct connection, ignoring all other specified proxies.
QUESTION
I am not receiving the Content posted by the HTTPClient but I can read the other information such as Content-Length and other headers. Let me explain the code here:
Here is the Server Code :
...ANSWER
Answered 2022-Feb-22 at 09:36try this
QUESTION
I have a .NET5.0 standalone service (which is installed as a windows service) which starts a basic SignalR hub. It uses Http.Sys internally has some other API controllers, and I'm currently connecting to it from a WinForms client using HubConnection.
It works perfectly on my development machine, but when I move it into an Azure VM (server 2019 datacentre) the first WebSocket call from the client hangs for a while, then fails. It then seems to "work" from then on, but very, very slowly (too slow to use).
I have opened the appropriate ports incoming and outgoing on the Azure server, and in Azure portal.
If I run my Winforms client on a different azure VM in the same virtual network, it all works fine. Therefore, I am sure the server works correctly, it only fails when I come in over the internet.
The server logfile shows this when I try to connect externally:
2021-11-24 17:10:19.6884|1|DEBUG|Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher|Received hub invocation: InvocationMessage { InvocationId: "1", Target: "GetAllState", Arguments: [ ], StreamIds: [ ] }. 17:10:31.1564|4|DEBUG|Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport|Waiting for the application to finish sending data. 2021-11-24 17:10:31.1564|12|DEBUG|Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport|Error writing frame. System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Net.WebSockets.ManagedWebSocket.SendFrameFallbackAsync(MessageOpcode opcode, Boolean endOfMessage, ReadOnlyMemory
1 payloadBuffer, CancellationToken cancellationToken) at System.Net.WebSockets.WebSocketExtensions.SendMultiSegmentAsync(WebSocket webSocket, ReadOnlySequence
1 buffer, WebSocketMessageType webSocketMessageType, CancellationToken cancellationToken) at Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsServerTransport.StartSending(WebSocket socket) 2021-11-24 17:10:31.1564|2|DEBUG|Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport|Socket closed.The client shows this:
2021-11-24 18:55:43.4679|50|ERROR|Microsoft.AspNetCore.SignalR.Client.HubConnection|The server connection was terminated with an error. System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.. ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
I have looked at so many articles trying to fix this but I can't find anything relevant. It must be config-related, and it must be at "azure portal" level because the service works across the LAN. Does anyone have any idea what I need to do to get this running?
Update:
I created a couple of console apps using TcpListener and TcpClient on a new custom port, ran the server app on my Azure VM, connected to it successfully across the internet, and passed messages between the two. Very confused as to why the WebSocket fails to work over the internet.
Update2:
I changed the service to use Kestrel and it now works as expected. So, for whatever reason it refuses to work over the internet using Http.Sys. I was only using Http.Sys to have basic auth for some other clients that won't be using this service so Kestrel will work fine.
...ANSWER
Answered 2022-Jan-11 at 17:00I changed the service to use Kestrel and it now works as expected. So, for whatever reason it refuses to work over the internet using Http.Sys.
QUESTION
I want my TCP client to connect to multiple servers(each server has a separate IP and port).
I am using async_connect. I can successfully connect to different servers but the read/write fails since the server's corresponding tcp::socket object is not available.
Can you please suggest how I could store each server's socket in some data structure? I tried saving the IP, socket to a std::map, but the first server's socket object is not available in memory and the app crashes.
I tried making the socket static, but it does not help either.
Please help me!!
Also, I hope I am logically correct in making a single TCP client connect to 2 different servers. I am sharing below the simplified header & cpp file.
...ANSWER
Answered 2021-Dec-14 at 12:00You seem to know your problem: the socket object is unavailable. That's 100% by choice. You chose to make it static, of course there will be only one instance.
Also, I hope I am logically correct in making a single TCP client connect to 2 different servers.
It sounds wrong to me. You can redefine "client" to mean something having multiple TCP connections. In that case at the very minimum you expect a container of tcp::socket
objects to hold those (or, you know, a Connection
object that contains the tcp::socket
.
For fun and glory, here's what I think you should be looking for.
Notes:
- no more new, delete
- no more void*, reinterpret casts (!!!)
- less manual buffer sizing/handling
- no more
bind
- buffer lifetimes are guaranteed for the corresponding async operations
- message queues per connection
- connections are on a strand for proper synchronized access to shared state in multi-threading environments
- I added in a connection max idle time timeout; it also limits the time taken for any async operation (connect/write). I assumed you wanted something like this because (a) it's common (b) there was an unused
deadline_timer
in your question code
Note the technique of using shared pointers to have Comm
manage its own lifetime. Note also that _socket
and _outbox
are owned by the individual Comm
instance.
QUESTION
In my Code does my Function OnPropertyChanged
not update the UI, despite the Fact that it does work everywhere else in my Programm. I try to upgrade the Content of the Button, so that if the User presses it, it will change it's Content. It does Update the value
but then won't show it in the UI for some weird Reason. Any help is appreciated...
The View:
ANSWER
Answered 2021-Nov-23 at 14:57The CreateMPViewModel
class must inherit from INotifyPropertyChanged
.
QUESTION
I'm working on a peer-to-peer file sharing application that can be used to transfer files without having a central server.
It works like a charm when it comes to one-to-one file transfer, however I originally planned it in a way, that the sender can send files to multiple clients.
The app uses TCP Hole Punching so port forwarding is not necessary. The problem is, that in order to make TCP hole punch work, I need to specify a local port for each TCPClient. Thus, I need to bind the sockets.
...ANSWER
Answered 2021-Nov-15 at 18:20Okay, I've found a solution! For those who might stuck with the same problem, apparently there is an option to tell the TCP socket that the local endpoint can be reused.
Depending on the OS and the TCP implementation, you can add the option SO_REUSEADDRESS
and SO_REUSEPORT
for the socket. This will make the socket ignore endpoint duplication. (As far as I'm concerned it only works on the local endpoint.)
Head over this website for further information on the topic, it is a very good read.
Now for that, you will need to set these properties BEFORE binding the socket. So you will need to call socket.Bind()
separately from the TcpClient constructor.
Use TcpClient.Client
to access the socket of the tcpClient.
Proper way of creating a TcpClient with reusable local endpoint:
QUESTION
I am trying to use WebClient to consume an endpoint which provides a token. Using Postman it works as expected. Exported curl from postman is:
...ANSWER
Answered 2021-Sep-26 at 14:51Using --data-urlencode
curl option, you are adding a parameter to the request's body. In your Kotlin code, you are not passing the same data in the request's body, but in the headers.
What you should do (to mimic postman behavior) is to pass grant_type
, client_id
, client_secret
in the request body by using BodyInserters
, like this:
QUESTION
I run client code on Unity and server code on a console application. The problem is that when the server sent a 'welcome' message to the client, the client receives an empty message. But weirdly when I run the code on Unity in debug mode it works well. Does anyone know why this happens?
Here is the client code
...ANSWER
Answered 2021-Oct-05 at 12:11networkStream.BeginRead(data, 0, data.Length, DefaultReadCallback, networkStream);
QUESTION
I have simple TcpListener
which accepts a pending connection and I read data from NetworkStream
with using a StreamReader.ReadLineAsync
.
TcpClient
writes data to the stream.
According to documentation
A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n")
The problem is that StreamReader.ReadLineAsync
ignores the carriage return and never reads the line.
Server:
...ANSWER
Answered 2021-Oct-05 at 07:45The StreamReader behaves exactly as described in documentation. Unfortunately it does it by waiting for next character following \r
to see if it is \n
or not.
Code for both sync and async version (reference code ) is roughly
QUESTION
Any ideas on why I can't get a unity C# server and python client on the same network to play nicely?
C# server
...ANSWER
Answered 2021-Sep-11 at 19:35The solution to my problem was simply to add a firewall exception using This helpful resource
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install tcpclient
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