By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,122 Members | 1,527 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,122 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.Strea m,
ProtocolType.Tcp);

Toserver.Connect(ipep);

sent = sToserver.Send(data, total, dataleft, SocketFlags.None);
Jul 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sun, 15 Jul 2007 16:46:36 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
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.
Jul 16 '07 #2

P: n/a
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.
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sun, 15 Jul 2007 16:46:36 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
> 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.

Jul 16 '07 #3

P: n/a
On Mon, 16 Jul 2007 06:06:30 -0700, Jason <sd****@ertre.comwrote:
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
Jul 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.