473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Client Socket won't disconnect?!

Hi. Can somebody please help me - I cannot get my TCP client application to close its socket one I attempt a send.

It will close the socket fine if I don't call Send or BeginSend. Soon as I do, it won't close.

Thank you

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Net.Sockets;
  7. using System.Net;
  8.  
  9. namespace SockTest
  10. {
  11.   class SockTest
  12.   {
  13.     public static ManualResetEvent connectDone;
  14.     public static ManualResetEvent sendDone;
  15.     public static Socket           socket;
  16.     public static IPAddress        remoteIPAddress;
  17.     public static IPEndPoint       remoteEndPoint;
  18.  
  19.     //------------------------------------------------------------------------------------
  20.     // ConnectCallback()
  21.     //------------------------------------------------------------------------------------
  22.     private static void ConnectCallback( IAsyncResult ar )
  23.     {
  24.       Socket s = (Socket) ar.AsyncState;
  25.       s.EndConnect( ar );
  26.       connectDone.Set();
  27.     }
  28.  
  29.     //------------------------------------------------------------------------------------
  30.     // SendCallback()
  31.     //------------------------------------------------------------------------------------
  32.     private static void SendCallback( IAsyncResult ar )
  33.     {
  34.       Socket s = (Socket) ar.AsyncState;
  35.       s.EndSend( ar );
  36.       sendDone.Set();
  37.     }
  38.  
  39. public static Byte[]  abTest =
  40. {
  41.   0x59, 0x00, 0x38, 0x00, 0x6F, 0x30, 0x00, 0x77, 0x30, 0x30, 0x31, 0x31, 0x32, 0x35, 0x37, 0x00,  
  42.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  43.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,  
  44.   0x36, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  45.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
  46.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  47. };
  48.  
  49.     static void Main( string[] args )
  50.     {
  51.       Byte[] asBytes  = new Byte[ 4 ];
  52.  
  53.       asBytes[ 0 ] = 127;
  54.       asBytes[ 1 ] = 0;
  55.       asBytes[ 2 ] = 0;
  56.       asBytes[ 3 ] = 1;
  57.  
  58.       connectDone     = new ManualResetEvent( false );
  59.       sendDone        = new ManualResetEvent( false );
  60.       remoteIPAddress = new IPAddress( asBytes );
  61.       remoteEndPoint  = new IPEndPoint( remoteIPAddress, 2005 );
  62.  
  63.       socket          = new Socket( AddressFamily.InterNetwork, 
  64.                                     SocketType.Stream, 
  65.                                     ProtocolType.Tcp );
  66.  
  67.       socket.BeginConnect( remoteEndPoint, ConnectCallback, socket );
  68.  
  69.       if( ! connectDone.WaitOne( 2000, false ) )
  70.       {
  71.         Console.WriteLine( "Connect failed" );
  72.         return;
  73.       }
  74.  
  75.       // Below line of code causes the socket to NOT close when socket.Close()
  76.       // is called!!
  77.       socket.Send( abTest, 0 );
  78.  
  79. /* This doesn't work either
  80.       socket.BeginSend( abTest, 
  81.                         0, 
  82.                         abTest.Length, 
  83.                         SocketFlags.None, 
  84.                         new AsyncCallback( SendCallback ), socket );
  85.  
  86.       if( ! sendDone.WaitOne( 2000, false ) )
  87.       {
  88.         Console.WriteLine( "Send failed" );
  89.       }
  90.  */
  91.  
  92.       socket.Close();
  93.  
  94.       Console.WriteLine( "Press a key to STOP" );
  95.       Console.ReadLine();
  96.     }
  97.   }
  98. }
  99.  
  100.  
  101.  
Jun 28 '07 #1
2 2222
Hmmm... Actually, the information I stated in my previous post is not entirely true..

The server application I am connecting to is an old MSVC 6 C++ application that used Win32 Winsock calls (select(), recv(), send etc).

My C# .NET client does close its connection when I connect to any other application (I just tried it on an MFC socket test server application).

The offending Win32 Winsock based server has been around for years.. I don't know why it isn't detecting the client disconnections from .NET socket client applications.

There must be a socket configuration parameter which causes this behaviour..??
Jun 29 '07 #2
Resolved!

For anyone experiencing the same problem, its dodgy old Win32 code usage.

The server application I'm using only tests for recv() returning zero in order to determine if a client has disconnected. The .NET client I am using results in recv() returning -1 (SOCKET_ERROR) and this wasn't being handled.

Hope this helps somebody else thats been in my boat.
Jun 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
5
by: Morten | last post by:
How do I detect if a client socket is no longer connected to the listen tcp socket ? I have tried with (just an example): --------------------- Socket tcpSocket; ...
4
by: Herbert VON GRÜNENWALD | last post by:
hi everyone, i've made a client/server class in C++ and some other to do the same thing in c#. clients and server works fine, in the two langages, but there is one problem when a c++ client...
8
by: Claire | last post by:
I'm trying to debug my network application ie I want to check my error handling when the connection is broken. Im using 127.0.0.1 as the connection address. Unfortunately, the client socket goes...
5
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the...
0
by: khu84 | last post by:
Here is client server very simple code, seems to work with telnet but with with web client code gives blank output. Following is the server code:- <?php function...
4
by: Adam Clauss | last post by:
A while back I posted regarding a problem we were having with one of our applications which was randomly crashing. Monitoring memory usage revealed a spike in nonpaged pool memory just prior to...
1
by: s3raph | last post by:
Hi, I'm having trouble detecting the client machine connection lost when the client physically pushes the power or restart button on their computer. Using the code from Plater in the following...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.