473,659 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listen to Port

I am looking for code to listen to a tcp port. Another (not .net) process
will be writing to that port on the same computer and I need c# code to be
able to get data, which is a simple string, from that port. I am not a c#
coder but can easy adapt working sample.
Thanks alot for help.

Jun 27 '08 #1
10 9351
read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin " <ma************ *@yahoo.comwrot e in message
news:03******** *************** ***********@mic rosoft.com...
>I am looking for code to listen to a tcp port. Another (not .net) process
will be writing to that port on the same computer and I need c# code to be
able to get data, which is a simple string, from that port. I am not a c#
coder but can easy adapt working sample.
Thanks alot for help.

Jun 27 '08 #2
Hi,

thats really easy to do in .NET. Thats all you need:

//inside a console Main

byte[] byteReadStream = null; // holds the data in byte buffer
IPEndPoint ipe = new IPEndPoint(IPAd dress.Parse("0. 0.0.0"),
8888);//listen on all local addresses and 8888 port
TcpListener tcpl = new TcpListener(ipe );

while(true){ //infinite loop
tcpl.Start(); // block application until data and connection
is requested
TcpClient tcpc = tcpl.AcceptTcpC lient(); //accept connection

byteReadStream = new byte[tcpc.Available]; //allocate space
for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
Console.WriteLi ne(Encoding.Def ault.GetString( byteReadStream)
+ "\n"); Write data to console buffer

}
But see the MSDN Library for more information
about the used Classes.
Regards

Kerem
--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

"markgoldin " <ma************ *@yahoo.comschr ieb im Newsbeitrag
news:03******** *************** ***********@mic rosoft.com...
I am looking for code to listen to a tcp port. Another (not .net) process
will be writing to that port on the same computer and I need c# code to
be
able to get data, which is a simple string, from that port. I am not a c#
coder but can easy adapt working sample.
Thanks alot for help.

Jun 27 '08 #3
What is a difference between listening to a socket or to a TCP port? Or it's
the same?

Thanks
"John Timney (MVP)" <xy******@timne y.eclipse.co.uk wrote in message
news:Qd******** *************** *******@eclipse .net.uk...
read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin " <ma************ *@yahoo.comwrot e in message
news:03******** *************** ***********@mic rosoft.com...
>>I am looking for code to listen to a tcp port. Another (not .net) process
will be writing to that port on the same computer and I need c# code to
be able to get data, which is a simple string, from that port. I am not a
c# coder but can easy adapt working sample.
Thanks alot for help.

Jun 27 '08 #4


"markgoldin " <ma************ *@yahoo.comwrot e in message
news:05******** *************** ***********@mic rosoft.com...
What is a difference between listening to a socket or to a TCP port? Or
it's the same?

Thanks
"John Timney (MVP)" <xy******@timne y.eclipse.co.uk wrote in message
news:Qd******** *************** *******@eclipse .net.uk...
>read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin " <ma************ *@yahoo.comwrot e in message
news:03******* *************** ************@mi crosoft.com...
>>>I am looking for code to listen to a tcp port. Another (not .net) process
will be writing to that port on the same computer and I need c# code to
be able to get data, which is a simple string, from that port. I am not a
c# coder but can easy adapt working sample.
Thanks alot for help.

A socket is basically a combination of an IP address and a port.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Jun 27 '08 #5
>A socket is basically a combination of an IP address and a port.

ACK.

But i would describe a Socket more an connection endpoint
that is bound to an ip address and a corresponding port.

Regards

K.

--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

"Joe Fawcett" <jo********@new sgroup.nospamsc hrieb im Newsbeitrag
news:12******** *************** ***********@mic rosoft.com...
>

"markgoldin " <ma************ *@yahoo.comwrot e in message
news:05******** *************** ***********@mic rosoft.com...
What is a difference between listening to a socket or to a TCP port? Or
it's the same?

Thanks
"John Timney (MVP)" <xy******@timne y.eclipse.co.uk wrote in message
news:Qd******** *************** *******@eclipse .net.uk...
read up on creating a socket listener
http://www.devarticles.com/c/a/C-Sha...g-in-C-Part-I/

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"markgoldin " <ma************ *@yahoo.comwrot e in message
news:03******** *************** ***********@mic rosoft.com...
I am looking for code to listen to a tcp port. Another (not .net)
process
>>will be writing to that port on the same computer and I need c# code
to
>>be able to get data, which is a simple string, from that port. I am not
a
>>c# coder but can easy adapt working sample.
Thanks alot for help.

