| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 339 人关注过本帖
标题:局域网里实现P2P的收发信功能,但是发送总是不成功,恳请各位高手帮忙
取消只看楼主 加入收藏
fantare
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-3-9
收藏
 问题点数:0 回复次数:0 
局域网里实现P2P的收发信功能,但是发送总是不成功,恳请各位高手帮忙
新手,刚开始学编程。想实现一个在局域网里的2台或以上的电脑之间任意发送以及接收数据,网上找了很多资料,自己修改了多次,但就是不成功,从一台电脑向另一台电脑发送数据的时候总是报错,说是连接超时,不知道问题出在哪里,请各位高手指点迷睛!谢谢

using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using
using
using System.Threading;
using

namespace SuperSimulator
{
    public partial class form1 : Form
    {
        public form1()
        {
            InitializeComponent();
        }
        private void eventRead_Click(object sender, EventArgs e)
        {
            try
            {
                localIf.Items.Clear();
                eventInput();
                localInfo();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

 
        }

        /* ------------------------数据读入-------------------------------*/  
        
        SortedList jobList = new SortedList();
        private void eventInput()
        {
            string line;
            string[] field;
             reader = new (@"C:/sample.txt", Encoding.Default);
            eventShow.Items.Clear();

            while (!reader.EndOfStream)
            {
                //lineはsample.txtの毎行
                line = reader.ReadLine();
                //,で分割してfield[]に保存
                field = line.Split('/');
                //以,分割成2大部分,field[0]是时间作为key,field[1]是工作内容及其它内容作为value,存入sortedlist
                //之后传递到其它client之后,可以用其它分割符对field[1]进一步分割
                jobList[field[0]]=field[1];               
            }
            reader.Close();

            int n = 1;
            foreach (DictionaryEntry jobLine in jobList)
            {
                string time=(string)jobLine.Key;
                string job = (string)jobLine.Value;

                eventShow.Items.Add(n.ToString()+": "+time+"/"+job);
                n++;
            }
            
        }

        /* ------------------------本地信息-------------------------------*/  

        static string hostName = Dns.GetHostName();
        static IPAddress[] adrList = Dns.GetHostAddresses(hostName);
        IPAddress localIp = adrList[adrList.Length - 1];

        private void localInfo()
        {
            localIf.Items.Add(hostName);
            localIf.Items.Add(localIp);        
        }


        /* ------------------------退出-----------------------------------*/  

        private void exit_Click(object sender, EventArgs e)
        {
            
            Close();

        }

        

        /* ------------------------建服务器-----------------------------------*/

        const int portNo = 1111;   
        bool listenerRun = true;
        string message;
        Thread th;

        TcpListener listener;

        private void Listen()
        {
            try
            {
                listener = new TcpListener(localIp, portNo);
                listener.Start();

                while (listenerRun)
                {
                    Socket s = listener.AcceptSocket();
                    Byte[] data = new Byte[s.ReceiveBufferSize];//struct
                    int i = s.Receive(data);
                    message = System.Text.Encoding.UTF8.GetString(data);
                    simuLog.AppendText(message + '\n');
                }
            }
            catch (System.Security.SecurityException se)
            {
                MessageBox.Show(se.ToString());
                simuLog.AppendText(se.ToString() + '\n');

            }
            catch (Exception)
            {
                simuLog.AppendText("Unconnected");
            }
        }
               
        private void setServer_Click(object sender, EventArgs e)
        {
            
            setServer.Enabled = false;
            unConnect.Enabled = true;
            th = new Thread(new ThreadStart(Listen));
        }

        private void Stop()
        {
            listener.Stop();
            th.Abort();
            Stop();
        }
                     
         
               
        private void simuSt_Click(object sender, EventArgs e)
        {
            string[,] jobInfo=new string[jobList.Count,2];
            int i=jobList.Count,n=0;

            //把集合jobList的数据复制到另一数组jobInfo中,便于控制
            foreach (DictionaryEntry jobLine in jobList)
            {            
                jobInfo[n,0]=(string)jobLine.Key;
                jobInfo[n,1]=(string)jobLine.Value;
               
                n++;              
            }

            jobList.Clear();

            //将jobInfo中的每条数据发送,并进行控制
            //for (int t = 0; t < i;t++ )
            //{
            //    SendMessage(jobInfo[t,0] + "/" +jobInfo[t,1]);
            SendMessage("hello");
            //    //这里加入等待回复程序,即可以控制一条一条的输出

            //    simuLog.AppendText(jobInfo[t, 0] + "の" + jobInfo[t, 1] + "発送した" + '\n');
            //}
            
        }
        const string IP1 = "10.0.1.6";
        const string IP2 = "10.0.1.14";
        
        
        
        public void SendMessage(string message)
        {
            try
            {               
                TcpClient tcpc = new TcpClient(IP1, portNo);
                NetworkStream tcpStream = tcpc.GetStream();
                              
                StreamWriter reqStreamW = new StreamWriter(tcpStream);
                reqStreamW.Write(message);
                reqStreamW.Flush();               
                tcpStream.Close();
                tcpc.Close();               
                simuLog.AppendText(message);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        

        public void Disconnect()
        {
            Stop();
        }

        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            Disconnect();
            Close();
        }

        private void unConnect_Click(object sender, EventArgs e)
        {
            setServer.Enabled = true;
            unConnect.Enabled = false;
            listenerRun = false;
            

        }
        

        


    }
}

搜索更多相关主题的帖子: 成功 局域网 
2011-03-09 14:30
快速回复:局域网里实现P2P的收发信功能,但是发送总是不成功,恳请各位高手帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054037 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved