473,848 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Client Socket won't disconnect?!

20 New Member
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 2258
Sparky74
20 New Member
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
Sparky74
20 New Member
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
4501
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 computations, the only data transfered is user mouse/kbd input. It works synchronously, but somehow, when I play in client window, both client and server have 17 fps, while when playing in server window, server has 44 fps while client ...
5
45087
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; while(tcpSocket.Connected)
4
7375
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 disconnect from the c# server: - the connection is OK - when i send a message from the client to the server, no problems (it is received by the server)
8
41446
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 into a CLOSE_WAIT state ad infinitum and never closes fully until my client and server applications shut down. Ive disabled lingeroptions. Is there anything else easy that i can do to force the loopback to close immediately?
5
8244
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 common process of connecting and of the server and client... here is the code i am trying to implement. this code snippet is for message transfer only for multiple clients. /////////////////////////////file receive method when sent from...
0
1752
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 createSocketServer($host='192.168.1.34',$port=2222) { $max_clients = 10;
4
4319
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 the crash each time. We finally think we have narrowed down the cause of this to a user (located semi-remotely) who would connect into our system and disconnect "ungracefully" (literally, by pulling his network cable). Connections here are all...
1
4698
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 thread: http://bytes.com/topic/net/answers/828133-c-net-detecting-socket-closed I have managed to detect disconnect by the client when the client window form is closed, but if the client computer switches off or powers off, it doesn't detect a...
0
11001
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10722
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10348
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9500
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5735
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5919
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4137
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.