A socket is basically a combination of an IP address and a port.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Jun 27 '08 #6
On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <ka*******@hotm ail.com>
wrote:
>A socket is basically a combination of an IP address and a port.

ACK.

But i would describe a Socket more an connection endpoint
that is bound to an ip address and a corresponding port.
Well, as long as we're nitpicking... :)

For TCP over TCP/IP, a connected socket represents _two_ endpoints -- the
local endpoint and the remote endpoint -- each of which is defined by an
IP address and a port, and of course the fact that the connection is the
TCP protocol is represented as well. In other words, the connected socket
encapsulates five different pieces of information, and is uniquely
identified by those pieces of information.

Different sockets vary. A listening socket of course has only the
protocol (TCP), local IP and port, and the fact that it's listening. Just
local IP and port wouldn't be sufficient to distinguish it from connected
sockets on the same port. And of course UDP sockets are connectionless,
so the remote endpoint only affects individual datagrams, not the socket
itself.

And then, of course, the Socket class in .NET is a managed object that
provides a managed interface to all of the above. :)

Pete
Jun 27 '08 #7
On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <ka*******@hotm ail.com>
wrote:
[...]
byteReadStream = new byte[tcpc.Available]; //allocate
space
for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
Console.WriteLi ne(Encoding.Def ault.GetString( byteReadStream)
+ "\n"); Write data to console buffer
This is wrong, for at least a couple of reasons.

The most significant one is that TCP does not guarantee that all of the
data sent will be read in a single call to Read(). You _must_ iterate the
read in some way (loop, async i/o, whatever) until you know for sure that
you've read all of the data. This could be as simple as reading
everything until the end of the stream (connection is closed), or more
complicated as in defining a fixed-length for the data, sending a length
before the string, or using some sort of delimiter/terminator (e.g. null
character).

The other issue is that the Available property may change between the time
one allocates the buffer and the time one calls Read(). If it increases,
then the buffer length passed to Read() is larger than the buffer itself
actually is, which will produce an error. At the very least, one should
be passing "byteReadStream .Length" as the buffer length, not some other
value. Preferable would be to allocate the buffer as some fixed size,
rather than anticipating the size of the read. What size is appropriate
depends on your application protocol, but for larger transfers, a 4K or 8K
buffer would be appropriate.

IMHO it would be better for the OP to simply read the docs for
TcpListener, etc. and take advantage of the code samples there. If he
still has questions after looking at the documentation, then we can answer
those in a more specific, directed way.

Pete
Jun 27 '08 #8
Hi Pete,

i even didnt compile the code. I wrote it on the
fly inside #develop. It was just a first hint for the OP.
As statet he should read the docs in my reply,...
Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"Peter Duniho" <Np*********@nn owslpianmk.coms chrieb im Newsbeitrag
news:op******** *******@petes-computer.local. ..
On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <ka*******@hotm ail.com>
wrote:
>[...]
byteReadStream = new byte[tcpc.Available]; //allocate
space
for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array

Console.WriteL ine(Encoding.De fault.GetString (byteReadStream )
+ "\n"); Write data to console buffer

This is wrong, for at least a couple of reasons.

The most significant one is that TCP does not guarantee that all of the
data sent will be read in a single call to Read(). You _must_ iterate the
read in some way (loop, async i/o, whatever) until you know for sure that
you've read all of the data. This could be as simple as reading
everything until the end of the stream (connection is closed), or more
complicated as in defining a fixed-length for the data, sending a length
before the string, or using some sort of delimiter/terminator (e.g. null
character).

The other issue is that the Available property may change between the time
one allocates the buffer and the time one calls Read(). If it increases,
then the buffer length passed to Read() is larger than the buffer itself
actually is, which will produce an error. At the very least, one should
be passing "byteReadStream .Length" as the buffer length, not some other
value. Preferable would be to allocate the buffer as some fixed size,
rather than anticipating the size of the read. What size is appropriate
depends on your application protocol, but for larger transfers, a 4K or 8K
buffer would be appropriate.

IMHO it would be better for the OP to simply read the docs for
TcpListener, etc. and take advantage of the code samples there. If he
still has questions after looking at the documentation, then we can answer
those in a more specific, directed way.

