'How to connect two computer on Internet using Sockets in C#
I want to send data from PC-01 to PC-B, two computer connected on internet. Is it possible to write socket program in c#? I try to search lot, all most example on local computer or using static ip address.
Computer 1 -> Internet Service Provider (having IP: 116.203.188.231) -> Broadband Router -> PC-01/PC-02
Computer 2 -> Internet Service Provider (having IP: 49.202.72.175) -> Broadband Router -> PC-A/PC-B
I try this:
On server:
m_socListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8000);
m_socListener.Bind(ipLocal);
m_socListener.Listen(4);
m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
On Client:
m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(49.202.72.175);
int iPortNo = System.Convert.ToInt32(8000);
IPEndPoint ipEnd = new IPEndPoint(ip.Address, iPortNo);
m_socClient.Connect(ipEnd);
EnableCommands(false);
//watching for data
Above code have no problem, but it is not working. When trying to connect it saying "No connection could be made because the target machine actively refused it".
I checked firewall and allow the application & port for both system.
IP of connecting computer is: 116.203.188.231 and IP of another computer is 49.202.72.175. The IP 49.202.72.175 is provided by ISP (it is not static ip and also this may share by other user from isp's end) and also more than computer connected to internet through 49.202.72.175 using local lan. So can I connect the computer using socket? Or is there any technology to connect two computer?
Thanks in advance.
Solution 1:[1]
Yes, of course you can
If you want to send it through socket you can see an example here :
http://code.msdn.microsoft.com/windowsdesktop/Communication-through-91a2582b
Also, your firewall is probably blocking this port. You can change the port to 80, which is usualy open, or set an exaption in all firewalls involved
This is nice if you always know the ip of the other computer
Otherwise, I recommand to use a server that will connect between them
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |