NP304 3onedata C# socket send directly from server to device. - c#

How can I send data from my C# socket program to my clients?
Currently I have two programs, server and client.
I start my server, then my clients connect to server, in this way they are connected together, but now I want to send directly from server to client?
As my client cannot start its own connection, of course I have IP and port of my client, how can I send data from my server to a client with known IP and port?
Thanks.

Since your clients are not computer programs you can control, you really have no chance but to contact them from the server. If your clients can handle TCP communications, you need to treat them as servers, and Connect from the server to each client (open the socket on the server side, and Connect to each client IP and port).
It's possible that your clients understand UDP and not TCP. That is actually going to be easier for you, as you only need to create one UDP socket, and use SendTo to send a data to each client (one SendTo call per client).
Let's just hope your clients aren't stuck on the Ethernet level...

I would say that COM-ports are slightly easy to communicate than implementing TCP/IP protocol on your device. Could your device read/write its COM-port?

Related

How I can send message from server to client using the port?

I am using Tcplistener to listen on a port for requests. When the requests come in from the client I want to know the client ip making the request.
the problem is : the server and all client take the same Ip
so I decided to send massage from server to client by port ... I search a lot on google But I did not knew how I can send message from server to client using the port ? so can any body help me ?
The clients are not listening for incoming connections, so the only way for the server to send a message to a client is by using an existing connection, that was previously established by the client.
You don't have to specify the port number when sending a message on an existing connection, your server can just use:
Socket handlerSocket = tcpListener.AcceptSocket();
// a connection is now established, "handlerSocket" can be used for both receiving and sending messages
handlerSocket.Send(...);
Your clients currently send data and immediately close the connection though, you'll have to modify them to read data from the NetworkStream in order to receive anything.
There are tons of tutorials available, I think you just googled the wrong keywords. Have you tried something like c# chat tutorial?
You are running them all in the same computer. That is why the IP is always the same.
SOP for this kind of thing is to have two ports: a control port and a data port.
The way this works is:
The control port is used to initiate the connection.
Upon successful connection, the server and the client negotiate a set of ports to be used for the data (local server data port and remote client data port).
The server establishes a connection to the remote client data port from the local server data port.
The client issues a request for data (e.g. command) through the control port.
The server sends the information back through the data port.

C# - Socket router for passing sockets to multiple socket servers

is it possible to have one socket router that would pass incoming sockets to socket servers?
I need this because I want to have multiple servers for handling sockets but only one port for clients to connect to. so if one of the servers goes down, router would send the socket to other healthy socket servers.
Is this even possible and how? or any other solution for my problem?
Yes this is definitely possible, and is used very widespread.
Basically you need to have one TCP server running on that one port you want clients to connect to. Then you can listen for connections. When a connection is established you can reroute packets to other servers (you choose).
Listen on a port with your main server
Accept connections as they come in.
When a connection is opened, read the data in another thread. Take that data and send it to the other server you want to.
Basically, it is a proxy.

TCP Sockets connecting to a server on two different ports

I have a custom TCP Server listening on port 5888(dummy port). The proxy server listens for incoming connections. When the proxy receives HTTP request for certain pages, it should relay it to the main server on port 80.For other page requests the proxy is required to send data to the main Server on port 8081.
The port 80 is used to service the HTML Pages where as the port 8081 is used for streaming data to the clients.
I am able to receive the incoming connections on the proxy and then read the data from the clients. After reading the data, I can determine which port to connect to on the main server for sending the data.
I am stuck at deciding how to connect on 2 ports for sending the data from the clients to the Main Server?
In that case you either need 2 socket connection objects to the same IP on the different ports (this is legal), or you have one connection object which reconnects according to the port you have to deal with.
Depending on how often you have to switch connections the latter version might have a huge overhead, plus the first one allows you to send data to both ports virtually simultaneously.
You need to stop thinking of your program as a server. After you have received the connection, read the data, and decided what port to send it to, shift gears and start operating as a client would.
Just open a new connection to "localhost" on either port 80 or 8081 and re-send the data you received as if you were the original client.
Your client is connected to the proxy server on port 5888 so no matter from what real server (Web or streaming) you take the data, you are going to provide the data to the client using port 5888 only.
It seems to be a not so practical solution. I am assuming here that you are trying to achieve a kind of control port and data port structure where one port is controlling the streaming from another port.
Just creating two sockets is sufficient for obtaining data from two servers. Here you will have to manually create a protocol which your client understands as you are going to provide both html and streaming data to the client using single port.

Different port Socket C# for sending and receive

i'm using UDP async socket in C#.net and i want to make the server and a client communicate in different port for sending and receive,
the server send to client with port A and receive from client with port B
the client receive from server with port A and send data to server with port B
is it possible??
In the client bind the socket to port A, and in the client bind to port B. It's as simple as that. The server shouldn't really know A, but get it from the messages it receives messages from the client (using something like ReceiveFrom.
Remember that using UDP, the client has to be the first to send messages, otherwise it's not really a client-server system but more distributed system.
This is exactly how it happens already. The source port for a client is a random port chosen by the OS.
It's not possible to do this, since an endpoint consists of only one IP address and one port number. You would need to use two different sockets and establish two connections with the server in order to use port A and port B.
If you were using TCP rather than UDP as part of the constructor of the TCPClient you can specify which EndPoint you want the outgoing connection to use.

UDP client-server question

I'm trying to write a client-server console application on C# using UDP.
And i have one question.
So, when i send command from client to server - servers must have a read IP adress.
client must get some settings from server,but in this case client must have real IP adress too. Other application like games do not require client real IP. What must do I to it work in such a way?
Games either use the host computer with a public IP as a server or the server itself as a server.
A client with private IP establishes a connection with a server and the server then uses the connection to return data to a client (similar to browsers).
In C# you could use the NetworkStream class for TCP and UdpClient for UDP.
If I understand your question correctly: you could use Socket.ReceiveFrom, which will tell the server the IP address of the client who is sending a request.
Using Socket.BeginReceiveFrom / EndReceiveFrom the server gets the IP address of the client. If the server needs to reply, it uses the IPEndPoint.

Categories