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

AsyncSocket doesn't accept another connection

The first tim ethis app is run it works just fine as well as every
restart. What i can't figure is how to get it to accept a connection
after the first one.

This is a console app but i don't think that matters.
using SNS = System.Net.Sockets;
......

SNS.TcpListener listn = new SNS.TcpListener(System.Net.IPAddress.Any,
20000);
listn.Start();

IAsyncResult ar = listn.BeginAcceptTcpClient(new
AsyncCallback(ProcessSocket), listn);
.....

static void ProcessSocket(IAsyncResult ar)
{
// Get the listener that handles the client request.
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
SNS.TcpClient clientSocket = listener.EndAcceptTcpClient(ar);

Console.WriteLine("Client connected completed");

UpdateAndRestart();

clientSocket.Close();

listener.Stop();
listener.Start();
}
Nov 9 '06 #1
3 3967
After the EndAcceptSocket in ProcessSocket, you need to tell your server
socket to listen for another connection.

Mike.

"Dan Holmes" <da*******@bigfoot.comwrote in message
news:eZ**************@TK2MSFTNGP03.phx.gbl...
The first tim ethis app is run it works just fine as well as every
restart. What i can't figure is how to get it to accept a connection
after the first one.

This is a console app but i don't think that matters.
using SNS = System.Net.Sockets;
.....

SNS.TcpListener listn = new SNS.TcpListener(System.Net.IPAddress.Any,
20000);
listn.Start();

IAsyncResult ar = listn.BeginAcceptTcpClient(new
AsyncCallback(ProcessSocket), listn);
....

static void ProcessSocket(IAsyncResult ar)
{
// Get the listener that handles the client request.
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
SNS.TcpClient clientSocket = listener.EndAcceptTcpClient(ar);

Console.WriteLine("Client connected completed");

UpdateAndRestart();

clientSocket.Close();

listener.Stop();
listener.Start();
}

Nov 9 '06 #2
Hi

You need to do it multithread:

main thread:
loop
listen connection
got connection, spawn a new thread to handle it
end loop

This is actual code ( you may need to have the listening method a worker
thread too):
private void ListenForConnections()
{

TcpListener lis = new TcpListener( 4455);

Socket source = lis.AcceptSocket();
Conn conn = new Conn();
conn.Source = source;
q.Enqueue( conn);
new System.Threading.Thread( new System.Threading.ThreadStart(
ConnHandler)).Start();

}
void ConnHandler()
{
}
"Dan Holmes" wrote:
The first tim ethis app is run it works just fine as well as every
restart. What i can't figure is how to get it to accept a connection
after the first one.

This is a console app but i don't think that matters.
using SNS = System.Net.Sockets;
......

SNS.TcpListener listn = new SNS.TcpListener(System.Net.IPAddress.Any,
20000);
listn.Start();

IAsyncResult ar = listn.BeginAcceptTcpClient(new
AsyncCallback(ProcessSocket), listn);
.....

static void ProcessSocket(IAsyncResult ar)
{
// Get the listener that handles the client request.
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
SNS.TcpClient clientSocket = listener.EndAcceptTcpClient(ar);

Console.WriteLine("Client connected completed");

UpdateAndRestart();

clientSocket.Close();

listener.Stop();
listener.Start();
}
Nov 9 '06 #3
I wouldn't take the mulit-threaded route shown here. There are much, much
better ways do to it.

Change your Process Socket method to:

static void ProcessSocket(IAsyncResult ar)
{
// Get the listener that handles the client request.
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
SNS.TcpClient clientSocket = listener.EndAcceptTcpClient(ar);

//*** New line here: Put the original socket back in async
listen mode
listener.BeginAcceptTcpClient(new AsyncCallback(ProcessSocket),
listn);

//*** Most people would then put the new socket into Async Read
mode
clientSocket.BeginRead(new AsyncCallback(IncomingData),
clientSocket);
}

This approach, using Async Sockets, will give you excellent performacne,
nearly unlimited scalability, and is easier to use than trying to manage
your own threads.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

"ignacio machin" <ig***********@discussions.microsoft.comwrote in message
news:02**********************************@microsof t.com...
Hi

You need to do it multithread:

main thread:
loop
listen connection
got connection, spawn a new thread to handle it
end loop

This is actual code ( you may need to have the listening method a worker
thread too):
private void ListenForConnections()
{

TcpListener lis = new TcpListener( 4455);

Socket source = lis.AcceptSocket();
Conn conn = new Conn();
conn.Source = source;
q.Enqueue( conn);
new System.Threading.Thread( new System.Threading.ThreadStart(
ConnHandler)).Start();

}
void ConnHandler()
{
}
"Dan Holmes" wrote:
>The first tim ethis app is run it works just fine as well as every
restart. What i can't figure is how to get it to accept a connection
after the first one.

This is a console app but i don't think that matters.
using SNS = System.Net.Sockets;
......

SNS.TcpListener listn = new SNS.TcpListener(System.Net.IPAddress.Any,
20000);
listn.Start();

IAsyncResult ar = listn.BeginAcceptTcpClient(new
AsyncCallback(ProcessSocket), listn);
.....

static void ProcessSocket(IAsyncResult ar)
{
// Get the listener that handles the client request.
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
SNS.TcpClient clientSocket =
listener.EndAcceptTcpClient(ar);

Console.WriteLine("Client connected completed");

UpdateAndRestart();

clientSocket.Close();

listener.Stop();
listener.Start();
}

Nov 10 '06 #4

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

Similar topics

2
by: sundialsvc4 | last post by:
(Environment: Linux) curl 7.10.6 (i386-redhat-linux-gnu) libcurl/7.10.6 OpenSSL/0.9.7a ipv6 zlib/1.1.4 Protocols: ftp gopher telnet dict ldap http file https ftps Features: IPv6 SSL libz NTLM ==...
14
by: Mary Ellen Curtin | last post by:
(bad developer! no biscuit!) Hello gang, I'm a newbie here and to CSS, though I've been hacking around in HTML for a bit. One of the ways I've been learning CSS is by wending through AListApart....
1
by: Nikolay Petrov | last post by:
Code: \\\\\\\\ Private Sub ConnectionsListener() _acceptCallBack = New AsyncCallback(AddressOf acceptHandler) 'Dim listenSocket As Socket Try _listenSocket = New...
3
by: Ole | last post by:
When creating a socket connection a typical scenario is: MySocket = ListenSocket.Accept(); but if only one connection is necessary will it then not be better to close the ListenSocket after that...
6
by: felix.citycs | last post by:
Why I cannot do with this code and exception is thrown with "the requested address is not valid in its context" try { IPAddress inputDNS_IP = Dns.Resolve(inputDNS_IP).AddressList; // start...
2
by: CLM | last post by:
When I test the remote access at work (lapttop computer to desk top computer) the connection works fine. When I bring the laptop home and try to connect to my desktop I get the error msg: The...
3
by: =?Utf-8?B?Sm9obkJhdGVz?= | last post by:
I'm trying to (programatically) backup and clear the security event log on the local machine. I can do this manually through the event viewer and I am logged on as an administrator. I can...
0
by: T1Stuff | last post by:
I have programmed a simple synchronous socket server app in vb.net to communicate and receive messages from our data integration tool that works well with all our other systems. The program I...
0
by: Xionbox | last post by:
Hello everybody, The error I have seems very easy to solve, but for some odd reason I can't seem to solve it. Anyways, here's my "setup". I created a server running on localhost:1200 (telnet...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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.