Form1.cs
上传用户:bjelec
上传日期:2015-05-18
资源大小:30k
文件大小:5k
- 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.Threading;
- using System.Net.Sockets;
- using System.IO;
- namespace server
- {
- public partial class Form1 : Form
- {
-
- public Form1()
- {
- InitializeComponent();
- }
- private IPAddress myIP = IPAddress.Parse("127.0.0.1");
- private IPEndPoint MyServer;
- private Socket seSock;
- private bool flag = true;
- private Socket reSock;
-
- private void targett()
- {
- Control.CheckForIllegalCrossThreadCalls = false;
- try
- {
- if (reSock.Connected)
- {
- while (flag)
- {
- toolStripLabel1.Text = "与客户端建立连接!";
- byte[] r_data = new byte[64];
- reSock.Receive(r_data, r_data.Length, 0);
- string str = System.Text.Encoding.BigEndianUnicode.GetString(r_data);
- listBox1 .Items .Add (str.Substring(1, str.Length - 1));
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- myIP = IPAddress.Parse("127.0.0.1");
- }
- catch
- {
- MessageBox.Show("你输入的IP地址格式不正确,请重新输入!");
- }
- try
- {
- if (seSock != null)
- {
- seSock.Close();
- }
- MyServer = new IPEndPoint(myIP, 6001);
- seSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- MessageBox.Show("主机" + "127.0.0.1" + "端口" + "6001" + "等待连接……");
- seSock.Bind(MyServer);
- seSock.Listen(50);
- reSock = seSock.Accept();
- Thread thread = new Thread(new ThreadStart(targett));
- thread.Start();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void sendmsg(string send)
- {
- try
- {
- byte[] s_data = new byte[64];
- s_data = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray ());
- reSock.Send(s_data, s_data.Length, 0);
- }
- catch
- {
- MessageBox.Show("连接尚未建立!无法发送!");
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- listBox1.Items.Add(DateTime .Now .ToString ("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1 .Text );
- sendmsg(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "我说:" + richTextBox1 .Text .ToString ());
- richTextBox1.Clear();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- }
- //private void button4_Click(object sender, EventArgs e)
- //{
- // openFileDialog1.InitialDirectory = "c:\";
- // openFileDialog1.Filter = "全部文件|*.*|文本文件|*.txt";
- // if (openFileDialog1.ShowDialog() == DialogResult.OK)
- // {
- // textBox1.Text = openFileDialog1.FileName;
- // FileStream fs = new FileStream(textBox1.Text.ToString(), FileMode.OpenOrCreate);
- // textBox2.Text = fs.Length.ToString() + "K";
- // }
- //}
- //private void button5_Click(object sender, EventArgs e)
- //{
- // Socket sensock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- // IPEndPoint ipep = new IPEndPoint(myIP, 6001);
- // try
- // {
- // if (textBox1.Text == "")
- // {
- // MessageBox.Show("文件名不能为空");
- // }
- // else
- // {
- // FileStream fs = new FileStream(textBox1.Text.ToString(), FileMode.OpenOrCreate);
- // BinaryReader r = new BinaryReader(fs);
- // byte[] postArray = r.ReadBytes((int)fs.Length);
- // sensock.Send(postArray);
- // }
- // }
- // catch (SocketException se)
- // {
- // MessageBox.Show(se.Message);
- // }
- //}
-
-
- }
- }