相忘于江湖

不抛弃,不放弃... 请给我勇敢,改变可以改变的;请给我坚强,接受不可以改变的;请给我智慧,分辨这两者。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题。

源代码:

发送消息的窗口代码

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace TCPSocket
{
    public partial class FrmTcpSend : Form
    {
        public FrmTcpSend()
        {
            InitializeComponent();
        }

        private void buttonSendFile_Click(object sender, EventArgs e)
        {
           // TcpClient tcpClient = new TcpClient(textBoxHostName.Text, Int32.Parse(textBoxPort.Text));
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(IPAddress.Parse(textBoxHostName.Text), Int32.Parse(textBoxPort.Text));

            NetworkStream ns = tcpClient.GetStream();

            FileStream fs = File.Open("..\\..\\FrmTcpSend.cs", FileMode.Open);

            int data = fs.ReadByte();

            while (data != -1)
            {
                ns.WriteByte((byte)data);
                data = fs.ReadByte();
            }

            fs.Close();
            ns.Close();
            tcpClient.Close();

        }

        private void buttonSendMessage_Click(object sender, EventArgs e)
        {
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(IPAddress.Parse(textBoxHostName.Text), Int32.Parse(textBoxPort.Text));

            NetworkStream ns = tcpClient.GetStream();


            if (ns.CanWrite)
            {
                Byte[] sendBytes = Encoding.UTF8.GetBytes(textBoxMessage.Text);
                ns.Write(sendBytes, 0, sendBytes.Length);
            }
            else
            {
                MessageBox.Show("不能写入数据流", "终止", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                //Console.WriteLine("You cannot write data to this stream.");
                tcpClient.Close();

                // Closing the tcpClient instance does not close the network stream.
                ns.Close();
                return;
            }


            ns.Close();
            tcpClient.Close();

        }
    }
}

 

 

接收消息的窗口代码

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace TcpReceive
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //Thread thread = new Thread(new ThreadStart(Listen));

            Thread thread = new Thread(new ThreadStart(SocketListen));

            thread.Start();

            IPAddress ipAddress = IPAddress.Any; //IPAddress.Parse("172.16.102.11");


            this.Text = ipAddress.ToString()+"正在监听...";

        }

        protected delegate void UpdateDisplayDelegate(string text);


        public void Listen()
        {
            IPAddress ipAddress = IPAddress.Any; //IPAddress.Parse("172.16.102.11");


          
           
            TcpListener tcpListener = new TcpListener(ipAddress,9999);
            tcpListener.Start();

            TcpClient tcpClient = tcpListener.AcceptTcpClient();

          
            NetworkStream ns = tcpClient.GetStream();

            StreamReader sr = new StreamReader(ns);

            string result = sr.ReadToEnd();

            Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object[] { result });

            tcpClient.Close();
            tcpListener.Stop();
        }

        public void SocketListen()
        {
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(new IPEndPoint(IPAddress.Any, 9999));

            listener.Listen(0);

            Socket socket = listener.Accept();
            Stream netStream = new NetworkStream(socket);
            StreamReader reader = new StreamReader(netStream);

            string result = reader.ReadToEnd();
            Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object[] { result });

            socket.Close();
            listener.Close();


        }

        public void UpdateDisplay(string text)
        {
            richTextBox1.Text = text;
        }
    }
}

posted on 2011-05-03 09:27  playman0211  阅读(3353)  评论(0编辑  收藏  举报