I am testing my socket programs at home, in local network.
Server and client programs are running on seperate machines.
Server program socket is binded as: serverSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8999));
Client program (on the other computer) is connected as: clientSocket.Connect(IPAddress.Parse("192.168.2.3"), 8999);
Why can Client not communicate with server?
Do I need to make some firewall configuration or something like that?
Or am I writing Server Ip incorrectly to the Client? (I got it from cmd->ipconfig of server)
You are only binding to local 127.0.0.1 IP therefore your server would be accessible only from the same machine. Try the following:
serverSocket.Bind(new IPEndPoint(IPAddress.Any), 8999));
Because your server is binding to the localhost loop-back address 127.0.0.1. This means nothing except what's running on the server can communicate with the socket.
You need to:
verify the server has a network connection on the same subnet as the client (192.168.2.0 or 192.168.0.0) - call it the "public" IP address
bind your socket to the server's "public" IP address or bind your socket to all interfaces - usually with the special IP address 0.0.0.0
Related
I have a synchronous TCP server and client application the works absolutely fine on two separate host machines.
What I'd like to know is what IP and port do I bind the server socket and the client socket to when the applications are both running on the same host machine.
I can't find any solid information on Google about this.
When I try and use my network IP which was 192.168.0.32 I get an error that says the Host actively refused the connection.
I cannot find any reasonable information about this error.
Can I listen and send on the same Port?
What IP address should I use to bind the server and the client, when both applications are running on the same machine?
Thanks for your time.
In order to run both client and server applications on the same host you should bind your server socket to localhost (you can actually write "localhost" it's a preserved word or 127.0.0.1 ) and address it from the client as well.
Localhost allways refers to the computer you work on.
If you'd like to access your server from a machine which is outer to your local network using your network ip you've mentioned, you should first search for "IP FORWARDING" option in your router settings and forward incomming requests to the machine where the server is running on.
Or (my favourite) use the great IP TUNNELING service of ngrok. You can find it here https://ngrok.com/
good luck.
So the answer to this question is that I must bind to my loop back address with separate ports for the client and the server !!
The IP address could be the loopback 127.0.0.1 for both, or your IP address, I don't see why it would not work.
The port on the other hand has to be the same for it to work, assuming the client application doesn't also listens to the port that you "bind" it to.
You have to tell the server on which port it should listen. The client then has to send data on the same port for the server to get the information.
This example should get you going: https://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
I make a TCP/IP server in C# and client too. TCP-server is broadcasting packets to clients.
But it only broadcast on local IP not on other computers/machines.
All computers on connected SN MP server. I also changed IP address in code from (127.0.0.0) to network IP address (SNMP server IP Address). I pinged others IP with my computer; it's working but not making a connection with my TCP-server that I have made in C#.
Can you help me in this scenario?
Can't give a proper answer without seeing some code, but for starters, you can check whether the port is blocked in firewall or something (try to allow incoming connections in firewall for the specific port (whitelist it) )
I have C# TCP Server program which is running into VMWare and network type is NAT. There TCP server works as a http server, so that I can connect it using browser. Now when I am connecting it using this url http://localhost:33333/OrderDetails, it works without any problem. But when I am trying to connect it from outside of VMWare using this link http://1.39.37.243:33333/OrderDetails (1.39.37.243 is my PC's public ip), then its not working. I can't change the network type NAT, because my customer's office Network system is also NAT system. Where am I doing it wrong ? Any thoughts or suggestion will be highly appreciated.
This isn't working because the machine receiving the TCP packets has no reason to forward them to the VM. In other words, this isn't working because you haven't done anything to make it work.
You need to configure VMWare's NAT to forward these incoming TCP connections to the VM.
You could also have other problems:
The server might not be listening on the right IP address. For example, if the listening socket is bound to localhost, only localhost connections will work.
There might be something else in the path keeping the TCP packets from getting to the host. For example, if the host itself is behind NAT, port forwarding may need to be configured in the router.
Port forwarding won't work from inside the LAN unless the router supports hairpinning. If the host itself is behind NAT, test from outside the LAN after setting up port forwarding in the router.
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.
I have a question.
I would like to create a socket listener, but the listen address is from a remote server.
So say SERVER A has the socket listener.
SERVER B (ex. IP = 123.456.78.23:1970) has the listen port.
If I create the socket like this (and execute it on server A):
IP EndPoint endpointIp = new IPEndPoint(123.456.78.23, 1970);
this._socket = new Socket(endpointIp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this._socket.Bind(endpointIp); // crashes here
this._socket.Listen(MaxPendingConnections);
The program crashes on the Bind code: "The requested address is not valid in its context".
How can I listen on a remote port?
Thank you!
You can't listen to a port on one computer from another. (Hence the message "not valid in this context")
What you CAN do is write one program/service that runs on SERVER B and listens to the port, and use Remoting or WCF to write ANOTHER program that runs on SERVER A that monitors your service on SERVER B rather than trying to listen on a port on SERVER B.
You can't open a socket using an IPEndPoint (basically IP and port) of a different server. What you are asking about is server spoofing - pretending to be a different server and receiving the traffic addressed to it. You will need to configure server A to have the IP of server B, and make sure every router on the way to it can reach server A with this IP.
Edit: Following your comment on the original question, what you need is a load balancer. In order to allow for server B to take over when server A is dead, you must have a router or a server that receives all traffic and directs it to server A, then if it senses that server A is down will redirect all traffic to server B. Of course since you already are doing load balancing, might as well make it a real load balancer, forwarding the traffic to both servers while both are up, and to the remaining one when the other one fails.