Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2004
    Messaggi
    120

    [C#] inviare stringhe tra 2 pc tramite internet

    Ciao a tutti.
    sto testando una semplice applicazione per inviare stringhe tramite il protocollo tcp, in rete locale funziona tutto perfettamente, quando invece provo in internet mi esce l'errore Impossibile stabilire la connessione. Rifiuto persistente del computer di destinazione.
    Premetto che ho aperto la porta nel router, ho controllato anche digitando netstat -a, e viene segnata su LISTENING.
    ho provato anche con TELNET ip porta e mi dice impossibile aprire una connessione con l'host..
    da cosa può dipendere?

    SERVER:
    codice:
    string host = Dns.GetHostName();
    IPAddress[] ip = Dns.GetHostAddresses(host);
    
    Console.WriteLine("Host: " + ip[0].ToString());
    TcpListener listener = new TcpListener(ip[0], 31135);
    listener.Start();
    
    Console.WriteLine("Wait for call...");
    Socket s = listener.AcceptSocket();
    
    NetworkStream nt = new NetworkStream(s);
    byte[] strLength = new byte[1], buffer;
    while (true)
    {
           try
           {
                 nt.Read(strLength, 0, 1);
                 buffer = new byte[(int)strLength[0]];
                 nt.Read(buffer, 0, buffer.Length);
                 Console.WriteLine(Encoding.ASCII.GetString(buffer));
            }
            catch { break; }
    }
    
    s.Close();
    Console.WriteLine("Connection Closed\nPress any key to quit.");
    Console.Read();
    CLIENT
    codice:
    TcpClient client = new TcpClient();
    
    Console.WriteLine("Please Wait...");
    IPAddress ip = getWanIp();
    client.Connect(ip, 31135);
    Console.WriteLine("Ready.");
    
    NetworkStream nt = client.GetStream();
    byte[] strLength = new byte[1], buffer;
    while (true)
    {
            try
            {
                  buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Console.ReadLine());
                  strLength[0] = (byte)buffer.Length;
                  nt.Write(strLength, 0, 1);
                  nt.Write(buffer, 0, buffer.Length);
             }
             catch { }
    }
    
    private static IPAddress getWanIp()
    {
            WebClient wc = new WebClient();
            string ip = Encoding.ASCII.GetString((wc.DownloadData("http://whatismyip.com/automation/n09230945.asp")));
            wc.Dispose();
            return IPAddress.Parse(ip);
    }

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Puo' essere un problema di firewall.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    200

    Re: [C#] inviare stringhe tra 2 pc tramite internet

    Originariamente inviato da Rank-1
    ........Socket s = listener.AcceptSocket();

    [/CODE]

    [/CODE]
    Non conosco il Visual C ma io farei:

    TcpClient client = listner.AcceptTcpClient();

    da mettere nel ciclo while true seguito da:

    NetworkStream stream= client.GetStream;

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.