| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2128 人关注过本帖
标题:使用 TCPclient 发送数据问题
只看楼主 加入收藏
ZY137867312
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-4-14
结帖率:0
收藏
 问题点数:0 回复次数:1 
使用 TCPclient 发送数据问题
最近做一个电力系统规约解析的程序,建立一个客户端(服务端不用我设置,有专门软件),既能接收又能发送数据,要求是在建立TCP连接后客户端马上给服务器发送启动报文(680407000000H),然后自动接收上传上来的报文。当我把程序编出来后,用TCP测试软件测试,发现接收数据没有问题,但就是发不下去启动报文,求大神支招。代码为
程序代码:
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 namespace _104规约解析软件通讯
{
    public partial class ClientForm : Form
    {
        public ClientForm()
        {
            InitializeComponent();
            handleSave = new HandleSaveText(AddDataText);
        }
  
        private string data = null;

        private delegate void HandleSaveText(string text);     //定义委托,用于多线程保存文件
        private HandleSaveText handleSave;

        private bool isExit = false;
        private TcpClient tcpclient;
        private NetworkStream netStream;

        //通知一个或多个正在等待的线程已发生事件
        private ManualResetEvent allDone = new ManualResetEvent(false);

        private void Connect()
        {
            tcpclient = new TcpClient(AddressFamily.InterNetwork);
            //引用在相应异步操作完成时调用的方法
            AsyncCallback RequestCallBack = new AsyncCallback(requestCallBack);
                        
            allDone.Reset();//将事件线程设置为非终止状态

            tcpclient.BeginConnect(IPAddress.Parse(ServerIP.Text.Trim()), int.Parse(Port.Text.Trim()), requestCallBack, tcpclient);
            
            allDone.WaitOne();//阻止当前线程,直到alldone.set()之后继续运行
        }

        /// <summary>
        /// 接收数据基础类
        /// </summary>
        public class dataread
        {
            public NetworkStream netstream;
            public byte[] msg;
            public dataread(NetworkStream ns, int buffersize)
            {
                this.netstream = ns;
                this.msg = new byte[buffersize];
            }
        }

        private void requestCallBack(IAsyncResult ar)
        {
            allDone.Set();
            tcpclient = (TcpClient)ar.AsyncState;
      
            if (tcpclient.Connected)
            {
                txtState.Text = "连接服务器成功!";
                int NS = 0, NR = 0;
                netStream = tcpclient.GetStream();//如果远程客户端断开一样可以获得netStream 
                byte[] msg1 = new byte[6] { 0x68, 0x04, 0x07, 0x00, 0x00, 0x00 };               
                
                netStream.Write(msg1, 0, msg1.Length);
                string str = byteToHexStr(msg1);
                NS++;             
                rtb.AppendText("主站发送的报文为:" + str + "\r\n");

                //client.receivebuffersize接收缓冲区的大小
                dataread dr = new dataread(netStream, tcpclient.ReceiveBufferSize);

                //开始接受数据
                netStream.BeginRead(dr.msg, 0, dr.msg.Length, new AsyncCallback(ReadCallBack), dr);
                
             }
                 
            
            else
            {
                txtState.Text = "连接服务器失败,请检查服务器地址或端口!";
            }
         }

        

        public string byteToHexStr(byte[] bytes)//转化成16进制数
        {
            string returnStr = " ";
            Color Color = rtb.SelectionColor;
            SetTextColor(Color.Blue);
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                    if (((i + 1) % 6) == 0)
                        returnStr += "  ";
                }
            }
            return returnStr;
        }

        private void SetTextColor(Color color)
        {
            rtb.Select(rtb.Text.Length, 0);
            rtb.SelectionColor = color;

        }

     
       private void ReadCallBack(IAsyncResult ar)
        {
            try
            {
                dataread dr = (dataread)ar.AsyncState;
                int rec = (ar);

                string Msg = Encoding.UTF8.GetString(dr.msg, 0, rec);

                if (Msg != string.Empty)
                {
                 
                    string str1 = byteToHexStr(dr.msg);
                    rtb.AppendText("主站收到的报文为:" + str1 + "\r\n");

                }

                if (isExit == false)
                {
                    dr = new dataread(netStream, tcpclient.ReceiveBufferSize);
                    netStream.BeginRead(dr.msg, 0, dr.msg.Length, ReadCallBack, dr);
                }
            }
            catch (Exception ex)
            {
                txtState.Text = ex.Message;
            }
        }

        private void connect_Click(object sender, EventArgs e)
        {
            try
            {
                Connect();
                connect.Enabled = false;
                Stopconnect.Enabled = true;
                ServerIP.Enabled = false;
                Port.Enabled = false;
            }
            catch (Exception ex)
            {
                txtState.Text = ex.Message;
            }
        }
  private void Stopconnect_Click(object sender, EventArgs e)
        {
            Stopconnect.Enabled = false;
            connect.Enabled = true;
            ServerIP.Enabled = true;
            Port.Enabled = true;
            tcpclient.Close();
            txtState.Text = "已与服务器断开!";
        }

 }
    
}

新手分数不多,见谅!

[ 本帖最后由 ZY137867312 于 2014-4-16 09:29 编辑 ]
搜索更多相关主题的帖子: 客户端 服务端 服务器 软件测试 
2014-04-16 09:27
ZY137867312
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-4-14
收藏
得分:0 
没人回复吗 ?
2014-04-17 10:48
快速回复:使用 TCPclient 发送数据问题
数据加载中...
 
   



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

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