0

I want to create a simple messaging app that uses tcp protocol to communicate, including with multiple people. Messages get sent to the server, which distributes them to all other clients. At the moment, I have it fully functioning and it works perfectly - on a local computer and a local network, using the ipv4 address.

After an extensive Google search, I discovered that to communicate from a different network I need to port-forward my server. However, how do I make my server able to communicate with clients without all the clients port-forwarding? As far as I'm aware, not everybody's device is port-forwarded.

So, how do I implement this? Is it possible with just C#? Or did I misunderstand something, and port-forwarding isn't really required?

Thanks for all the help.

Ironstone
  • 91
  • 2
  • 2
  • 10
  • Whoever voted to close this as too broad needs to re-read this post. – Ryan Wilson Oct 22 '19 at 16:40
  • You do not need port forwarding. All clients should be using the same port number to connect to the server. You need application software at your server to take all incoming messages and send to the other clients. – jdweng Oct 22 '19 at 16:42
  • @jdweng Sorry if I misunderstood, but how does the server send it to all the clients if he can't access them via sockets or ports? – Ironstone Oct 22 '19 at 16:44
  • A server is a listener that accepts connections from one or more clients. So the server has a socket for each client all using the same port number but each client has a different IP. A socket connection has three properties 1) Source IP address 2) Destination IP address 3) Port number. So each client connects to server with the same Destination IP address and Port number. The client IP address is the machine where the connection start (source IP). So your server while send a message to each of the clients. Each client is a different socket. – jdweng Oct 22 '19 at 17:05
  • @jdweng Ah OK, thanks. So if a client connects to the server, he can send information back to them? In which case, do I need to port-forward the server? Additionally, will this work with the inbuilt Tcp Listener and TcpClient classes? – Ironstone Oct 22 '19 at 17:07
  • Once a client connects to a server the server can send back on same connection. Port Forwarding has nothing to do with straight connection between a client and server. You will use the standard Net Libraries for TCP Client and Server/Listener. – jdweng Oct 22 '19 at 17:30
  • @jdweng Thanks a lot, but if portforwarding isn't required then which IP address do I put for the TcpClient? The public IP address? – Ironstone Oct 22 '19 at 17:44
  • Not sure why you keep are referring to port forwarding. When you make a connection you need three items 1) Source IP address 2) Destination IP address 3) Port number. The listener will be started at the destination IP address is the same port number as client. Port Forwarding uses more than on port number. You only have one port number. – jdweng Oct 22 '19 at 17:49
  • @jdweng Sorry then, I guess I misunderstood certain Google search results. So to access the Tcp Listener with a TcpClient from another network do I have to use the public IP address? I should probably mention that the Tcp Listener is just on a normal computer that is part of a network. – Ironstone Oct 22 '19 at 17:55
  • @jdweng If I try to use the TcpClient to connect to the TcpListener via the public IP address, the client throws an error saying the connection has been denied - even when I turn my firewall off. Is this a router issue? – Ironstone Oct 22 '19 at 18:38
  • Two things 1) What is you local endpoint for the client 2) Can you from cmd.exe >Ping Server Try both server name and IP. The error can occur for multiple reasons A) Server is not running B) Not route to server. That is why trying PING 3) The connection already exists 4) Port Number is blocked. Make sure you use a port number that is above 1000. I usually recommend > 10,000. – jdweng Oct 22 '19 at 18:58
  • @jdweng When I try to Ping, it says it doesn't exist, so I assume the server doesn't work? But it works on local Ipv4. As for the client, it crashes before it can print it. – Ironstone Oct 22 '19 at 19:04
  • Try ping with both IP and then with Computer Name. Also check cmd.exe >IPCONFIG/ALL and check IP and Mask. It is possible the mask it wrong. Does machine have more than one Ethernet card? Ping only checks if the machine is running and not any app. Another remote possibility is you have two machines with the same IP. – jdweng Oct 23 '19 at 09:05
  • @jdweng Sorry, I messed up last time, I put the port in too for no obvious reason. Both pings (IPv4 and public IP) were successful with 0% loss. Also, my port number is 12021, which I don't think is used for anything? When I ping my PC it pings an "Ethernet adapter VMware Network Adapter VMnet8" IPv6 address. – Ironstone Oct 23 '19 at 14:14
  • The new windows software has a address[0] as IP V6 and addrress[1] as IP V4 : string name = System.Net.Dns.GetHostName(); System.Net.IPAddress[] addr = System.Net.Dns.GetHostAddresses(name); – jdweng Oct 23 '19 at 15:10
  • @jdweng Okay, but sorry how does that affect connection issues? – Ironstone Oct 23 '19 at 16:32
  • You want to use the IP4 address, not IP6. I assumed you were taking the first index of the array and that is why you are failing. Is it a WIFI connection? I had an issue a couple of years ago where the machine and WIFI were different and the connection failed. – jdweng Oct 23 '19 at 16:47
  • @jdweng oh sorry, I'm not using the Dns methods, I'm manually parsing it in the client. Is this something I was supposed to do in the server? – Ironstone Oct 23 '19 at 17:01
  • Parsing the Server IP address in the client is perfect. DNS is if the client and server are on the same machine. You have ping working so there is a route from client to server. So you need to have server listening which you can verify on server from cmd.exe >Netstat -a and check if port is listening. Then there is no reason the client shouldn't be able to connect to server unless the port is blocked or being used. You should also be able to check on client that port is working using from cmd.exe >Netstat -a. – jdweng Oct 23 '19 at 19:30
  • @jdweng Used Netstat -a on the server, and it comes up with '0.0.0.0:12021', and the status 'LISTENING', which I assume means it's working? However, connecting to it with the client it says 'No connection could be made as the target machine actively refused it'. I feel like I should mention that when testing both the server and the client are on the same machine. – Ironstone Oct 26 '19 at 09:37
  • Can you ping from server to client? Try ping with using computer name instead of IP? From cmd.exe >IPCONFIG/ALL. What is IP address and mask? (on both client and server)? How are the client and server connected? To debug you can ping any routers in between client and server to isolate where things stop working. – jdweng Oct 26 '19 at 10:46

1 Answers1

0

Why don't you use SignalR. I think it will be best for your problem.

kailashdesiti
  • 165
  • 1
  • 13
  • I was considering this, but I didn't really understand how to implement it with pure C#. The tutorial on the official website shows it using web pages and I don't plan to utilise these, but if there is a way to use just C# could you please tell me where I could find this? – Ironstone Oct 22 '19 at 16:41
  • Please refer below post for simple implementation of SignalR with console app.https://stackoverflow.com/questions/11140164/signalr-console-app-example – kailashdesiti Oct 22 '19 at 16:44
  • Thanks a ton, this looks like it might help – Ironstone Oct 22 '19 at 16:48