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

网络编程

开发平台:

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. using System.IO;
  12. namespace server
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         private IPAddress myIP = IPAddress.Parse("127.0.0.1");
  22.         private IPEndPoint MyServer;
  23.         private Socket seSock;
  24.         private bool flag = true;
  25.         private Socket reSock;
  26.       
  27.         private void targett()
  28.         {
  29.             Control.CheckForIllegalCrossThreadCalls = false;
  30.             try
  31.             {
  32.                 if (reSock.Connected)
  33.                 {
  34.                     while (flag)
  35.                     {
  36.                         toolStripLabel1.Text = "与客户端建立连接!";
  37.                         byte[] r_data = new byte[64];
  38.                         reSock.Receive(r_data, r_data.Length, 0);
  39.                         string str = System.Text.Encoding.BigEndianUnicode.GetString(r_data);
  40.                         listBox1 .Items .Add (str.Substring(1, str.Length - 1));
  41.                     }
  42.                 }
  43.             }
  44.             catch (Exception ex)
  45.             {
  46.                MessageBox.Show(ex.Message);
  47.             }
  48.         }
  49.         private void button1_Click(object sender, EventArgs e)
  50.         {
  51.             try
  52.             {
  53.                 myIP = IPAddress.Parse("127.0.0.1");
  54.             }
  55.             catch
  56.             {
  57.                 MessageBox.Show("你输入的IP地址格式不正确,请重新输入!");
  58.             }
  59.             try
  60.             {
  61.                 if (seSock != null)
  62.                 {
  63.                     seSock.Close();
  64.                 }
  65.                 MyServer = new IPEndPoint(myIP, 6001);
  66.                 seSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  67.                MessageBox.Show("主机" + "127.0.0.1" + "端口" + "6001" + "等待连接……");
  68.                 seSock.Bind(MyServer);
  69.                 seSock.Listen(50);
  70.                 reSock = seSock.Accept();
  71.                 Thread thread = new Thread(new ThreadStart(targett));
  72.                 thread.Start();
  73.             }
  74.             catch (Exception ex)
  75.             {
  76.                 MessageBox.Show(ex.Message);
  77.             }
  78.         }
  79.         private void sendmsg(string send)
  80.         {
  81.             try
  82.             {
  83.                 byte[] s_data = new byte[64];
  84.                 s_data = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray ());
  85.                 reSock.Send(s_data, s_data.Length, 0);
  86.             }
  87.             catch
  88.             {
  89.                 MessageBox.Show("连接尚未建立!无法发送!");
  90.             }
  91.         }
  92.         private void button2_Click(object sender, EventArgs e)
  93.         {
  94.             listBox1.Items.Add(DateTime .Now .ToString ("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1 .Text );
  95.             sendmsg(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1 .Text .ToString ());
  96.             richTextBox1.Clear();
  97.         }
  98.         private void button3_Click(object sender, EventArgs e)
  99.         {
  100.             listBox1.Items.Clear();
  101.         }
  102.         //private void button4_Click(object sender, EventArgs e)
  103.         //{
  104.         //    openFileDialog1.InitialDirectory = "c:\";
  105.         //    openFileDialog1.Filter = "全部文件|*.*|文本文件|*.txt";
  106.         //    if (openFileDialog1.ShowDialog() == DialogResult.OK)
  107.         //    {
  108.         //        textBox1.Text = openFileDialog1.FileName;
  109.         //        FileStream fs = new FileStream(textBox1.Text.ToString(), FileMode.OpenOrCreate);
  110.         //        textBox2.Text = fs.Length.ToString() + "K";
  111.         //    }
  112.         //}
  113.         //private void button5_Click(object sender, EventArgs e)
  114.         //{
  115.         //    Socket sensock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  116.         //    IPEndPoint ipep = new IPEndPoint(myIP, 6001);
  117.         //    try
  118.         //    {
  119.         //        if (textBox1.Text == "")
  120.         //        {
  121.         //            MessageBox.Show("文件名不能为空");
  122.         //        }
  123.         //        else
  124.         //        {
  125.         //            FileStream fs = new FileStream(textBox1.Text.ToString(), FileMode.OpenOrCreate);
  126.         //            BinaryReader r = new BinaryReader(fs);
  127.         //            byte[] postArray = r.ReadBytes((int)fs.Length);
  128.         //            sensock.Send(postArray);
  129.         //        }
  130.         //    }
  131.         //    catch (SocketException se)
  132.         //    {
  133.         //        MessageBox.Show(se.Message);
  134.         //    }
  135.         //}
  136.      
  137.        
  138.     }
  139. }