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. 10 9324
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.comwrote in message
news:03**********************************@microsof t.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.
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(IPAddress.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.AcceptTcpClient(); //accept connection
byteReadStream = new byte[tcpc.Available]; //allocate space
for data
tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
//read data into byte array
Console.WriteLine(Encoding.Default.GetString(byteR eadStream)
+ "\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.comschrieb im Newsbeitrag
news:03**********************************@microsof t.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.
What is a difference between listening to a socket or to a TCP port? Or it's
the same?
Thanks
"John Timney (MVP)" <xy******@timney.eclipse.co.ukwrote 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.comwrote in message
news:03**********************************@microsof t.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.
"markgoldin" <ma*************@yahoo.comwrote in message
news:05**********************************@microsof t.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******@timney.eclipse.co.ukwrote 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.comwrote in message news:03**********************************@microso ft.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
>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********@newsgroup.nospamschrieb im Newsbeitrag
news:12**********************************@microsof t.com...
>
"markgoldin" <ma*************@yahoo.comwrote in message
news:05**********************************@microsof t.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******@timney.eclipse.co.ukwrote 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.comwrote in message
news:03**********************************@microsof t.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
On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <ka*******@hotmail.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
On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <ka*******@hotmail.com>
wrote:
[...]
byteReadStream = new byte[tcpc.Available]; //allocate
space
for data
tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
//read data into byte array
Console.WriteLine(Encoding.Default.GetString(byteR eadStream)
+ "\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
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*********@nnowslpianmk.comschrieb im Newsbeitrag
news:op***************@petes-computer.local...
On Sat, 31 May 2008 08:50:24 -0700, Kerem Gümrükcü <ka*******@hotmail.com>
wrote:
>[...] byteReadStream = new byte[tcpc.Available]; //allocate space for data tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available); //read data into byte array
Console.WriteLine(Encoding.Default.GetString(byte ReadStream) + "\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
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*********@nnowslpianmk.comschrieb im Newsbeitrag
news:op***************@petes-computer.local...
On Sat, 31 May 2008 09:27:39 -0700, Kerem Gümrükcü <ka*******@hotmail.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
Thanks for your code.
I am running the code, but it only shows data being sent to TCP port if I
debug program firstly stopping it at the line:
byteReadStream = new byte[tcpc.Available];
otherwise, if I just run it I dont see data shown in the Console.
"Kerem Gümrükcü" <ka*******@hotmail.comwrote in message
news:u5**************@TK2MSFTNGP06.phx.gbl...
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(IPAddress.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.AcceptTcpClient(); //accept
connection
byteReadStream = new byte[tcpc.Available]; //allocate space
for data
tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
//read data into byte array
Console.WriteLine(Encoding.Default.GetString(byteR eadStream)
+ "\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.comschrieb im Newsbeitrag
news:03**********************************@microsof t.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |
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: 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: 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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |