| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1645 人关注过本帖
标题:我想在局域网内将服务端和客户端连接起来 但是一直连接不上 报出由于目 ...
只看楼主 加入收藏
chenleikkk
Rank: 1
来 自:上海
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-16
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
我想在局域网内将服务端和客户端连接起来 但是一直连接不上 报出由于目标计算机积极拒绝无法连接
服务器端
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using
using
using System.Threading;
using
using System.Collections;


namespace 即时通讯1_1
{
    public partial class Form1 : Form
    {
        private ThreadStart start;
        private Thread listenThread, client_th;
        static private bool m_bListening = false;
        static private MyIP = ("127.0.0.1");
        static private TcpListener listener = new TcpListener(MyIP, 8086);
        private String msg;

        ArrayList clientArray = new ArrayList();

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            start = new ThreadStart(startListen);
            listenThread = new Thread(start);  
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (m_bListening)
            {
                m_bListening = false;
                label4.Text = "Server Idle...";
            }
            else
            {
                m_bListening = true;
                listenThread.Start();
                label4.Text = "Server Listening...";
            }
        }

        private void showmore()
        {
            richTextBox1.Text = msg;
        }
        private void startListen()
        {
            //textBox1.Text = "listening...";
            listener.Start();
            //接收数据
            while (m_bListening)
            {
                //测试是否有数据
                try
                {
                    TcpClient client = listener.AcceptTcpClient();
                    clientArray.Add(client);
                    ParameterizedThreadStart threadStart = new ParameterizedThreadStart(AcceptMsg);
                    client_th = new Thread(threadStart);
                    client_th.Start(client);
                }
                catch (Exception re)
                {
                    MessageBox.Show(re.Message);
                }
            }

            listener.Stop();        //
        }

        private void AcceptMsg(object arg)
        {
            TcpClient client = (TcpClient)arg;
            NetworkStream ns = client.GetStream();
            //StreamReader sr = new StreamReader(ns);//流读写器
            //字组处理
            while (true)
            {
                try
                {
                    byte[] bytes = new byte[1024];
                    int bytesread = ns.Read(bytes, 0, bytes.Length);
                    msg = Encoding.Default.GetString(bytes, 0, bytesread);
                    //显示
                    //MessageBox.Show(msg);
                    showmore();
                    ns.Flush();
                    //ns.Close();
                }
                catch
                {
                    MessageBox.Show("与客户端断开连接了");
                    break;
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            int i=0;
            foreach (TcpClient sd in clientArray) ;

            object a = clientArray[i];
            TcpClient client = (TcpClient)clientArray[i];
            if (client == null)
                return;
            NetworkStream sendStream = client.GetStream();
            String msg = richTextBox2.Text;
            Byte[] sendBytes = Encoding.Default.GetBytes(msg);
            sendStream.Write(sendBytes, 0, sendBytes.Length);
            sendStream.Flush();
           sendStream.Close();
           i++;
        }

      

    }
}

客户端
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using
using
using System.Threading;


namespace 即时通讯1_2
{
    public partial class Form1 : Form
    {
        private TcpClient client;
        private Thread client_th;

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String mystr = richTextBox1.Text;
            string ip = "127.0.0.1";
            this.connect_s(ip);

        }

        public void connect_s(string ip)
        {
            try
            {
                client = new TcpClient(ip, 8086);
                ThreadStart threadStart = new ThreadStart(AcceptMsg);
                client_th = new Thread(threadStart);
                client_th.Start();
                label1.Text = "连接成功";
            }

            catch (System.Exception e)
            {
                label1.Text = e.ToString();
            }
        }

        private void AcceptMsg()
        {
            NetworkStream ns = client.GetStream();
            //StreamReader sr = new StreamReader(ns);//流读写器
            //字组处理
            while (true)
            {
                try
                {
                    byte[] bytes = new byte[1024];
                    int bytesread = ns.Read(bytes, 0, bytes.Length);
                    string msg = Encoding.Default.GetString(bytes, 0, bytesread);
                    //显示
                    //MessageBox.Show(msg);
                    richTextBox1.Text = msg;
                    ns.Flush();
                    //ns.Close();
                }
                catch
                {
                    MessageBox.Show("与服务器断开连接了");
                    break;
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (client == null)
                return;
            NetworkStream sendStream = client.GetStream();
            String msg = richTextBox2.Text;
            Byte[] sendBytes = Encoding.Default.GetBytes(msg);
            sendStream.Write(sendBytes, 0, sendBytes.Length);
            sendStream.Flush();
            //sendStream.Close();
            richTextBox2.Text = "";

        }


    }
}
搜索更多相关主题的帖子: 计算机 客户端 private 局域网 public 
2012-03-16 19:26
chenleikkk
Rank: 1
来 自:上海
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-16
收藏
得分:0 
2012-03-17 15:13
红色警戒
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:19
帖 子:444
专家分:2967
注 册:2005-11-20
收藏
得分:10 
防火墙是不是开着呢?

2012-03-19 09:06
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:10 
目标主机要先监听某端口,其他电脑才能主动去连接

站在春哥的肩膀上
2012-03-19 11:18
快速回复:我想在局域网内将服务端和客户端连接起来 但是一直连接不上 报出 ...
数据加载中...
 
   



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

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