473,698 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reset a socket?

Hello.

I'm writing an application that manages sockets and has a message protocol.
I connect to another machine and I have to send and receive certain
messages.

I need the possibility to restart the communication. I tried with a simple
socket.close method and trying to connect again as the first time, but the
connection is refused.

Is it possible to do that?

--

Regards,

Diego F.

May 29 '07 #1
6 10288
here it is

/// <summary>
/// Description: Check for dormant sockets and close them.
/// </summary>
/// <param name="eventStat e">Required parameter for a timer
call back
/// method.</param>
private void CheckSockets(ob ject eventState)
{
lostTimer.Chang e(System.Thread ing.Timeout.Inf inite,
System.Threadin g.Timeout.Infin ite);
try
{
foreach (StateObject state in connectedSocks)
{
if (state.workSock et == null)
{ // Remove invalid state object
Monitor.Enter(c onnectedSocks);
if (connectedSocks .Contains(state ))
{
connectedSocks. Remove(state);
Interlocked.Dec rement(ref sockCount);
}
Monitor.Exit(co nnectedSocks);
}
else
{
if (DateTime.Now.A ddTicks(-
state.TimeStamp .Ticks).Minute timeoutMinutes)
{
RemoveSocket(st ate);
}
}
}
}
catch (Exception)
{
}
finally
{
lostTimer.Chang e(Server.timerT imeout,
Server.timerTim eout);
}
}

Auratius
http://www.auratius.co.za
May 29 '07 #2
On 29 Mag, 10:12, "Diego F." <diego_f...@msn .comwrote:
Hello.

I'm writing an application that manages sockets and has a message protocol.
I connect to another machine and I have to send and receive certain
messages.

I need the possibility to restart the communication. I tried with a simple
socket.close method and trying to connect again as the first time, but the
connection is refused.

Is it possible to do that?

--

Regards,

Diego F.
You can try with this:

if (MySocket != null)
{
if (MySocket.Conne cted)
{
MySocket.Close( );
MySocket.Discon nect(); // this does not work with Pocket PC
MySocket = null;
}
}
May 29 '07 #3
I tried the close method, assign to null and connecting again, but I get an
exception: No connection could be made because the target
machine actively refused it

--

Regards,

Diego F.
"Sheikko" <sh*****@gmail. comwrote in message
news:11******** **************@ p47g2000hsd.goo glegroups.com.. .
On 29 Mag, 10:12, "Diego F." <diego_f...@msn .comwrote:
>Hello.

I'm writing an application that manages sockets and has a message
protocol.
I connect to another machine and I have to send and receive certain
messages.

I need the possibility to restart the communication. I tried with a
simple
socket.close method and trying to connect again as the first time, but
the
connection is refused.

Is it possible to do that?

--

Regards,

Diego F.

You can try with this:

if (MySocket != null)
{
if (MySocket.Conne cted)
{
MySocket.Close( );
MySocket.Discon nect(); // this does not work with Pocket PC
MySocket = null;
}
}


May 29 '07 #4
On 29 Mag, 11:09, "Diego F." <diego_f...@msn .comwrote:
I tried the close method, assign to null and connecting again, but I get an
exception: No connection could be made because the target
machine actively refused it

--

Regards,

Diego F.

"Sheikko" <shei...@gmail. comwrote in message

news:11******** **************@ p47g2000hsd.goo glegroups.com.. .
On 29 Mag, 10:12, "Diego F." <diego_f...@msn .comwrote:
Hello.
I'm writing an application that manages sockets and has a message
protocol.
I connect to another machine and I have to send and receive certain
messages.
I need the possibility to restart the communication. I tried with a
simple
socket.close method and trying to connect again as the first time, but
the
connection is refused.
Is it possible to do that?
--
Regards,
Diego F.
You can try with this:
if (MySocket != null)
{
if (MySocket.Conne cted)
{
MySocket.Close( );
MySocket.Discon nect(); // this does not work with Pocket PC
MySocket = null;
}
}
In this case the machine that you connect to it must close your
connecgtion. I think the machine let the connection alive. If machine
is yours, yous must close the connection after a timeout.
I have written an application last week on pocket pc and I have used
the code that I passed to you. It work perfectly.
Can you explain me what your application do?

