473,398 Members | 2,113 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,398 software developers and data experts.

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

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
3 10260
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MBW | last post by:
The following code is for a simple server/client asplication that allows the user to toggle between serve and or client modes and send/recieve a message however i am getting an Attribute error on...
1
by: wobbles | last post by:
Hi Everyone, I have a server which reads information from a "server.exe.config" file and a client that also reads from its "client.exe.config" file. If the server location or the port number...
1
by: mike g | last post by:
Hello, I have postgres set to log all connections. I run it on port 5432. I have noticed in the postgres logs that the port being used to connect to the database is logged but never equal to...
4
by: Stefka | last post by:
Hi all, I try to implement a python xml-rpc server and call it from a php client. If the server and the client are on the same machine (localhost) the communication between them is just fine....
9
by: xparrot1 | last post by:
I know that I can get the SERVER port number like this: HttpContext.Current.Request.ServerVariables My question is how do I get the remote CLIENT port number? Thanks Derek
1
by: Vesna Sarajevo | last post by:
Hi all! I have to pass a SOAP message to a server, and I know only server IP and the port number. I was gonna do it from a form, using WebRequest Class and StreamWriter to fetch the result....
0
by: ankuragt | last post by:
(problem in writing a string into a file on client side.) hey i have written a code of server and client.what i want to do is to transfer the contents of file on server side to the client side (line...
3
by: Hukkky | last post by:
I'm testing simple server/client codes on linux. just server can wait for client's connect sign and accept, and client can't connect to server, this is all. There's no problems just for this...
0
by: rotaryfreak | last post by:
Hello, Im building a client-server application and the user needs to input the hostName and the portNumber the client application and needs to send the portnumber to the server application. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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
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,...
0
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...
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
isladogs
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 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.