找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
認識好友、聊天,分享生活趣事你準備好成為出色的版主了嗎?安全提問(回答) 和 永久尊貴會員 事宜
蘿莉無碼流出cosplayge催眠中文mg
public t[mvg 040絲襪クロア×yvain’s緋彈的亞mg

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

[繁]怪物轉生 Re:Mons

【高清繁中】✡ 霹靂

【超清繁中】✡ 霹靂

[繁中]霹靂天機貳 仙

1月新番[繁體]最弱魔

✡ 斗破蒼穹 年番/鬥
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 3117|回復: 5
打印上一主題下一主題

[求助]C# TCP/IP[複製鏈接]

Rank: 2Rank: 2

帖子
854
積分
201 點
潛水值
13845 米
跳轉到指定樓層
樓主
發表於 2015-5-12 12:17 AM|只看該作者|倒序瀏覽
若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php
本帖最後由 arthurliuliu 於 2015-5-22 07:55 AM 編輯
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;

  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string strHostName = Dns.GetHostName();
  13.             IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);
  14.             IPAddress ip=null;
  15.             // 取得所有 IP 位址
  16.             foreach (IPAddress ipaddress in iphostentry.AddressList)
  17.             {
  18.                 // 只取得IP V4的Address
  19.                 if (ipaddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  20.                 {
  21.                     ip=ipaddress;                    
  22.                 }
  23.                 Console.WriteLine("Local IP: " + ipaddress.ToString());
  24.             }
  25.             int a = Int32.Parse(Console.ReadLine());                       
  26.             if (a == 1)
  27.             {
  28.                 server k = new server();
  29.                 k.constart(ip.ToString());
  30.             }
  31.             if (a == 2)
  32.             {
  33.                 client k = new client();
  34.                 String z = Console.ReadLine();
  35.                 k.Connect(z, "testmessage");
  36.             }
  37.         }
  38.         class server
  39.         {
  40.             public void constart(String ip)
  41.             {
  42.                 TcpListener server = null;
  43.                 try
  44.                 {
  45.                     // Set the TcpListener on port 13000.
  46.                     Int32 port = 12000;
  47.                     IPAddress localAddr = IPAddress.Parse(ip);

  48.                     // TcpListener server = new TcpListener(port);
  49.                     server = new TcpListener(localAddr, port);

  50.                     // Start listening for client requests.
  51.                     server.Start();

  52.                     // Buffer for reading data
  53.                     Byte[] bytes = new Byte[256];
  54.                     String data = null;

  55.                     // Enter the listening loop.
  56.                     while (true)
  57.                     {
  58.                         Console.Write("Waiting for a connection... ");

  59.                         // Perform a blocking call to accept requests.
  60.                         // You could also user server.AcceptSocket() here.
  61.                         TcpClient client = server.AcceptTcpClient();
  62.                         Console.WriteLine("Connected!");

  63.                         data = null;

  64.                         // Get a stream object for reading and writing
  65.                         NetworkStream stream = client.GetStream();

  66.                         int i;

  67.                         // Loop to receive all the data sent by the client.
  68.                         while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  69.                         {
  70.                             // Translate data bytes to a ASCII string.
  71.                             data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  72.                             Console.WriteLine("Received: {0}", data);

  73.                             // Process the data sent by the client.
  74.                             data = data.ToUpper();

  75.                             byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

  76.                             // Send back a response.
  77.                             stream.Write(msg, 0, msg.Length);
  78.                             Console.WriteLine("Sent: {0}", data);
  79.                         }

  80.                         // Shutdown and end connection
  81.                         client.Close();
  82.                     }
  83.                 }
  84.                 catch (SocketException e)
  85.                 {
  86.                     Console.WriteLine("SocketException: {0}", e);
  87.                 }
  88.                 finally
  89.                 {
  90.                     // Stop listening for new clients.
  91.                     server.Stop();
  92.                 }


  93.                 Console.WriteLine("\nHit enter to continue...");
  94.                 Console.Read();
  95.             }
  96.         }
  97.         class client
  98.         {
  99.             public void Connect(String server, String message)
  100.             {
  101.                 try
  102.                 {
  103.                     // Create a TcpClient.
  104.                     // Note, for this client to work you need to have a TcpServer
  105.                     // connected to the same address as specified by the server, port
  106.                     // combination.
  107.                     Int32 port = 12000;
  108.                     TcpClient client = new TcpClient(server, port);

  109.                     // Translate the passed message into ASCII and store it as a Byte array.
  110.                     Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

  111.                     // Get a client stream for reading and writing.
  112.                     //  Stream stream = client.GetStream();

  113.                     NetworkStream stream = client.GetStream();

  114.                     // Send the message to the connected TcpServer.
  115.                     stream.Write(data, 0, data.Length);

  116.                     Console.WriteLine("Sent: {0}", message);

  117.                     // Receive the TcpServer.response.

  118.                     // Buffer to store the response bytes.
  119.                     data = new Byte[256];

  120.                     // String to store the response ASCII representation.
  121.                     String responseData = String.Empty;

  122.                     // Read the first batch of the TcpServer response bytes.
  123.                     Int32 bytes = stream.Read(data, 0, data.Length);
  124.                     responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  125.                     Console.WriteLine("Received: {0}", responseData);

  126.                     // Close everything.
  127.                     stream.Close();
  128.                     client.Close();
  129.                 }
  130.                 catch (ArgumentNullException e)
  131.                 {
  132.                     Console.WriteLine("ArgumentNullException: {0}", e);
  133.                 }
  134.                 catch (SocketException e)
  135.                 {
  136.                     Console.WriteLine("SocketException: {0}", e);
  137.                 }
  138.                 Console.WriteLine("\n Press Enter to continue...");
  139.                 Console.Read();
  140.             }
  141.         }
  142.     }
  143. }
