Arduino server.write() or server.print() to specific port?

Is it Possible in the Arduino Ethernet class to designate a port over which the server sends its data? I have a client connected over port 9798 which sends a string to the server. The very same string needs to be sent back over the same port. How can i achieve this?

have you checked the doc?

https://www.arduino.cc/en/Reference/Ethernet

Yes but i cant find a solution in there :frowning:

this seems to meet your needs:


Ethernet : Server class

print()

Description

Print data to all the clients connected to a server. Prints numbers as a sequence of digits, each an ASCII character (e.g. the number 123 is sent as the three characters '1', '2', '3').

Syntax

server.print(data)
server.print(data, BASE)

Parameters

data: the data to print (char, byte, int, long, or string)

BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).

Returns

byte
print() will return the number of bytes written, though reading that number is optional

Sadly not. The problem is that i cant specify the port over which the server sends data to the client.

https://www.arduino.cc/en/Reference/EthernetServer

ServerPort

You don't tell a server to send something to a specific port,
you let the server serve that specific port.

then you want a client and you need a server on the other side to listen.

Yes i did. But whenever i want to send an answer back, according to wireshark it goes to another port. I tried server.write() and server.print(). I got the server listening to port 9798. A client sends a string my programm buffers it and after some actions from the arduino i want to send it back over 9798. Is that not possible?

Problem is that i cant change "the other side". It has to be a client.

You should investigate how ports work.

In the connection process to your specified port,
the client tells the server on which port it should respond, IIRC.

That is in no way connected to the port the server is using.

if the server listens on 9798 it doesn't mean the client has to use 9798 on its side. what is your client?

Ok so my client is a Program from work. I have been told it sends out a string and waits for the same string on the same port to come back so it can resume its work. Keep in mind i am not that experienced with TCP/IP so maybe i dont quite get something hence why i am asking for help :slight_smile: .

I think that was not what was said, but it's worth asking again.
If it is really as you describe, you need an additional client.

What happens if you just echo the received data (which should be a string?)?.

I triple checked the string that was sent out im sure its not the data. So you are saying that i would need another client that is connected to the same server in order to achieve what im looking for? Can you connect two clients so that they communicate over one port? Im 100% confident that was said to me but then again maybe the confusion lies elsewhere :).

I don't understand what you want to express.

If you can not use the requesting path to deliver the answer,
you will have to make a connection back to the requestor.

So what was once a server that could have replied,
becomes a client that wants to deliver the answer via a request to a server.

You did not answer my question.

That would be the task of the client calling you, by providing an identical port for the back traffic.

TCP is just a bidirectional binary stream,
server and client are roles in connection build up.
After a connection has been established,
both sides are basically the same.

However, protocols inside the TCP stream (like HTTP/FTP/...)
demand a special behavior of the connected nodes,
often defined and or described in RFCs.
They are often bound to well-known ports.

Are you accepting a connection from the client before you communicate with the client? Server.print, as the documentation says, will print data to all the clients connected to your server, which doesn't seem to be what you want. You probably want to call Client.print (call the print method on the client object that you received from calling server.accept).

TCP has many 'service' packets. you see them in Wireshark. the first packet sent back to a client is a service packet, not data. forget Wireshark. It is too low level.

show us the sketch you use

Thanks everyone. It seems i had another problem.

But that is secret?

You made us share the problem, but not the solution?

Why? To demotivate us to help on the next problem you have?