This question already has answers here:
Listening for an Ethernet Cable Unplugging Event for a TCP Server Application
(4 answers)
Closed 5 years ago.
I have an application with one server and one client.
How the client will know if the server cable is unplugged or disconnect? Is there event for that?
I don’t want to set a timer to check occasionally if the server gets messages…
And i don't want to use poll...
Thanks,
Elad
Refer at that questions :
Stack overflow 1
StackOverflow 2
Yopu have to analyze the connection status in all the phases and understand in wich phase are the problem, anyway why the connection doesn't works.
There are also other link that refer to your question :
Stack overflow 3
Blog
Last method, work with NetworkChange.NetworkAvailabilityChanged Event
Link : enter link description here
Related
This question already has answers here:
Is TCP 100% reliable? [closed]
(3 answers)
How reliable is a TCP connection?
(2 answers)
How reliable is .NET TCP protocol?
(5 answers)
Closed 5 years ago.
I'm currently working with the assumption that when sending TCP data with System.Net.Sockets.Socket.Send ,I am guaranteed to get an exception if the connection drops. Is it possible to have a connection drop at the OS layer without receiving a notification/exception in the C# application on top?
I don't imagine there is such a case, in .net, but how would I go about demonstrating this to someone that is skeptical.
Actually as far as i know It is possible. Imagine you create a packet send it and it gets dropped by the way. Os should automatically retransmit when ttl timesout. It will retry few times before giving up. More advanced firewalls have one small option as I remember. Drop with or without notification. Second looks like packet was 'lost' on the way to destination. They actually receive it but let's say- sends them to null without any answer.
I do not know how exactly socket.send works but from network point of view it is possible to not get confirmation for every packet that was lost/dropped.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a question regarding a TCP Connection Class I'm writing.
The Connection Class I already wrote can establish a connection via TCP. It listens and writes to that connection from two different threads. Now I want to improve that class.
I want to establish this connection fully asynchronous so that the connection can be establishes while other things in my application will be prepared.
But I have some other Classes which needs an established connection to work properly. So, my question is, could someone explain me with an little example how I could establish the connection asynchronous with the possibility that an other class starts to work after the connection is established?
My first idea was to fire an event after the TCP Client is connected but then I got in trouble with the fact, that the connection is only fully established if the remote server sends a message back.
After that I got an idea to create a connection state. But now I don't know how to proceed further.
If it's not the way how to use await and async please let me know.
Thanks in advance,
Patrick
Edit:
Thanks for your answers. To clarify things I want to update my post.
As someone requested what I have so far, I posted my source code at GitHub. In Line 46 I set the connection state to Connecting. After the TCP connection is established I change the status to Fetching (L155). Now I have to wait for the message which indicates that the connection is fully ready. After the Message came I expected I set the State to Open (L315) and fire an ConnectionEstablished Event.
Please excuse but I can't post more than 2 links.
My bit into this: instead of using thread blocks; write a Task for the TCP connection with ManualresetEvent. Making the other part of Thread to change their state based on ManualresetEvent. I personally like Task link is -> https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx
over Thread or Threadpool.
Edit: I have written this on notepad today. If it gives any help.
Task t = Task.Run(() =>
{
//do the connection thing
Connection();
connectionDone.Set(); //Manualeventreset connectionDone = new ManualResetEvent(false);
});
try
{
t.Wait();
}
catch (AggregateException ae)
{
//write the exception somewhere
WriteLog("Exception in task: " + ae);
}
finally
{
connectionDone.Reset();
}
This question already has answers here:
Request.UserHostAddress issue with return result "::1"
(4 answers)
Closed 9 years ago.
I have a login page where the user can login to my website when the user click on the submit button in the login page I want to read it's Ip address how ca i do this is c# I tried this
Request.UserHostAddress;
but the result was ::1 is it because I am logging in from the local machine or the statement that I used above is wrong?
That's basically the IPv6 version of 127.0.0.1 so is technically correct. If you don't use or need IPv6 disable it in the network adapter settings.
::1 is the IPv6 address, your code is correct.
See this answer for details.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
My program uses a serial communication port such as COM1. But sometimes this port get locked in a port in use state because of a program failure or something else.
Suppose that another software use it. Can I force using that port?
I would like to use that port anyway. Is it possible?
#MaxMommersteeg if there is method such as DLL func ,WinApi it would be great.
It isn't hard getting the available COM ports, and it is easier than you think.
public List<string> GetAllAvailableComPorts()
{
return System.IO.Ports.SerialPort.GetPortNames().ToList();
}
Refer to Get Port Names Method (MSDN) for the GetPortnames method.
The code above will return you a List<string> filled with all available COM ports.
You can call it with code below:
//Create new list<string>
List<string> availableComPorts = new List<string>();
//Fill list with list<string> returned from GetAllAvailableComports method.
availableComPorts = GetAllAvailableComports();
All COM ports in the list are available, and COM ports that are currently in use will not appear in the list. You can use the List<string> to check if COM1 is available. If so, you can connect to it, and if not, you can give the user a message that it is in use (users will get an "Acces denied" message when connecting to an unavailable port.)
I think you are using Windows Forms, since you tagged C# and SerialPort and I don't think ASP and WPF are able to use the SerialPort class (correct me if I am wrong). So I would create a listbox, filled with all available COM ports and let the user choose which COM port to use. The user isn't able to pick the wrong COM port, since they aren't in the list.
If you need more information about this, you can just leave a comment below. I still got enough code in head left since my last created application which used a serialport connection as well.
I would like to refer you to my own question below. It has some more information:
Get list of available COM ports
This question already has an answer here:
Is there a way to specify the local port to used in tcpClient?
(1 answer)
Closed 9 years ago.
First off, I don't want to do this in production! I need to test whether someone else's implementation of a protocol on top of TCP is causing issues.
I want to use a certain outbound port over and over for multiple TCP sessions. Windows normally increments the port for each new session, and I want to circumvent this for testing. How can I set the outbound port of a TcpClient?
According to another post (Is there a way to specify the local port to used in tcpClient?). You need to use the constructor overload that takes an IPEndpoint in order to specify the local port to use.