複製代碼

我想寫個C#的TCP/IP的連線,這些程式碼幾乎都是從MSDN查到的。
也確實可以在本地端執行
但是我用兩台電腦測試
一台是遠端,一台是我的電腦
但是遠端的當Server就可以成功,而本機當Server就會失敗

上面是錯誤的例外資訊
下面是我自己電腦的遠端設定


請問可能問題會出現在哪裡?
關閉防火牆、防毒軟體後一樣沒有辦法
關閉的流程是:開啟(網路和共用中心),打開(windows防火牆)
,打開(開啟或關閉windows防火牆),公用跟家用皆選擇(關閉windows防火牆(不建議))
另外我在遠端用命令列視窗ping+ip也同樣沒辦法連到我的電腦,封包皆遺失

因為還是不知道問題所在,我決定不做連線了,謝謝各位的幫忙,最後在問一下有可能會是浮動IP的問題嗎
02.發帖/回帖未依規定包住程式碼(-5分)
版主代為編輯。


...
瀏覽完整內容,請先 註冊登入會員
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊
分享分享0收藏收藏0支持支持0

使用道具檢舉

hatakc56923us 該用戶已被刪除
頭香
發表於 2015-5-12 01:19 PM|只看該作者
回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。
looks like a connection timeout error, firewall?
若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。

使用道具檢舉

Rank: 3Rank: 3Rank: 3

帖子
317
積分
1000 點
潛水值
16095 米
3
發表於 2015-5-12 02:21 PM|只看該作者
如果你忘記伊莉的密碼,請在登入時按右邊出現的 '找回密碼'。輸入相關資料後送出,系統就會把密碼寄到你的E-Mail。
你先確認你所設定的port有沒有開放,或是被檔,
你這情況沒意外應該是port被檔掉。

簡易的確認方法很簡單,
請將server與client的防毒軟體、防火牆都全部關掉,
然後再跑一次你的程式,我想應該會沒問題。
分享使你變得更實在,可以使其他人感到快樂,分享是我們的動力。今天就來分享你的資訊、圖片或檔案吧。

使用道具檢舉

Rank: 2Rank: 2

帖子
541
積分
212 點
潛水值
13707 米
4
發表於 2015-5-12 08:19 PM|只看該作者
若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。
你要同時在一台主機測試Server和Client的連接
首先你必須在這台主機上執行Server端和Client端程式
以你這個程式為例,就是要執行兩次,第一次是設定為Server端
如下圖所示,這台主機的IP是192.168.0.100 (IPV4)

輸入1按Enter則出現"Waiting for a connection..訊息表示已經等待連線了
注意!!如果有出現防火牆新增例外的訊息,要選擇允許!

再來就是在執行本程式一次,輸入2按Enter
...
瀏覽完整內容,請先 註冊登入會員
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊
如果發覺自己無法使用一些功能或出現問題,請按重新整理一次,並待所有網頁內容完全載入後5秒才進行操作。

使用道具檢舉

Rank: 2Rank: 2

帖子
541
積分
212 點
潛水值
13707 米
5
發表於 2015-5-12 08:23 PM|只看該作者
如果瀏覽伊莉時速度太慢或無法連接,可以使用其他分流瀏覽伊莉,www01.eyny.com(02,03)。

排列錯誤,從貼一次。




附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊
所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。

使用道具檢舉

帖子
372
積分
21 點
潛水值
5911 米
6
發表於 2015-5-13 09:17 AM|只看該作者
所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。
在測試可以先用Intranet內部網先測試,可以先避開分享器的防火牆問題,Client & Server 電機防火牆先關閉,確認沒有問題後再開啟測試。

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部