In pratica ho un router Atlantis ma non riesco lo stesso a collegarmici con un programma in C#, anche con la porta aperta e il firewall disattivato![]()
posto il codice del programma se può servire:
Codice PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
class TCP
{
static string localIP = "192.168.1.102";
static int port = 789;
public static string GetRemoteIP()
{
try
{
string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
IPAddress externalIp = null;
externalIp = IPAddress.Parse(requestHtml);
string string_externalIp = externalIp.ToString();
return string_externalIp;
}
catch
{
return null;
}
}
public static void accConn()
{
/*
TcpListener listener = new TcpListener(IPAddress.Any, 7000);
Console.WriteLine(GetRemoteIP());
listener.Start();
listener.AcceptSocket();
*/
EndPoint ep = new IPEndPoint(IPAddress.Parse(localIP), port);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(ep);
listener.Listen(5);
listener.Accept();
//listener.Start();
//listener.AcceptSocket();
Console.WriteLine("Connessione ACCETTATA!!!");
Console.ReadKey();
}
public static void Conn()
{
TcpClient client = new TcpClient();
client.Connect(GetRemoteIP(), port);
Console.WriteLine("Connessione EFFETTUATA!!!");
Console.ReadKey();
}
}
static void Main(string[] args)
{
Thread listener = new Thread(TCP.accConn);
Thread client = new Thread(TCP.Conn);
listener.Start();
Console.WriteLine("Listener avviato");
client.Start();
Console.WriteLine("Client avviato");
Console.ReadKey();
}
}
}

Rispondi quotando