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

A simple server question

Hello,

I've inherited the following code and am trying to get it
to work. As I understand it's purpose, it creates a
server, listens for a client to connect, does some work
with the client, then loops back around and listens for
the client to reconnect again in case it should become
disconnected. If the client never disconnects it just
sits there waiting forever.

Although the code does seem to work I get the following
errors the first few times through the loop each time
after the client connects, then it settles down and
resumes listening properly. I'm not sure what is going on
since I'm new to this networking business but all-in-all
the way this is implemented seems like a kluge to me. All
suggestions of a better way to do this are welcome.

Thanks,
Ray Mitchell

Output from first iteration:

Starting listener...
Client connected...

Output from next few iterations after client work is done:

Starting listener...
Exception2: Not listening. Please call the Start() method.
Starting listener...
Exception2: Not listening. Please call the Start() method.
Starting listener...
Exception2: Not listening. Please call the Start() method.
Starting listener...
Exception2: Not listening. Please call the Start() method.
....
....
....
Starting listener...
Client connected...
The Code:

using System;
using System.Net;
using System.Net.Sockets;

class Class1
{
[STAThread]
static void Main(string[] args)
{
IPAddress ipAddress = Dns.GetHostByName
("MYHOST").AddressList[0];
TcpListener server = null;

while (true)
{
try
{
try
{
Console.WriteLine("Starting listener...");
server = new TcpListener(ipAddress, 6007);
server.Start();
}
catch (Exception e1)
{
}
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client connected...");
// Do work with client here...
}
catch (Exception e2)
{
Console.WriteLine("Exception2: " + e2.Message);
}
}
}
}

Nov 15 '05 #1
1 1257
HI Ray,

Why do you repeately create and start a new server on each iteration?
I would try this:
class Class1
{
[STAThread]
static void Main(string[] args)
{
IPAddress ipAddress = Dns.GetHostByName
("MYHOST").AddressList[0];
TcpListener server = null;

try
{
Console.WriteLine("Starting listener...");
server = new TcpListener(ipAddress, 6007);
server.Start();
}
catch (Exception e1)
{
}

while (true)
{
try{
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client connected...");
// Do work with client here...
}
catch (Exception e2)
{
Console.WriteLine("Exception2: " + e2.Message);
}
}
}
} //method
} //class
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ray Mitchell" <Ra*****************@MeanOldTeacher.com> wrote in message
news:09****************************@phx.gbl...
Hello,

I've inherited the following code and am trying to get it
to work. As I understand it's purpose, it creates a
server, listens for a client to connect, does some work
with the client, then loops back around and listens for
the client to reconnect again in case it should become
disconnected. If the client never disconnects it just
sits there waiting forever.

Although the code does seem to work I get the following
errors the first few times through the loop each time
after the client connects, then it settles down and
resumes listening properly. I'm not sure what is going on
since I'm new to this networking business but all-in-all
the way this is implemented seems like a kluge to me. All
suggestions of a better way to do this are welcome.

Thanks,
Ray Mitchell

Output from first iteration:

Starting listener...
Client connected...

Output from next few iterations after client work is done:

Starting listener...
Exception2: Not listening. Please call the Start() method.
Starting listener...
Exception2: Not listening. Please call the Start() method.
Starting listener...
Exception2: Not listening. Please call the Start() method.
Starting listener...
Exception2: Not listening. Please call the Start() method.
...
...
...
Starting listener...
Client connected...
The Code:

using System;
using System.Net;
using System.Net.Sockets;

class Class1
{
[STAThread]
static void Main(string[] args)
{
IPAddress ipAddress = Dns.GetHostByName
("MYHOST").AddressList[0];
TcpListener server = null;

while (true)
{
try
{
try
{
Console.WriteLine("Starting listener...");
server = new TcpListener(ipAddress, 6007);
server.Start();
}
catch (Exception e1)
{
}
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client connected...");
// Do work with client here...
}
catch (Exception e2)
{
Console.WriteLine("Exception2: " + e2.Message);
}
}
}
}

Nov 15 '05 #2

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

Similar topics

2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
24
by: joeldault | last post by:
Question For Microsoft Access Data Base -------------------------------------------------------------------------------- I am Trying to create a single formula that would do the following: If...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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...

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.