标题:c#网络编程问题,求助!!!把所有的分都给了!!
只看楼主
caixiaoyi101
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-5-10
结帖率:0
 问题点数:0 回复次数:3 
c#网络编程问题,求助!!!把所有的分都给了!!
服务器端是一个火灾报警系统的控制界面,客户端是一个现场场景的仿真界面,现场场景有烟雾探测器和手动报警器,当有火灾发生时,向服务器端的控制界面发送火灾消息,然后服务器端根据情况给出解决方案,把解决方案的信号发送给客户端。

现场场景有两个烟雾探测器和一个手动报警器,当他们被触发时发送消息给服务器端,也就是等效于客户端有三个按钮,按下这三个按钮时发送不同的消息给服务器,使服务器端能判定到底是哪个按钮被按下了,而且不用等服务器响应(发送解决方案给客户端)。

当客户端没有请求连接时,服务器端不会因为一直在监听有无客户端请求连接而不能执行其他操作,也就是监听不会影响客户端界面的操作,是否要用thread来实现??

场景和控制界面自己弄的差不多了,就差通信了。
哪位大侠帮帮我~~给我个解决方案呀~~

对于创建socket或是tcpclient,connect,listen,accept等我有点了解,但是不知道应该怎么用thread、怎样按不同的按钮发送不同的消息,现在脑子里一团浆糊。。。
搜索更多相关主题的帖子: 网络 
2010-05-10 21:25
caixiaoyi101
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-5-10
得分:0 
问题补充:我应该用同步TCP还是异步TCP呢?
用tcpclient,tcplistener好?还是用socket好?
2010-05-10 21:36
caixiaoyi101
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-5-10
得分:0 
这是我网上找到的服务器和客户端程序,但是发送数据好像不成功,不知道是什么原因??


服务器:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using
using System.Threading;

namespace 服务器x
{
    public partial class Form1 : Form
    {
        int num = 0;
        //导入程序中使用到的名字空间
        private Socket socketForClient;
        private NetworkStream networkStream;
        private TcpListener tcpListener;
        private StreamWriter streamWriter;
        private StreamReader streamReader;
        private Thread _thread1;
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            num++;
            if (num <= 10)
            {
                this.pictureBox1.BackColor = Color.Red;
            }
            if ((num > 10) && (num < 20))
            {
                this.pictureBox1.BackColor = Color.DimGray;
            }
            if (num == 20)
            {
                num = 0;
            }  
        }
        private void Listen()
       {
         //创建一个tcpListener对象,此对象主要是对给定端口进行侦听
           tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"),8000);
        //开始侦听
           tcpListener.Start();
        //返回可以用以处理连接的Socket实例
           socketForClient = tcpListener.AcceptSocket();
         try
          {
          //如果返回值是”true”,则产生的套节字已经接受来自远方的连接请求
            if (socketForClient.Connected)
              {
                  MessageBox.Show("已经和客户端成功连接!");
                 while(true)
                 {
                  //创建networkStream对象通过网络套节字来接受和发送数据
                     networkStream = new NetworkStream(socketForClient);
                  //从当前数据流中读取一行字符,返回值是字符串
                  streamReader = new StreamReader(networkStream);
                  string receive= streamReader.ReadLine();
                  if (receive == "hello!")
                  {
                      this.timer1.Enabled = true;
                  }

                 streamWriter = new StreamWriter(networkStream);
                  //string send= "hello!";
                  //往当前的数据流中写入一行字符串
                  //streamWriter.WriteLine(send);
                  //刷新当前数据流中的数据
                  //streamWriter.Flush();
                 }
               }
            }
           
         catch (Exception ey)
         {
             MessageBox.Show(ey.ToString());
         }
      }

      

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("服务已经启动!");
            _thread1 = new Thread(new ThreadStart(Listen));
            _thread1.Start();
                     
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            num++;
            if (num <= 10)
            {
                this.pictureBox2.BackColor = Color.Red;
            }
            if ((num > 10) && (num < 20))
            {
                this.pictureBox2.BackColor = Color.DimGray;
            }
            if (num == 20)
            {
                num = 0;
            }  
        }
        private void Form1_Closing(object sender, e)
        {
            //关闭线程和流
            networkStream.Close();
            streamReader.Close();
            streamWriter.Close();
            _thread1.Abort();
            tcpListener.Stop();
            socketForClient.Shutdown(SocketShutdown.Both);
            socketForClient.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //关闭线程和流
            networkStream.Close();
            streamReader.Close();
            streamWriter.Close();
            _thread1.Abort();
            tcpListener.Stop();
            socketForClient.Shutdown(SocketShutdown.Both);
            socketForClient.Close();

        }

    }
}


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

namespace 客户端x
{
    public partial class Form1 : Form
    {
        private NetworkStream networkStream;
        private StreamReader streamReader;
        private StreamWriter streamWriter;
        TcpClient myclient;

        
        public Form1()
        {
            InitializeComponent();
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
          try
           {
               IPAddress localIP = IPAddress.Parse("127.0.0.1");
              IPEndPoint local=new IPEndPoint(localIP,8000);
              myclient = new TcpClient(local);
              myclient.Connect(IPAddress.Parse("127.0.0.1"),8000);

           }
          catch
           {
             MessageBox.Show ("Connect error!!" );
             return ;
           }
           //创建networkStream对象通过网络套节字来接受和发送数据
            networkStream = myclient.GetStream();
            streamReader = new StreamReader(networkStream);
            streamWriter = new StreamWriter(networkStream);
            MessageBox.Show("可发送数据");
          }         

           

            

        
        private void Form1_Closing(object sender, e)
        {
            try
            {
                streamReader.Close();
                streamWriter.Close();
                networkStream.Close();
                myclient.Close();

            }
            catch { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {

                //往当前的数据流中写入一行字符串
                streamWriter.WriteLine("hello!");
                //刷新当前数据流中的数据
                streamWriter.Flush();
            }
            catch(Exception ee)
            {
                MessageBox.Show("从服务器端读取数据出现错误,类型为:" + ee.ToString());
             }

        }


        private void button3_Click(object sender, EventArgs e)
        {
         
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
      
           
        }

        
      


      
    }
}
2010-05-10 21:40
zhouxiaoga
Rank: 1
等 级:新手上路
帖 子:10
专家分:3
注 册:2009-5-12
得分:0 
  用同步的
服务器(正版).rar (69.66 KB)
2010-06-13 15:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-306115-1-1.html




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

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