using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace Socket
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
//声明监听线程对象
private Thread MyListenThread;
//声明网络监听对象
private TcpListener MyTcpListener;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.label3 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.ForeColor = System.Drawing.SystemColors.Menu;
this.button1.Location = new System.Drawing.Point(336, 432);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 32);
this.button1.TabIndex = 0;
this.button1.Text = "发送";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.ForeColor = System.Drawing.SystemColors.Menu;
this.label1.Location = new System.Drawing.Point(88, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(66, 17);
this.label1.TabIndex = 1;
this.label1.Text = "远程主机名";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.SystemColors.Menu;
this.label2.Location = new System.Drawing.Point(280, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(42, 17);
this.label2.TabIndex = 2;
this.label2.Text = "本机名";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Location = new System.Drawing.Point(152, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(104, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Anchor = System.Windows.Forms.AnchorStyles.None;
this.textBox2.BackColor = System.Drawing.SystemColors.Desktop;
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox2.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox2.ForeColor = System.Drawing.SystemColors.Menu;
this.textBox2.Location = new System.Drawing.Point(325, 16);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(104, 19);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// richTextBox1
//
this.richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.richTextBox1.ContextMenu = this.contextMenu1;
this.richTextBox1.Location = new System.Drawing.Point(8, 368);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
this.richTextBox1.Size = new System.Drawing.Size(312, 96);
this.richTextBox1.TabIndex = 5;
this.richTextBox1.Text = "";
this.richTextBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.richTextBox1_KeyDown);
this.richTextBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.richTextBox1_KeyUp);
//
// richTextBox2
//
this.richTextBox2.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.richTextBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.richTextBox2.Location = new System.Drawing.Point(8, 48);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(312, 280);
this.richTextBox2.TabIndex = 6;
this.richTextBox2.Text = "";
//
// label3
//
this.label3.ForeColor = System.Drawing.SystemColors.Menu;
this.label3.Location = new System.Drawing.Point(8, 336);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 24);
this.label3.TabIndex = 8;
this.label3.Text = "将要发送的内容";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label5
//
this.label5.AllowDrop = true;
this.label5.AutoSize = true;
this.label5.ForeColor = System.Drawing.SystemColors.Menu;
this.label5.Location = new System.Drawing.Point(8, 16);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(42, 17);
this.label5.TabIndex = 10;
this.label5.Text = "收发:";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.ForeColor = System.Drawing.SystemColors.Menu;
this.button2.Location = new System.Drawing.Point(256, 336);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 11;
this.button2.Text = "浏览图片";
this.button2.Visible = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.Control;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(323, 48);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(125, 152);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 12;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
//
// openFileDialog1
//
this.openFileDialog1.Filter = "All file(*.*)|*.*";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "复制(&C)";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "粘贴(&P)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "剪切(&T)";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Desktop;
this.ClientSize = new System.Drawing.Size(450, 470);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.label5);
this.Controls.Add(this.label3);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.KeyPreview = true;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "QQ-ZGG";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
System.Net.IPHostEntry ss = Dns.GetHostByAddress(textBox1.Text);
label5.Text = ss.HostName.ToString();
}
catch(Exception er){
MessageBox.Show("没有找到该主机名!");
}
if(textBox1.Text.Length < 1 || textBox2.Text.Length < 1 || richTextBox1.Text.Trim().Length < 1){
return;
}
try{
// richTextBox1.Text = richTextBox1.Text.Substring(0,richTextBox1.TextLength - "\n".Length);
string MyMessage = textBox2.Text + ":" + richTextBox1.Text + "\n";
//根据目标计算机地址建立连接
TcpClient MyTcpClient = new TcpClient(textBox1.Text,888);
// System.Net.Sockets.UdpClient MyUdpClient = new UdpClient(textBox1.Text,888);
//获得用网络访问数据流
NetworkStream MyTcpStream = MyTcpClient.GetStream();
StreamWriter MyStream = new StreamWriter(MyTcpStream);
//将字符串写入流
MyStream.Write(MyMessage);
//将缓冲数据写入基础流
MyStream.Flush();
//关闭网络流
MyStream.Close();
MyTcpClient.Close();
richTextBox2.ReadOnly = false;
richTextBox2.AppendText(textBox2.Text + ":" + "\n" + richTextBox1.Text + "\n");
richTextBox2.ReadOnly = true;
richTextBox1.Clear();
richTextBox1.Focus(); // 得到焦点
}
catch(Exception err){
MessageBox.Show(err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void Key_Click(){
if(textBox1.Text.Length < 1 || textBox2.Text.Length < 1 || richTextBox1.Text.Trim().Length < 1)
{
return;
}
try
{
richTextBox1.Text = richTextBox1.Text.Substring(0,richTextBox1.TextLength - "\n".Length);
string MyMessage = textBox2.Text + ":" + richTextBox1.Text + "\n";
//根据目标计算机地址建立连接
TcpClient MyTcpClient = new TcpClient(textBox1.Text,888);
// System.Net.Sockets.UdpClient MyUdpClient = new UdpClient(textBox1.Text,888);
//获得用网络访问数据流
NetworkStream MyTcpStream = MyTcpClient.GetStream();
StreamWriter MyStream = new StreamWriter(MyTcpStream);
//将字符串写入流
MyStream.Write(MyMessage);
//将缓冲数据写入基础流
MyStream.Flush();
//关闭网络流
MyStream.Close();
MyTcpClient.Close();
richTextBox2.ReadOnly = false;
richTextBox2.AppendText(textBox2.Text + ":" + "\n" + richTextBox1.Text + "\n");
richTextBox2.ReadOnly = true;
richTextBox1.Clear();
richTextBox1.Focus(); // 得到焦点
}
catch(Exception err)
{
MessageBox.Show(err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void StartListen(){
try
{
MyTcpListener = new TcpListener(888);
//开始监听
MyTcpListener.Start();
while(true)
{
//获取TcpClient
TcpClient MyTcpClient = MyTcpListener.AcceptTcpClient();
// Socket s = MyTcpListener.AcceptSocket();
NetworkStream MyStream = MyTcpClient.GetStream();
Byte[] MyBytes = new Byte[1024];
int MyByteRead = MyStream.Read(MyBytes,0,MyBytes.Length);
string MyMessage = System.Text.Encoding.UTF8.GetString(MyBytes,0,MyByteRead);
textBox1.Text = MyMessage.Substring(0,MyMessage.LastIndexOf(":"));
this.Text = "QQ-ZGG:" + "来自" + textBox1.Text + "的呼叫!";
richTextBox2.ReadOnly = false;
richTextBox2.Text += MyMessage;
richTextBox2.ReadOnly = true;
}
}
catch(Exception err){
Console.WriteLine(err.Message);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
//开始监听
//创建监听线程
string LocalHost = Dns.GetHostName();
textBox2.Text = LocalHost;
textBox2.ReadOnly = true;
MyListenThread = new Thread(new ThreadStart(StartListen));
//启动线程
MyListenThread.Start();
}
private void Form1_Closed(object sender, System.EventArgs e)
{
try
{
if(MyTcpListener!=null){
//关闭监听器
MyTcpListener.Stop();
}
if(MyListenThread != null){
if(MyListenThread.ThreadState == ThreadState.Running)
{
MyListenThread.Abort();
}
}
}
catch(Exception err){
MessageBox.Show(err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
}
private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{//使用快捷组合键
}
private void button2_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
pictureBox1.Image = System.Drawing.Bitmap.FromFile(openFileDialog1.FileName);
}
private void richTextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Control == true)
{
switch (e.KeyCode)
{
case Keys.Enter:
// richTextBox1.Text = richTextBox1.Text + "\n";
Key_Click();
break;
default: break;
}
}
}
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
}
private void pictureBox1_DoubleClick(object sender, System.EventArgs e)
{
// if(button2.Visible == false)
// button2.Visible = true;
// else
// button2.Visible = false;
button2_Click(sender,e);
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
//copy
richTextBox1.Copy();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
richTextBox1.Cut();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
richTextBox1.Paste();
}
}
}