Pete

Jun 27 '08 #9
ACK.
Cheers

K.

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"Peter Duniho" <Np*********@nn owslpianmk.coms chrieb im Newsbeitrag
news:op******** *******@petes-computer.local. ..
On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <ka*******@hotm ail.com>
wrote:
>>A socket is basically a combination of an IP address and a port.

ACK.

But i would describe a Socket more an connection endpoint
that is bound to an ip address and a corresponding port.

Well, as long as we're nitpicking... :)

For TCP over TCP/IP, a connected socket represents _two_ endpoints -- the
local endpoint and the remote endpoint -- each of which is defined by an
IP address and a port, and of course the fact that the connection is the
TCP protocol is represented as well. In other words, the connected socket
encapsulates five different pieces of information, and is uniquely
identified by those pieces of information.

Different sockets vary. A listening socket of course has only the
protocol (TCP), local IP and port, and the fact that it's listening. Just
local IP and port wouldn't be sufficient to distinguish it from connected
sockets on the same port. And of course UDP sockets are connectionless,
so the remote endpoint only affects individual datagrams, not the socket
itself.

And then, of course, the Socket class in .NET is a managed object that
provides a managed interface to all of the above. :)

Pete

Jun 27 '08 #10

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

Similar topics

2
11411
by: Aquila Deus | last post by:
Hi all! I just tried to enable TCP/IP on SQL Server 2000 (trial, SP3a). The Server Network Utility shows it's already turned on, but after restarting the server doesn't listen to any TCP port, and in the eventlog it reports "SuperSocket info: gethostbyname(MSAFD Tcpip ) : Error 11004." and "SuperSocket info: (SpnRegister) : Error 2102.". I googled but found nothing helpful. Does anyone know what's wrong??
9
43135
by: mBird | last post by:
I wrote a service that listens for broadcast messages from my firewall (UDP snmp trap messages) and parses and puts the data in an database. I'd also like to write an app that is a simple form that can listen in when it runs (so I can see messages in a form as they occur.) So I need the ability to listen to a UDP port with two apps at the same time (the service and my app). But when I do that I get an error:...
1
1716
by: HABJAN ®iga | last post by:
Hello, how can i bind a socket for listen on some port, and all addresses : In the sample i bind myself to localhost(127.0.0.1) on port 5500. It works fine when connecting to localhost, but if i connect from another computer, or i connect by computer name, the accept callback never happens. Private sckL As Socket
3
13682
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example, port 12345): '----------- Dim udpClient as New Sockets.UdpClient(12345) Dim ipEndPoint as New IPEndPoint(IPAddress.Any, 0) Dim receiveBytes as Byte()
1
2409
by: Marcelo | last post by:
_____ From: Marcelo Posted At: Friday, September 30, 2005 1:40 PM Posted To: microsoft.public.dotnet.languages.vb Conversation: Subject: Client and Server - Listen por packets in a specific port trhu UDP
1
3401
by: gregory_may | last post by:
I want to bind multiple UDPClients to the same port on the same machine. I want them to all listen for broadcast messages & respond accordingly. Multiple client bindings "under normal circumstances" doesn't work (That's the error I am getting). But, it seems very reasonable to set up many UDPclients to receive the same broadcast messages off the same port on the same machine. Is there some way to passively bind multiple UDPclients...
1
18131
by: ushachandran | last post by:
Hi Please let me know what is the error in the following code. I am doing a tracking project. My vehicle unit has a simcard with sms message stored on it. When the unit powers up it reads the sms and enables the GPRS module(xxxgprs.com is my APN). It establishes the GPRS TCP/IP connection to my server on port 29999. I created a php program which listens the port 29999 to receive the data transmitted by the unit. When I run this...
7
3170
by: silverburgh.meryl | last post by:
Hi, I read the following code which open a server socket for client request. However, i would like to know how can I change it so that i just listen for client requestfor 10 seconds, after that, it bows out? Code:
9
364
by: markgoldin | last post by:
I am looking for code to listen to a tcp port. Another (not .net) process will be writing to that port on the same computer and I need c# code to be able to get data, which is a simple string, from that port. I am not a c# coder but can easy adapt working sample. Thanks alot for help.
0
8427
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
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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
8525
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
5649
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
4175
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
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.