473,406 Members | 2,707 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,406 software developers and data experts.

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 10243
here it is

/// <summary>
/// Description: Check for dormant sockets and close them.
/// </summary>
/// <param name="eventState">Required parameter for a timer
call back
/// method.</param>
private void CheckSockets(object eventState)
{
lostTimer.Change(System.Threading.Timeout.Infinite ,
System.Threading.Timeout.Infinite);
try
{
foreach (StateObject state in connectedSocks)
{
if (state.workSocket == null)
{ // Remove invalid state object
Monitor.Enter(connectedSocks);
if (connectedSocks.Contains(state))
{
connectedSocks.Remove(state);
Interlocked.Decrement(ref sockCount);
}
Monitor.Exit(connectedSocks);
}
else
{
if (DateTime.Now.AddTicks(-
state.TimeStamp.Ticks).Minute timeoutMinutes)
{
RemoveSocket(state);
}
}
}
}
catch (Exception)
{
}
finally
{
lostTimer.Change(Server.timerTimeout,
Server.timerTimeout);
}
}

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.Connected)
{
MySocket.Close();
MySocket.Disconnect(); // 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.googlegr oups.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.Connected)
{
MySocket.Close();
MySocket.Disconnect(); // 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.googlegr oups.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.Connected)
{
MySocket.Close();
MySocket.Disconnect(); // 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.googlegr oups.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.googleg roups.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.Connected)
{
MySocket.Close();
MySocket.Disconnect(); // 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.googlegr oups.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.googleg roups.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.Connected)
{
MySocket.Close();
MySocket.Disconnect(); // 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
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...
3
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...
4
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...
3
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)...
3
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...
0
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...
1
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...
14
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...
3
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);...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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,...
0
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...

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.