[問題]網路連線

看板C_Sharp作者 (RICO)時間15年前 (2010/08/21 15:59), 編輯推噓1(103)
留言4則, 4人參與, 最新討論串1/1
我最近再玩網路連線時,遇到了一些問題 我的程式 可以自己連自己 可是卻無法使用2台區網內的電腦進行連線 以下是我的CODE ************************ Server端 ************************ private void button1_Click(object sender, EventArgs e) { threadA = new Thread(new ThreadStart(UDPserverBMP)); threadA.Start(); } private void UDPserverBMP() { IPEndPoint ipeq = new IPEndPoint(IPAddress.Any, 5555); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.Bind(ipeq); while (true) { this.Invoke((shoeinformation)delegate { listBox1.Items.Add("Wait....."); }); IPEndPoint Asender = new IPEndPoint(IPAddress.Any, 5555); EndPoint Remote = (EndPoint)Asender; byte[] tmp = new byte[50]; int recv = newsock.ReceiveFrom(tmp, ref Remote); string str = (string)Deserialize(tmp); try { byte[] data = new byte[109182]; Bitmap bmp = new Bitmap(160, 120); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.Black); data = Serialize(bmp); newsock.SendTo(data, Remote); data = null; this.Invoke((shoeinformation)delegate { listBox1.Items.Add("___________________________"); listBox1.Items.Add("Okay!!!"); listBox1.Items.Add("GET"+str); listBox1.Items.Add("接收時所用的"); listBox1.Items.Add(ipeq.Address.ToString()); listBox1.Items.Add(ipeq.Port.ToString()); listBox1.Items.Add(""); listBox1.Items.Add("___________________________"); }); } catch (Exception e) { MessageBox.Show(e.ToString()); } } } *************************************** Client *************************************** private void button1_Click(object sender, EventArgs e) { threadA = new Thread(new ThreadStart(UDPgetBMP)); threadA.Start(); } private void UDPgetBMP() { try { byte[] data = new byte[1024 * 1024 * 8]; IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.3"), 5555); Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); string str="this is client"; byte[] buffer = Serialize((object)str); server.SendTo(buffer, buffer.Length, SocketFlags.None, ipep); IPEndPoint Asender = new IPEndPoint(IPAddress.Parse("192.168.1.3"), 5556); EndPoint Remote = (EndPoint)ipep; int rece = server.ReceiveFrom(data, ref Remote); object tmp = Deserialize(data); this.Invoke((showinformation)delegate { pictureBox1.Image = (Bitmap)tmp; }); } catch (Exception e) { MessageBox.Show(e.ToString()); } } ************************ 在區網內進行連線時就無法連線 懇請各位大大們為我解答 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.137.182

08/21 18:02, , 1F
你都已經用try catch了 好歹也PO個Exception的錯誤訊息唄
08/21 18:02, 1F
錯誤訊息是 遠端主機已強制關閉一個現存的連線。... ※ 編輯: laevatain103 來自: 140.128.137.182 (08/21 18:35)

08/21 20:52, , 2F
為什麼選用UDP而不用TCP @@?
08/21 20:52, 2F

08/21 21:39, , 3F
你可以下斷點跑一次程式...
08/21 21:39, 3F

09/04 14:10, , 4F
改用異步吧,改用socket 會比較好
09/04 14:10, 4F
文章代碼(AID): #1CRuT6Kl (C_Sharp)