can i get client is offline or online in new thread in server side ?
it's mean when a client connected to my server i want check in new thread that The client is still online or not .
It looks like you have the url incorrect for 'TcpClient', and you are not calling the 'Connect' method of the 'TcpClient' instance... here's the corrected version..see this here on MSDN....If I were you, I would wrap this up in a try/catch clause to see the exact error... nitpicky aside, if you are using disposable objects, wrap them up in a using clause also!
Related
Hi StackOverflow Members,
I have created an HTTP server using WebsocketsharpAPI https://github.com/sta/websocket-sharp.
Here I have created an HTTP address = "http://localhost:0001/MoviePage";
I am then, Initializing and creating the Httpserver and starting it under the
Subquery /MoviePage . Here to test if client is receiving data in first place, I am sending a string of movie name instead of movie object that I have
Server.WebSocketServices[DisplayCollimationPort].Sessions.Broadcast("Spider Man 2");
The Server works without any issue. However, the issue is - I am not sure, How to use this
Websocketsharp API to Create a client and receive the sent string via a broadcast function from the server.
The examples are given only harnessing(or maybe I am missing something) the Websocketserver and Websocket class for the Server and Client in the examples provides.
Now since my address is an HTTP one and not a (ws or wss) (WebSocket) one, I would like to know if I can still use this websocketsharp API to implement a client to fetch string sent in this URL and access it. This is also because I would not be able to receive the information from httpserver with Websocket class.
Any suggestions would be much appreciated.
Thanks in Advance !!
I'm struggling with setting connect timeout with c# HttpClient or its siblings (HttpClientHandler,WebRequestHandler,...) . There's a timeout property in HttpClient, but it seems to be a timeout from the beginning of the request until receiving the response. I want to have a method which specify that for example if you don't received ACK from the net socket within 10 seconds for example , then break up and do the next.
I saw that there may be something similar in WinHttpHandler class, but it seems to be deleted or not available in recent version. compare the first link vs second :
1.WinHttpHandler MSDN
2.WinHttpHandler Microsoft Docs
I really need this, because I must differentiate asap between IP's which have a working web servers (maybe slow) vs which don't have a web server at all.
I use HttpWebRequest.Timeout in my project to verdict the connection time before establish tcp connection. And use HttpWebRequest.ReadOrWriteTimeout to verdict whole response timeout.
Ps: The HttpClient seems cut off some useful properties.
I have been been developing a platform using ZMQ (2.2) as the main communications layer. Earlier this week I decided to take the advice on the zeromq website and upgrade to the latest stable build 3.2.2
However after going through the pain of updating to the new API I was seriously disappointed to discover that there seems to be a problem with the clrzmq binding in that it fails to load the libzmq library on Windows XP (SP3) machines. I keep getting a SEHException exception?!
I was just wondering if anyone out there has had the same problem and if there is a workaround (or even better a fix) for it?
Cheers
:)
EDIT
Just to clarify, the library is loaded fine, I know this because the context is created without any issue. The problem occurs when the CreateSocket method is called on the context... see code snippet below
ZmqContext context = ZmqContext.Create();
ZmqSocket socket = context.CreateSocket(SocketType.REQ);
After adding tracing as suggested by Jakob, I get the following output
Assertion failed: Connection refused (..\..\..\src\signaler.cpp:310)
Any Ideas what this means?
EDIT
I should also mention that this issue does not happen on all the XP machines, only some of them. I have been trying to figure out what the difference is between the machines that work and the ones that don't. Without knowing this it would be far too risky to upgrade and release into a production environment.
Looking at the example you provided, you are binding to a REQ socket (Request, i.e. client socket), and also binding the REQ socket using wildcards. I am not sure how this will play out, but to me it does not make sense. I do not think this is supported but I cannot find or remember any documentation about binding to a REQ socket. Likely strange things will happen.
The REP (response) socket is the "server" end of a REQ/REP setup (request/response), where you bind the server side using a REP socket to an endpoint, either explicitly specified "tcp://127.0.0.1:5555" or using wildcards, e.g. "all interfaces", "tcp://*:5555". The client side would then connect using a REQ socket to an explicit endpoint address, "tcp://127.0.0.1:5555", no wildcards.
The server would do this:
ZmqContext context = ZmqContext.Create();
ZmqSocket socket = context.CreateSocket(SocketType.REP);
socket.Bind("tcp://*:5501");
And the client this:
ZmqContext context = ZmqContext.Create();
ZmqSocket socket = context.CreateSocket(SocketType.REQ);
socket.Connect("tcp://127.0.0.1:5501");
Apart from those issues, you should also make sure the firewall is not blocking and make sure the port is not already in use (using for example the NETSTAT command).
For ZeroMq addressing rules, see the zmq_tcp API documentation, and for the socket, see the zmq_socket API documentation.
I'm working on an app that uses .net remoting for IPC.
When my client app starts up, it uses the following code to connect to the server:
chnl = gcnew HttpChannel();
ChannelServices::RegisterChannel(chnl, false);
IPCObjectInstance = (BaseRemoteObject)Activator.GetObject(
typeof(BaseRemoteObject),
"http://localhost:1237/MyRemoteObject.soap");
And, when I make my function call, I use the following code:
BaseRemoteObject.GetFileTextDelegate svd = new BaseRemoteObject.GetFileTextDelegate(IPCObjectInstance, BaseRemoteObject.GetFileText);
AsyncCallback callback = new AsyncCallback(this, &ClientGUI.RecievedStringText);
IAsyncResult arValSet = svd.BeginInvoke(callback, null);
This code works great as is. However, I want my client to detect whether or not the server is running when it boots, and display the appropriate error message.
When the server isn't running, the client waits for about 3 seconds, before throwing a web exception (shown at bottom). There is no error "location", so I'm not sure of where to put the try\catch block.
What is the best way to detect my server not running?
Thanks!
It should work to do a try/catch around your BeginInvoke line.
But my suggestion would be to create a Status method which you call synchrously instead of async, and do try/catch around that call instead. It can be a dummy method doing nothing.
It also possible to open a tcp connection to the remote server on the port specified and see if you get a connection. But this would be much like try/catch around a remoting call.
I am developing an Tcp client in C# and I am using the TcpClient class.
I am not able to connect to the server.
Debugging the application I can see that the call to Connect is successfull but after I do this , I check with netstat the status of the connection on both server and client. The result is that the server the connection is ESTABLISHED, but on the client I cannot see any connection to the server in netstat result.
Reading the TcpClient instance I can see that the property Connected is true so it should be fine but when I try to read from the NetworkStream it hangs.
When I say it hangs I mean that the server is sending data but the readline doesn't get any data at all.
Do you know what could be the issue and some workaround?
Thanks
First, a recommendation: In my experience, TcpClient is best used asynchronously.
In all my usage of TcpClient though, I've never been able to Read a response without first doing a Write with a request. You'll block forever attempting to synchronously await a response to a request you haven't sent.
Expanding on that, sending a request will be done like this:
TcpClient.GetStream().BeginWrite( tcpMessage, ... );
Which will send the request that's in tcpMessage, which will be a bytestream produced from a string like this:
byte[] tcpMessage = httpEncoding.GetBytes( httpMessage );
Which has your request message like this:
httpMessage = "GET / HTTP/1.1\r\n" + ...;
That sends your request, which causes the server to generate a response which you can then collect like this:
TcpClient.GetStream().BeginRead( ... );
And you should finally be able to receive something back!
Even if it's only a "I didn't like your request!" response. 8 D
What you saw is absolutely normal.
Reading the TcpClient instance I can see that the property Connected is true so it should be fine but when I try to read from the NetworkStream it hangs.
When you try to read, the thread will be blocked till the server sends you some data. Otherwise, based on the read method you used, it can be blocked forever.
It depends on the server, but most TCP servers will wait for the client to send a request before it sends a response. It sounds like your thread is waiting for data. Did you set a read timeout?
Make sure you send a request so that the server knows to send you a response.
Normally, you would put your TcpClient into a separate thread and use a read time out to avoid hanging the whole program awaiting a response.
Make sure that you don't use non-blocking (asynchronous) sockets when communicating with a blocking (synchronous) server, you'll only run into problems. Blocking sockets are normal for the web, as most web services use the request/response paradigm.
on client side, what port were you looking for when you did netstat?
Because when client make connections, it would use ephemeral port. Which means it would use any free port # above maximum of well known port number. So, your client maybe using different port # than you are expecting.
And for the networkstream issue, I would have to see the code to determine what went wrong.