我知道這個話題已經被問到,我已經使用了很多來自互聯網的帖子,比如codeproject,stackoverflow,但是我仍然不能在2臺計算機之間傳輸數據而不是LAN網絡。通過互聯網進行數據傳輸
在LAN上傳輸還是通過互聯網傳輸有區別嗎?我需要臨時服務員來完成這項任務嗎?
我的程序正在研究LAN網絡,但是當我嘗試在WAN網絡中測試程序時,我在我的朋友的計算機中設置了遠離我的服務器(接收),然後在我的計算機中,用我朋友的電腦的IP替換了局域網的IP,但它不起作用。我誤解了任何東西嗎?
如果您不瞭解我,請給我鏈接解決方案或教我如何在2臺不在LAN的計算機上傳輸數據。非常感謝你,對我英語不好的話感到抱歉。
CODE:(只進行連接)
接收:(我設置此我朋友的機器上)
namespace RecieveFile
{
public partial class Form1 : Form
{
#region Variables
private Thread thrDownload;
private Stream strLocal;
private NetworkStream strRemote;
private TcpListener tlsServer;
#endregion
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
thrDownload = new Thread(StartRecieving);
thrDownload.Start();
}
public void StartRecieving()
{
try
{
string hstServer = Dns.GetHostName();
IPAddress ipaLocal = Dns.GetHostEntry(hstServer).AddressList[0];
if (tlsServer == null)
{
tlsServer = new TcpListener(IPAddress.Any, Convert.ToInt32(29250));
}
tlsServer.Start();
TcpClient tclServer = tlsServer.AcceptTcpClient();
MessageBox.Show("Connection has been created");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
strLocal.Close();
strRemote.Close();
MessageBox.Show("In finally");
StartRecieving();
}
}
}
}
發送:(我成立這個在我的機器)
private void Form1_Load(object sender, EventArgs e)
{
//171.255.9.234 is my friend's IP
//if this ip is a ip of computer in my LAN network, it can work
ConnectToSever("171.255.9.234", 29250);
}
public void ConnectToSever(string severIP, int severPort)
{
tcpClient = new TcpClient();
try
{
tcpClient.Connect(severIP, severPort);
MessageBox.Show("Connection has been created!","Message");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
請顯示代碼,否則這只是爲我們猜測。 – deathismyfriend 2014-11-08 18:43:01
假設通過TCP/IP進行通信,從應用程序代碼的角度來看沒有任何區別。有了這裏提供的信息,沒有人真的能夠告訴你很多,但不管它是服務器故障的更多問題,儘管它們也可能拒絕它。 – 2014-11-08 19:45:28
感謝您的通知,我已經添加了代碼,希望我能得到答案。 – 2014-11-09 02:22:16