- 最後登錄
- 2024-4-16
- 在線時間
- 627 小時
- 註冊時間
- 2009-8-17
- 閱讀權限
- 20
- 精華
- 0
- UID
- 6900239
- 帖子
- 854
- 積分
- 201 點
- 潛水值
- 13845 米
| 若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php 本帖最後由 arthurliuliu 於 2015-5-22 07:55 AM 編輯
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- string strHostName = Dns.GetHostName();
- IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);
- IPAddress ip=null;
- // 取得所有 IP 位址
- foreach (IPAddress ipaddress in iphostentry.AddressList)
- {
- // 只取得IP V4的Address
- if (ipaddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
- {
- ip=ipaddress;
- }
- Console.WriteLine("Local IP: " + ipaddress.ToString());
- }
- int a = Int32.Parse(Console.ReadLine());
- if (a == 1)
- {
- server k = new server();
- k.constart(ip.ToString());
- }
- if (a == 2)
- {
- client k = new client();
- String z = Console.ReadLine();
- k.Connect(z, "testmessage");
- }
- }
- class server
- {
- public void constart(String ip)
- {
- TcpListener server = null;
- try
- {
- // Set the TcpListener on port 13000.
- Int32 port = 12000;
- IPAddress localAddr = IPAddress.Parse(ip);
- // TcpListener server = new TcpListener(port);
- server = new TcpListener(localAddr, port);
- // Start listening for client requests.
- server.Start();
- // Buffer for reading data
- Byte[] bytes = new Byte[256];
- String data = null;
- // Enter the listening loop.
- while (true)
- {
- Console.Write("Waiting for a connection... ");
- // Perform a blocking call to accept requests.
- // You could also user server.AcceptSocket() here.
- TcpClient client = server.AcceptTcpClient();
- Console.WriteLine("Connected!");
- data = null;
- // Get a stream object for reading and writing
- NetworkStream stream = client.GetStream();
- int i;
- // Loop to receive all the data sent by the client.
- while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
- {
- // Translate data bytes to a ASCII string.
- data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
- Console.WriteLine("Received: {0}", data);
- // Process the data sent by the client.
- data = data.ToUpper();
- byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
- // Send back a response.
- stream.Write(msg, 0, msg.Length);
- Console.WriteLine("Sent: {0}", data);
- }
- // Shutdown and end connection
- client.Close();
- }
- }
- catch (SocketException e)
- {
- Console.WriteLine("SocketException: {0}", e);
- }
- finally
- {
- // Stop listening for new clients.
- server.Stop();
- }
- Console.WriteLine("\nHit enter to continue...");
- Console.Read();
- }
- }
- class client
- {
- public void Connect(String server, String message)
- {
- try
- {
- // Create a TcpClient.
- // Note, for this client to work you need to have a TcpServer
- // connected to the same address as specified by the server, port
- // combination.
- Int32 port = 12000;
- TcpClient client = new TcpClient(server, port);
- // Translate the passed message into ASCII and store it as a Byte array.
- Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
- // Get a client stream for reading and writing.
- // Stream stream = client.GetStream();
- NetworkStream stream = client.GetStream();
- // Send the message to the connected TcpServer.
- stream.Write(data, 0, data.Length);
- Console.WriteLine("Sent: {0}", message);
- // Receive the TcpServer.response.
- // Buffer to store the response bytes.
- data = new Byte[256];
- // String to store the response ASCII representation.
- String responseData = String.Empty;
- // Read the first batch of the TcpServer response bytes.
- Int32 bytes = stream.Read(data, 0, data.Length);
- responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
- Console.WriteLine("Received: {0}", responseData);
- // Close everything.
- stream.Close();
- client.Close();
- }
- catch (ArgumentNullException e)
- {
- Console.WriteLine("ArgumentNullException: {0}", e);
- }
- catch (SocketException e)
- {
- Console.WriteLine("SocketException: {0}", e);
- }
- Console.WriteLine("\n Press Enter to continue...");
- Console.Read();
- }
- }
- }
- }
複製代碼
我想寫個C#的TCP/IP的連線,這些程式碼幾乎都是從MSDN查到的。
也確實可以在本地端執行
但是我用兩台電腦測試
一台是遠端,一台是我的電腦
但是遠端的當Server就可以成功,而本機當Server就會失敗
上面是錯誤的例外資訊
下面是我自己電腦的遠端設定
請問可能問題會出現在哪裡?
關閉防火牆、防毒軟體後一樣沒有辦法
關閉的流程是:開啟(網路和共用中心),打開(windows防火牆)
,打開(開啟或關閉windows防火牆),公用跟家用皆選擇(關閉windows防火牆(不建議))
另外我在遠端用命令列視窗ping+ip也同樣沒辦法連到我的電腦,封包皆遺失
因為還是不知道問題所在,我決定不做連線了,謝謝各位的幫忙,最後在問一下有可能會是浮動IP的問題嗎
02.發帖/回帖未依規定包住程式碼(-5分)
版主代為編輯。
... |
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊 |