C # + socket disconnection rearrangement

Source: Internet
Author: User

Socket Connection Exception "due to the active rejection of the target machine, unable to connect (System.Net.Sockets.SocketException:Connection refused)"

1. If the TCP/UDP protocol is used to connect, check Windows Firewall to open the corresponding SOCKETTCP/UDP port;
The simple detection method is to shut down Windows Firewall and try again;

2. Check the firewall software to open the corresponding SOCKETTCP/UDP port;
The simple detection method is to shut down the firewall software and try again;

3. If both the server side and the client are running on this machine, the corresponding serverip= "127.0.0.1", serveraddress= "127.0.0.1";
Server-Side listening:
Listener = new TcpListener (Ipaddress.parse (ServerIP), listenport);
Listener. Start ();
The client establishes a connection to the server:
Clientsocket = new TcpClient (serveraddress, ServerPort);

4. If the server is running on a LAN or WAN, the corresponding
The serverip,serveraddress is set to the IP on the local area network or WAN;

Pay attention to Serverport==listenport;

System.Net.Sockets.SocketException: A socket operation was attempted on a network that could not be connected (System.Net.Sockets.SocketException:Network is Unreachable)

IPAddress ipserver = Ipaddress.parse ("192.x.x.x");

IPAddress ipserver = Ipaddress.parse ("127.0.0.1"); that's fine.

Socket. Connect (Ipserver, 4000);

------Solution--------------------
"192.x.x.x" is not a valid address
127.0.0.1 is a loopback address, the protocol returns immediately without any network transmission
There is no problem with the code, you need to check the host IP address and port number you want to connect, and use Telnet IP port test to see if you can connect to the remote host.


First, the network commonly used Method 1, when socket.conneted = = False, call the following function to judge

Click (here) to collapse or open///
When socket.connected is false, further determines the current connection state
///
///
private bool Issocketconnected ()
{
#region Remarks
/********************************************************************************************
* When socket.conneted is false, if you need to determine the current state of the connection, make a non-blocking, 0-byte Send call.
* If the call successfully returns or throws a Waewouldblock error code (10035), the socket is still in a connected state;
* Otherwise, the socket is no longer in a connected state.
* Depending on http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.connected.aspx?cs-save-lang=1 &cs-lang=csharp#code-snippet-2
********************************************************************************************/
#endregion

#region Process
This is what you can determine whether a socket is still connected.
bool Connectstate = true;
BOOL Blockingstate = socket. Blocking;
Try
{
byte[] tmp = new byte[1];

Socket. Blocking = false;
Socket. Send (TMP, 0, 0);
Console.WriteLine ("connected!");
Connectstate = true; If the send error jumps to execute the catch body, it does not execute the following code in its try body
}
catch (SocketException E)
{
10035 = = Wsaewouldblock
if (E.nativeerrorcode.equals (10035))
{
Console.WriteLine ("Still Connected, but the Send would block");
Connectstate = true;
}

Else
{
Console.WriteLine ("Disconnected:error Code {0}!", e.nativeerrorcode);
Connectstate = false;
}
}
Finally
{
Socket. Blocking = blockingstate;
}

Console.WriteLine ("Connected: {0}", client. Connected);
return connectstate;
#endregion
2, according to Socket.Poll judgment

Click (here) to collapse or open///
Another way to judge connected, but not to detect the disconnection or ungraceful of the end network cable
///
///
///
static bool Issocketconnected (Socket s)
{
#region Remarks
/* As Zendar wrote, it is nice to use the Socket.Poll and socket.available, but your need to take into conside Ration
* That's the socket might not have been initialized in the.
* This are the last (I believe) piece of information and it are supplied by the Socket.connected property.
* The revised version of the method would looks something like this:
* FROM:HTTP://STACKOVERFLOW.COM/QUESTIONS/2661764/HOW-TO-CHECK-IF-A-SOCKET-IS-CONNECTED-DISCONNECTED-IN-C * *
#endregion

#region Process

Return! ((S.poll (1000, Selectmode.selectread) && (s.available = 0)) | |!s.connected);

/* The long, but Simpler-to-understand version:

BOOL Part1 = s.poll (1000, selectmode.selectread);
BOOL Part2 = (s.available = = 0);
if ((part1 && part2) | | |!s.connected)
return false;
Else
return true;

*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.