473,779 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sock ets;

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

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

Nov 15 '05 #1
1 1271
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.GetHostByNa me
("MYHOST").Addr essList[0];
TcpListener server = null;

try
{
Console.WriteLi ne("Starting listener...");
server = new TcpListener(ipA ddress, 6007);
server.Start();
}
catch (Exception e1)
{
}

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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ray Mitchell" <Ra************ *****@MeanOldTe acher.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.Sock ets;

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

while (true)
{
try
{
try
{
Console.WriteLi ne("Starting listener...");
server = new TcpListener(ipA ddress, 6007);
server.Start();
}
catch (Exception e1)
{
}
TcpClient client = server.AcceptTc pClient();
Console.WriteLi ne("Client connected...");
// Do work with client here...
}
catch (Exception e2)
{
Console.WriteLi ne("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
8403
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 submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the client side javacript code for form validation and client side editing capabilities in order to save...
1
1550
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 get it to recieve only one line of data, but the next one i send to it wont be printed like the 1st one. I had a listner running in a thread, does anyone have a simple listner code example that would show how to have a tcplistner thread running...
4
118817
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 character into a number?????? In Oracle, it is:
10
2137
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 System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
24
2961
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 A1 is less then or equal to 499, deliver B1*1, if A1 is equal to or less than 999 but more than and including 500, deliver B1*2, If A1 is equal to or less than 1499 but more than and including 1000, deliver B1*3, AND SO ON..... I have read the...
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
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
9930
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
8961
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
6724
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
5373
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...
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
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.