P.S.
(Are you italian?)

May 29 '07 #5
My application connects to a server machine. After sending and receiving
some protocol messages, it starts receiving messages that are inserted in a
database.

The original VB6 application, is capable to detect that the server is
disconnected and tries to connect again. Also, there is a button to
reconnect.

When I try to do the same in VB.NET, as I don't have the Winsock events, I
don't know if the connection is down (I should send something to get an
error and suppose that the connection is down), and worst, I get an error
when trying to reconnect again with the server. I don't know if the problem
is that the server still is VB6 and is any imcompatibility .
PS, I'm from Spain

--

Regards,

Diego F.
"Sheikko" <sh*****@gmail. comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
On 29 Mag, 11:09, "Diego F." <diego_f...@msn .comwrote:
>I tried the close method, assign to null and connecting again, but I get
an
exception: No connection could be made because the target
machine actively refused it

--

Regards,

Diego F.

"Sheikko" <shei...@gmail. comwrote in message

news:11******* *************** @p47g2000hsd.go oglegroups.com. ..
On 29 Mag, 10:12, "Diego F." <diego_f...@msn .comwrote:
Hello.
>I'm writing an application that manages sockets and has a message
protocol.
I connect to another machine and I have to send and receive certain
messages.
>I need the possibility to restart the communication. I tried with a
simple
socket.close method and trying to connect again as the first time, but
the
connection is refused.
>Is it possible to do that?
>--
>Regards,
>Diego F.
You can try with this:
if (MySocket != null)
{
if (MySocket.Conne cted)
{
MySocket.Close( );
MySocket.Discon nect(); // this does not work with Pocket PC
MySocket = null;
}
}

In this case the machine that you connect to it must close your
connecgtion. I think the machine let the connection alive. If machine
is yours, yous must close the connection after a timeout.
I have written an application last week on pocket pc and I have used
the code that I passed to you. It work perfectly.
Can you explain me what your application do?

P.S.
(Are you italian?)

May 29 '07 #6
On 29 Mag, 13:25, "Diego F." <diego_f...@msn .comwrote:
My application connects to a server machine. After sending and receiving
some protocol messages, it starts receiving messages that are inserted in a
database.

The original VB6 application, is capable to detect that the server is
disconnected and tries to connect again. Also, there is a button to
reconnect.

When I try to do the same in VB.NET, as I don't have the Winsock events, I
don't know if the connection is down (I should send something to get an
error and suppose that the connection is down), and worst, I get an error
when trying to reconnect again with the server. I don't know if the problem
is that the server still is VB6 and is any imcompatibility .

PS, I'm from Spain

--

Regards,

Diego F.

"Sheikko" <shei...@gmail. comwrote in message

news:11******** **************@ k79g2000hse.goo glegroups.com.. .
On 29 Mag, 11:09, "Diego F." <diego_f...@msn .comwrote:
I tried the close method, assign to null and connecting again, but I get
an
exception: No connection could be made because the target
machine actively refused it
--
Regards,
Diego F.
"Sheikko" <shei...@gmail. comwrote in message
>news:11******* *************** @p47g2000hsd.go oglegroups.com. ..
On 29 Mag, 10:12, "Diego F." <diego_f...@msn .comwrote:
Hello.
I'm writing an application that manages sockets and has a message
protocol.
I connect to another machine and I have to send and receive certain
messages.
I need the possibility to restart the communication. I tried with a
simple
socket.close method and trying to connect again as the first time, but
the
connection is refused.
Is it possible to do that?
--
Regards,
Diego F.
You can try with this:
if (MySocket != null)
{
if (MySocket.Conne cted)
{
MySocket.Close( );
MySocket.Discon nect(); // this does not work with Pocket PC
MySocket = null;
}
}
In this case the machine that you connect to it must close your
connecgtion. I think the machine let the connection alive. If machine
is yours, yous must close the connection after a timeout.
I have written an application last week on pocket pc and I have used
the code that I passed to you. It work perfectly.
Can you explain me what your application do?
P.S.
(Are you italian?)
You can try also to use Dispose() method to releases the unmanaged
resources used by the Socket, and optionally disposes of the managed
resources.

