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.
Related
How does one connect to a client from a remote server using UDP if all we can get is the global IP (the router's IP)?
I am getting the global IP of the client using the code:
string externalIp = new WebClient().DownloadString("http://icanhazip.com");
Is there a way to get a client's local IP? Also to which IP address should my server connect when using UDP?
What I ended up doing is to use a game service like GameSparks to send TCP and UDP port information from my server to each game client. The game client would then connect to the TCP and UDP port which would give the server knowledge of the client's endpoint. This way a two way communication can be established with both TCP and UDP.
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.
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.
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?
I wanted to learn how networking in c# works, so I learned how to use TCP server and clients.
The only problem is that it's working only if both computers are connected to the same network..
How can I make them communicate even if they aren't?
TCP/IP sockets should work between any two end points as long as there is a route between them. If there is no route between them then you are talking about a case where there are two separate disconnected networks. In that case you will need something to bridge the two networks.
If you are using TCP/IP server/client communication and the computers are on different networks that has a route connecting them and they cannot communicate then you should look at firewall settings and other network settings to make sure TCP/IP packets from one network are able to reach the other network.
Make sure you are using the correct IP address when the client tries to connect to the server. If you have a server at IP address 10.0.0.5 listening on port 4823 try to telnet to that IP address from the client using the server IP address 10.0.0.5 and port 4823. If it connects that usually means that you have things set up right.
From a command prompt: telnet 10.0.0.5 4823
Communication in TCP is done with IP addresses. So even if the client and the server are not on the same network if you specify the IP address of the server, the client will be able to communicate with it (assuming of course the network that the client resides on is configured properly and knows how to reach the server's network). You could also use the DNS service and provide the FQDN of the server instead of an IP address. The DNS server on the client network will resolve the server's FQDN to an IP address.