c# TCP server client - How to send and recieve on the same port number

J

Jason

Hello

I've got some test code that I've found on the web that is a TCP server and
a TCP client. The server sends data to the client on the port i specify. I
know because get the port number through an IPEndPoint. However when I send
the data back to the server, the server receives it on a different port
number then what I specify, and I can't figure out why.

In my server I use this bit code to create the endpoint and socket. I've
got the server machine set to 192.168.1.3 and the client to 192.168.1.2

Any ideas??
Thanks
Jason
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5567); // my port #

Socket newsock = new Socket(AddressFamily.InterNetwork,

SocketType.Stream, ProtocolType.Tcp);

newsock.Bind(ipep);

newsock.Listen(10);

Console.WriteLine("Waiting for a client...");

Socket client = newsock.Accept();

IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;

Console.WriteLine("Connected with {0} at port {1}",newclient.Address,
newclient.Port);



As I said the client recieves the data on port 5567 but when I send it back
to the server the server get it on some odd port number



Client code that sends data back to the server.

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.3"), 5567);

Socket Toserver = new Socket(AddressFamily.InterNetwork,SocketType.Stream,
ProtocolType.Tcp);

Toserver.Connect(ipep);

sent = sToserver.Send(data, total, dataleft, SocketFlags.None);
 
P

Peter Duniho

I've got some test code that I've found on the web that is a TCP server
and
a TCP client. The server sends data to the client on the port i
specify. I
know because get the port number through an IPEndPoint. However when I
send
the data back to the server, the server receives it on a different port
number then what I specify, and I can't figure out why.

From the limited amount of code you posted, it appears to me that you have
this exactly backwards. That is, the server receives the data on the port
number you assigned it, while it sends data to the client on a different
port number. This is, in fact, normal.

Note that the "RemoteEndPoint" you get when you accept the client
connection is the address of the client, not the server.

In the code you posted, the address the server sees
(client.RemoteEndPoint) is the client's address, and includes the port to
which the server sends data (ie not the one you've specified), while the
address the client uses is the server's address, and includes the port on
which the server receives data (ie the port you did specify).

You didn't post any code that would bind the client to a specific port.
If you don't bind to a specific port, then on the first network operation
(for example, calling Connect()), the Socket will be assigned an arbitrary
port. The server will see this arbitrary port as the client's port upon
accepting the connection.

This is normally exactly what you want to do. The only reason you should
ever bind to a specific port is when the socket bound to that specific
port has to receive communications that have been initiated elsewhere.
This is very typical of a server, and is almost never the case for a
client.

Both the client and the server have their own port numbers. These do not
need to be the same, and in practice almost never are. The server will
have a specific port number, required in order for arbitrary clients to
find and connect to the server, but the client will have some "random"
(not really random, but it might as well be) port number assigned to it
when it begins communication with the server.

Pete
 
P

Peter Duniho

From the limited amount of code you posted, it appears to me that you
have this exactly backwards. That is, the server receives the data on
the port number you assigned it, while it sends data to the client on a
different port number. This is, in fact, normal.

And just to be clear: when I use the word "on" here, I am talking about
the destination port. There are always two ports involved -- the sending
port and the receiving (or destination) port -- but because your original
post discussed sending data "to" a port, I'm looking only at the
destination port.
 
J

Jason

OK

So what I've got on the end of my server, is a hardware device, that
probably uses some type of embedded C++ OS. I'm supposed to send some data
to the end client which is on port 5567 at an ip of 192.168.1.2

I'm told that once the end client receives data it will send back the exact
same data to the sender. Since the end client is expecting data on port
5567 wouldn't it send it back on that same port as well? Do I have the
server just accept any incoming connection, or do I really care in this
setup? The reason I created the client is to make sure the sever part is
working. I guess as long I send data out on that port 5567, thats all I
need to worry about.
 
P

Peter Duniho

So what I've got on the end of my server, is a hardware device, that
probably uses some type of embedded C++ OS.

Is the hardware device important? It doesn't sound to me as though it
is. It sounds as though the server communicates with the hardware device
via some other mechanism unrelated to the communications between your
server and client. If it's not important, we should ignore it.
I'm supposed to send some data
to the end client which is on port 5567 at an ip of 192.168.1.2

Is there an actual requirement that the client be on port 5567? Or are
you simply supposed to send data to the client?
I'm told that once the end client receives data it will send back the
exact
same data to the sender.

No problem.
Since the end client is expecting data on port
5567 wouldn't it send it back on that same port as well?

Who says that the client is expecting data on port 5567? It is connecting
to a server that is using port 5567, but that does not mean that the
client is using port 5567, nor does it mean that the client needs to be
using port 5567 in order to receive the data.

Also, both the server and the client send and receive data using their
respective ports. However, the server and client need not be using the
same port number, and in most applications they do not. As I said,
usually only the server has a predetermined port, and the client winds up
with whatever auto-assigned port the OS gives it.

So, the server sends and receives data using port 5567. But the client
sends and receives data using whatever port was assigned to it. It sends
data back using the same port with which it received data, and in neither
case is that port 5567. Again, at least based on the code you posted, the
only end of the connection that is using port 5567 is the server.
Do I have the
server just accept any incoming connection, or do I really care in this
setup?

I don't see anything fundamentally wrong with the code you posted, if
that's what you're asking. Assuming your client receives data, and
assuming the server receives the response that the client sends, there's
no problem.

Pete
 
J

Jason

No, the server will communicate to the end unit via a crossover cable. What
ever data the end unit receives on that port, it will send it right back out
on that port, and I need to receive it, to makes sure it sends back the
correct data.
 
P

Peter Duniho

No, the server will communicate to the end unit via a crossover cable.
What
ever data the end unit receives on that port, it will send it right back
out
on that port, and I need to receive it, to makes sure it sends back the
correct data.

"No" what?

To what are you referring when you write "no"? The actual physical
connection between your server and client is irrelevant, so I'm at a loss
as to why you brought that up. (I will point out that if you would learn
to usefully use the standard "quote-then-reply" convention, rather than
top-posting, it would be easier to figure out what you're responding to).

Nothing you've written indicates a) that there's anything broken, and b)
that you comprehend anything I've written.

I recommend that you do a little more studying about how TCP/IP works,
using other resources, since it's clear I'm not able to present the
information in a way that you can understand. That may be my fault, and
if so I'm sorry for that. But until you get past the idea that the client
and server need NOT have the same port number, you aren't going to make
any progress on this question.

You may want to start here: http://tangentsoft.net/wskfaq/

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top