If It don't work then the problem is in the server machine. Because
when you make Mysocket = null, than you have destroyed the socket and
when you create it in a second time you can use it. I think the
problem is on the server side, than it is not capable to detect if the
client is connected or not.
I think you can use a ping command in the server side.

Let me know. Ok
May 29 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1602
by: Hameed Khan | last post by:
hi all, i am getting some problems with my first socket script. can any one of you point me why this is happening. the server script suppose to accept one connection at a time and send countdown to client connected to it from a function through a separate thread. and echo all the connections opened and closed. the client script is working fine. it echoes the countdown it recieve from server.
3
15131
by: James Yang | last post by:
Hi, I am using Sockets to connect to a remote computer and send data using Socket.Send() and receive using Socket.Receive() (block mode) . for somereason tho, when the client disconnects the Socket.Receive() just passes without any exception. Is there any way to detect disconnection using Sockets? I believe I saw a way of doing this using IAsyncResult, and eventhandling but..no article really explained how to use it properly.
4
1726
by: User | last post by:
Hi, How do I pool my socket to see if there is any message pending? I don't want to use timers because they are multithreaded and it break the linearity of my messages. (incoming message are numbered and it is important to keep the order) Can I add a handler to the socket? So it could react by itself when there is pending data? Something like that the winsock (in VB6) was doing.
3
14114
by: mirandacascade | last post by:
This may be more of a socket question than a python question; not sure. Using this code to instantiate/connect/set options connectionHandle = socket.socket(socket.AF_INET, socket.SOCK_STREAM) errorStatus = connectionHandle.connect_ex((ipAddress, port)) connectionHandle.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, 60000) Using this code to send: retSendAll = connectionHandle.sendall(messageToHost)
3
7791
by: Stephan Steiner | last post by:
Hi I have a small program listening to UDP broadcast datagrams that are periodically sent out. It will stop listening for a certain period if either a sufficient number of packets has been received (this is triggered from a class not in the sample code), or if there has been no data on the net for a certain period. During the time where I don't want any packets, I set my socket receive buffer size to zero. The problem is, when I used...
0
1727
by: J Brad | last post by:
Hi every body I wrote a Asynch Server using ManualResetEvent.Reset(), ManualResetEvent.Set(), ManualResetEvent.WaitOne() events I'm receiving a Message (Example: "Hello Word") without Terminator (No Char or String Terminator like <EOF>) So When I Receive my Message ((Example: "Hello Word") ), My seconde call "handler.BeginReceive()" will not call the ReadCallback() delegate Is there any way to control the end of reading of the message...
1
6280
by: Chris Morse | last post by:
WARNING: Verbosity: skip to the very bottom paragraph for succinct version of my question.) Hi- I can't seem to find an answer to this. I am playing around with a variation of the ".NET Framework Developer's Guide" (in the MSDN docs) "Using an Asynchronous Server Socket" sample. As usual with docs like this, the example isn't very good. I had to
14
12773
by: ahlongxp | last post by:
Hi, everyone, I'm implementing a simple client/server protocol. Now I've got a situation: client will send server command,header paires and optionally body. server checks headers and decides whether to accept(read) the body. if server decided to throw(dump) the request's body, it'll send back a response message, such as "resource already exists" and close the connection.
3
3044
by: A. W. Dunstan | last post by:
I'm creating a socket as follows: m_networkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_networkSocket.LingerState = new LingerOption(true, 1); m_networkSocket.Blocking = true; m_networkSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true); m_networkSocket.SetSocketOption(SocketOptionLevel.Socket,
2
16142
by: kashifjawed | last post by:
I'm developing a c# asynchronous socket application for tranfering file or large data from client to server and server to client as well in chunks. Application sometimes transfer file from client to server and sometimes not, because connection betwenn client and server closed Abrupt. I cannot understand why connection between client and server is closed while i cannot closed the connection until a single block tranfered to server. ...
0
8601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
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
8892
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
8860
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...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3043
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
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.