473,397 Members | 1,950 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,397 software developers and data experts.

TCP IP Programming

MR
i would appreciate some assistance in finding a reference that describes how
to set up a tcp/ip server/listener in c#. also samples would come in handy.
thanks
m
Nov 15 '05 #1
6 6312
Check TcpListener class in Framework SDK help (or on msdn site). Description
of class provides simple samples for listening and accepting connections.

HTH
Alex

"MR" <bi*****@att.net> wrote in message
news:Oj*************@tk2msftngp13.phx.gbl...
i would appreciate some assistance in finding a reference that describes how to set up a tcp/ip server/listener in c#. also samples would come in handy. thanks
m

Nov 15 '05 #2
Are there any good sites (or books) that describe some of the more detailed
nuances of TCP/IP programming with .NET? The MSDN description of the
TcpListener class doesn't really go in to much detail. In particular,
issues such as how these connections are shared between processes? I
understand how this stuff works on linux (closeOnExec flags, referencing
FD's by integer ID in spawned processes, etc.) but in an object oriented
model, where the children processes don't have access to the same object
namespace, I'm not sure how it would work.

For example, I am having a problem where I have a windows service that
spawns another program that stops the first service and then restarts it,
and the second instance of service doesn't seem to be able to listen on the
port (as though the killing-program inherited it from the first instance of
the service and thus windows doesn't think it's been let go)? If I run the
killing-program by hand from a command shell (so it wouldn't have inherited
the listen from the service) I don't have this problem.

Thanks,
Doug
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:Ou**************@TK2MSFTNGP09.phx.gbl...
Check TcpListener class in Framework SDK help (or on msdn site). Description of class provides simple samples for listening and accepting connections.

HTH
Alex

"MR" <bi*****@att.net> wrote in message
news:Oj*************@tk2msftngp13.phx.gbl...
i would appreciate some assistance in finding a reference that describes

how
to set up a tcp/ip server/listener in c#. also samples would come in

handy.
thanks
m


Nov 15 '05 #3

"Doug Wyatt" <ne*********@starbak.net> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
In particular,
issues such as how these connections are shared between processes? I
understand how this stuff works on linux (closeOnExec flags, referencing
FD's by integer ID in spawned processes, etc.) but in an object oriented
model, where the children processes don't have access to the same object
namespace, I'm not sure how it would work.
Windows does not have "child processes", each process is independent. There
is no direct correlation, to my knowledge, of interacting two processes that
both utilize TCP/IP, except using Windows API calls or else opening a TCP/IP
socket on another application port and communicating "over the wire". You
can, of course, indirectly query the stack to see if another application is
bound to a specific port, such as by attempting to bind to the port and
handling any exceptions that occur in that attempt.
For example, I am having a problem where I have a windows service that
spawns another program that stops the first service and then restarts it,
and the second instance of service doesn't seem to be able to listen on the port (as though the killing-program inherited it from the first instance of the service and thus windows doesn't think it's been let go)?
You should never kill a TCP/IP port-bound application's process abruptly.
Request a closure. If it is a Windows Service, handle the Stop event [or
equivalent]. Then, while closing or stopping, the process should stop the
TcpListener.

Wait also for the binding to be released.

// StopPriorProcess(); // todo: here, stop the old process
bool error = true;
while (error) {
try {
TcpListener myTcpListener; // ...
// myTcpListener.Listen(); // todo: bind to port
error = false;
} catch {
error = true;
Thread.Sleep(0);
System.Windows.Forms.Application.DoEvents();
}
}

HIR [Hope I'm Right],
Jon

If I run the
killing-program by hand from a command shell (so it wouldn't have inherited the listen from the service) I don't have this problem.

Thanks,
Doug
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:Ou**************@TK2MSFTNGP09.phx.gbl...
Check TcpListener class in Framework SDK help (or on msdn site).

Description
of class provides simple samples for listening and accepting connections.
HTH
Alex

"MR" <bi*****@att.net> wrote in message
news:Oj*************@tk2msftngp13.phx.gbl...
i would appreciate some assistance in finding a reference that
describes how
to set up a tcp/ip server/listener in c#. also samples would come in

handy.
thanks
m



Nov 15 '05 #4
AA
Check

www.devshock.com
"MR" <bi*****@att.net> escribió en el mensaje
news:Oj*************@tk2msftngp13.phx.gbl...
i would appreciate some assistance in finding a reference that describes how to set up a tcp/ip server/listener in c#. also samples would come in handy. thanks
m

Nov 15 '05 #5

"Jon Davis" <jo*@REMOVE.ME.PLEASE.jondavis.net> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...

[...]

Windows does not have "child processes", each process is independent. There is no direct correlation, to my knowledge, of interacting two processes that both utilize TCP/IP, except using Windows API calls or else opening a TCP/IP socket on another application port and communicating "over the wire". You
can, of course, indirectly query the stack to see if another application is bound to a specific port, such as by attempting to bind to the port and
handling any exceptions that occur in that attempt.

Really? I had gathered that Windows didn't have a "process tree"
parent/child relationship like UNIX systems do, but I could swear that I
came across references to windows apps sharing open files and network
connections that way. But I have to admit, given .NET semantics, I couldn't
figure out how it'd be done.
[...]

You should never kill a TCP/IP port-bound application's process abruptly.
Request a closure. If it is a Windows Service, handle the Stop event [or
equivalent]. Then, while closing or stopping, the process should stop the
TcpListener.
I do do that. The chain of events is something like the following. And,
just to be clear, the two services are actually the same service. Just
different instantiations of it.

Old myService KillerProg New myService
------------ ---------- ----------
---
Gets Start Event
Creates TcpListener
Gets "restart" msg
Spawns KillerProg
Invokes "net stop
myService"
Waits for that to
finish
Gets Stop Event
Closes TcpListener
Exits
Sleeps 2 seconds
Invokes "net start
myService"
Waits for that to
finish

Gets Start Event

Creates TcpListener
Exits
and at this point, clients cannot connect to the new TcpListener, though,
oddly, I didn't get a failure from the second TcpListener creation. And,
like I said, if I invoke KillerApp from a cmd.exe shell (as opposed to
having myService invoke it itself), it works just fine. So it would seem
that it has something to do with how the first myService shutsdown when it
has spawned this process.

As an experiment, I added a call to close the TcpListener to myService right
before it invokes KillerApp. This seemed to solve the problem (again, very
similar to what would happen in POSIX had I not set it to closeOnExec).
Unfortunately, I can't leave it this way since I don't know whether
KillerApp is really going to restart the services (it's not really
KillerApp, it's more like "HelperApp", which sometimes turns out to restart
the services). So it really does seem that something about spawning off
the KillerApp before I close the TcpListener keeps it from dying correctly
when stopped by a process that it itself spawned.

Hrm....

Thanks,
Doug

[...]

HIR [Hope I'm Right],
Jon

[...]

Nov 15 '05 #6

"Doug Wyatt" <ne*********@starbak.net> wrote in message
news:en**************@tk2msftngp13.phx.gbl...

"Jon Davis" <jo*@REMOVE.ME.PLEASE.jondavis.net> wrote in message
news:ei*************@tk2msftngp13.phx.gbl...

[...]

Windows does not have "child processes", each process is independent. There
is no direct correlation, to my knowledge, of interacting two processes

that
both utilize TCP/IP, except using Windows API calls or else opening a

TCP/IP
socket on another application port and communicating "over the wire". You can, of course, indirectly query the stack to see if another application

is
bound to a specific port, such as by attempting to bind to the port and
handling any exceptions that occur in that attempt.


Really? I had gathered that Windows didn't have a "process tree"
parent/child relationship like UNIX systems do, but I could swear that I
came across references to windows apps sharing open files and network
connections that way.


Ah, no. Please share where you saw this. What a neat trick that would be....
Very useful.

On the other hand, Windows does support DCOM and DDE but it's sloppy and
..NET doesn't speak either one very well.

http://msdn.microsoft.com/library/de...etwork_dde.asp

But I have to admit, given .NET semantics, I couldn't
figure out how it'd be done.
[...]

You should never kill a TCP/IP port-bound application's process abruptly. Request a closure. If it is a Windows Service, handle the Stop event [or
equivalent]. Then, while closing or stopping, the process should stop the TcpListener.
I do do that.


Yep you do.
The chain of events is something like the following. And,
just to be clear, the two services are actually the same service. Just
different instantiations of it.

Old myService KillerProg New myService ------------ ---------- -------- -- ---
Gets Start Event
Creates TcpListener
Gets "restart" msg
Spawns KillerProg
Invokes "net stop
myService"

Somewhere in there you need to run TcpListener.Stop() before killing it. Are
you doing that? This suggestion shouldn't be necessary, though;
TcpListener's deconstructor should unbind from the TCP/IP stack
automatically. So I don't know. I have seen other problems in other areas of
the .NET framework where deconstructors seemed to be missing. For instance,
for the system tray icon object (NotifyIcon) you have to make its Visible
property to False before unloading it or else the icon will linger in the
Windows system tray until you mouse over it (at least this is true for me in
Windows XP).

Good luck, hope others are able to help.

Jon
Nov 15 '05 #7

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

Similar topics

5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
30
by: Jakle | last post by:
I have been googling, but can seem to find out about C GUI libraries. My main platform is Windows, but it would be nice to find a cross platform library. I've been programming with php, which...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
111
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks...
14
by: deko | last post by:
For building Windows desktop apps, the clear favorite is C#. But my clients can't afford to buy Microsoft products. So I need to develop software for Linux users and web applications. In the...
17
by: CoreyWhite | last post by:
I bought this book years ago, when I was just learning C++. Since then I've gone through every math course offered at my college, taken courses on coding C & thinking in terms how how to make the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...

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.