Form1.cs
上传用户:bjelec
上传日期:2015-05-18
资源大小:30k
文件大小:4k
源码类别:

网络编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.Threading;
  10. using System.Net.Sockets;
  11. namespace client
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private IPAddress myIP = IPAddress.Parse("127.0.0.1");
  16.         private IPEndPoint MyServer;
  17.         private Socket sock;
  18.         private bool flag = true;
  19.         
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.      
  25.         private void targett()
  26.         {
  27.             Control.CheckForIllegalCrossThreadCalls = false;
  28.             toolStripLabel1.Text = "与服务器端建立连接!";
  29.             try
  30.             {
  31.                 while (flag)
  32.                 {
  33.                     byte[] r_data = new byte[64];
  34.                     sock.Receive(r_data, r_data.Length, 0);
  35.                     string str = System.Text.Encoding.BigEndianUnicode.GetString(r_data);                   
  36.                     listBox1 .Items .Add (str.Substring(1, str.Length - 1) );
  37.                 }
  38.             }
  39.             catch (Exception ex)
  40.             {
  41.                 MessageBox.Show(ex.Message);
  42.             }
  43.         }
  44.         private void sendmsg(string sendtext)
  45.         {
  46.              try
  47.             {
  48.                 byte[] s_data = new byte[64];
  49.                 s_data = System.Text.Encoding.BigEndianUnicode.GetBytes(sendtext.ToCharArray ());
  50.                 sock.Send(s_data, s_data.Length, 0);
  51.             }
  52.             catch
  53.             {
  54.                 MessageBox.Show("连接尚未建立!无法发送!");
  55.             } 
  56.         }
  57.         private void Connect()
  58.         {
  59.                      try
  60.             {
  61.                 MyServer = new IPEndPoint(myIP, 6001);
  62.                 sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  63.                 sock.Connect(MyServer);
  64.                 Thread thread = new Thread(new ThreadStart(targett));
  65.                 thread.Start();
  66.             }
  67.             catch (Exception ex)
  68.             {
  69.                 MessageBox.Show(ex.Message);
  70.             }
  71.         }
  72.         private void button1_Click(object sender, EventArgs e)
  73.         {
  74.             Connect();
  75.             
  76.         }
  77.         private void button3_Click(object sender, EventArgs e)
  78.         {
  79.             listBox1.Items.Clear();
  80.         }
  81.         private void button2_Click(object sender, EventArgs e)
  82.         {
  83.             sendmsg(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1.Text);
  84.             listBox1.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1.Text);
  85.             richTextBox1.Clear();
  86.         }
  87.         //private void button4_Click(object sender, EventArgs e)
  88.         //{
  89.         //    openFileDialog1.InitialDirectory ="c:\" ;
  90.         //    openFileDialog1.Filter = "全部文件|*.*|文本文件|*.txt";
  91.         //    if (openFileDialog1.ShowDialog() == DialogResult.OK)
  92.         //    {
  93.         //      textBox1.Text = openFileDialog1.FileName;
  94.         //      FileStream fs = new FileStream(textBox1.Text.ToString (), FileMode.OpenOrCreate);
  95.         //      textBox2.Text = fs.Length.ToString ()+"K";            
  96.         //    }
  97.         //}
  98.         //private void button5_Click(object sender, EventArgs e)
  99.         //{
  100.         //    Socket sensock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  101.         //    IPEndPoint ipep  = new IPEndPoint(myIP, 6001);
  102.         //    try
  103.         //    {
  104.         //        if (textBox1.Text == "")
  105.         //        {
  106.         //            MessageBox.Show("文件名不能为空");
  107.         //        }
  108.         //        else
  109.         //        {
  110.         //            FileStream fs = new FileStream(textBox1.Text.ToString(), FileMode.OpenOrCreate);
  111.         //            BinaryReader r = new BinaryReader(fs);
  112.         //            byte[] postArray = r.ReadBytes((int)fs.Length);
  113.         //            sensock.Send(postArray);
  114.         //        }
  115.         //    }
  116.         //    catch (SocketException se)
  117.         //    {
  118.         //        MessageBox.Show(se.Message);
  119.         //    }
  120.         //}
  121.     }
  122. }