這是client端程式,可以跟server連接

說明如下文:

---------------------------------------------------------------

 

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.Threading;



namespace Client__GUI
{
    public partial class Form1 : Form
    {
        byte[] data = new byte[1024]; //用來做數據的緩衝區
        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8765);//指定接收位址--server
       // IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.50.130"), 8765);//指定接收位址--server
        int recv = 0;//判斷有無接受到資料
        string ipAddress = "127.0.0.1";
        int port = 8765;
        TcpClient tcpClient = null;

        NetworkStream netStream=null ;

        public Form1()
        {
            InitializeComponent();
            Listen_button.Enabled = true;
            stop_button.Enabled = false;
            exit_button.Enabled = false;
            send_button.Enabled = false;


        }
        private void Form1_Closed(object sender, System.EventArgs e)
        {

            byte[] writeData = Encoding.Unicode.GetBytes("Client is closed");
            IAsyncResult ar = netStream.BeginWrite(writeData, 0, writeData.Length, null, null);
            netStream.EndWrite(ar);
            tcpClient.Close();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Listen_button_Click(object sender, EventArgs e)
        {
            tcpClient = new TcpClient();
            tcpClient.Connect(ipAddress, port);

            tcpClient.NoDelay = false;//等資料多一點才會送,不會一點點資料就送出去

            netStream = tcpClient.GetStream();
            Listen_button.Enabled = false;
         //送出去的訊息
            string message = "Hello,I am colin from c#";
            byte[] writeData = Encoding.Unicode.GetBytes(message);
            IAsyncResult ar = netStream.BeginWrite(writeData, 0, writeData.Length, null, null);
            if (!ar.AsyncWaitHandle.WaitOne())
            {
                label1.Text = "errors connection";
              
            }
            netStream.EndWrite(ar);


            if (netStream.CanRead)
            {
                label1.Text = "can read";
            }

            byte[] data = new byte[1024];
        
            data = new byte[1024];

            if (netStream.CanRead)
            {
                // Reads NetworkStream into a byte buffer.
                
                byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
                recv = (int)tcpClient.ReceiveBufferSize;
                // Read can return anything from 0 to numBytesToRead.  
                // This method blocks until at least one byte is read.
                netStream.Read(bytes, 0, recv);

                // Returns the data received from the host to the console.
                string returndata = Encoding.UTF8.GetString(bytes);
                label1.Text = "message from server: " + returndata;


            }
            else
            {
                label1.Text = "You cannot read data from this stream.";
               // Console.WriteLine("You cannot read data from this stream.");
                tcpClient.Close();

                // Closing the tcpClient instance does not close the network stream.
                netStream.Close();
                return;
            }
                stop_button.Enabled = true;
                exit_button.Enabled = true;
                send_button.Enabled = true;


        }

        private void stop_button_Click(object sender, EventArgs e)
        {
            string exit = "Socket is closed";
            Listen_button.Enabled = true;
            stop_button.Enabled = false;
            exit_button.Enabled = false;
            send_button.Enabled = false;

            byte[] writeData = Encoding.Unicode.GetBytes(exit);
            IAsyncResult ar = netStream.BeginWrite(writeData, 0, writeData.Length, null, null);
            if (!ar.AsyncWaitHandle.WaitOne())
            {
                label1.Text = "errors connection";
                //  ErrorLog.WriteError("fail to wait");
            }
            netStream.EndWrite(ar);
            label1.Text = "";
            recv = 0;
            Thread.Sleep(300);
            tcpClient.Close();
           

        }

        private void send_button_Click(object sender, EventArgs e)
        {
            if (recv != 0)//要接受到server端的message,才能用send_button
            {
                byte[] writeData = Encoding.Unicode.GetBytes(input_textBox.Text);
                IAsyncResult ar = netStream.BeginWrite(writeData, 0, writeData.Length, null, null);
                if (!ar.AsyncWaitHandle.WaitOne())
                {
                    label1.Text = "errors connection";
                    //  ErrorLog.WriteError("fail to wait");
                }
                netStream.EndWrite(ar);
               // tcpClient.Close();
            }
           
            else
            {
                MessageBox.Show(this, "press the Listen button please.", "Error");
            }
           
            input_textBox.Text="";
        }

        private void exit_button_Click(object sender, EventArgs e)
        {
            string exit = "exit";
            byte[] writeData = Encoding.Unicode.GetBytes(exit);
            IAsyncResult ar = netStream.BeginWrite(writeData, 0, writeData.Length, null, null);
            if (!ar.AsyncWaitHandle.WaitOne())
            {
                label1.Text = "errors connection";
                //  ErrorLog.WriteError("fail to wait");
            }
            netStream.EndWrite(ar);
            Thread.Sleep(300);
            this.Close();

        }

      

    }
}

文章標籤
創作者介紹

colin異世界

colin 發表在 痞客邦 PIXNET 留言(0) 人氣()