⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.cs

📁 一个可联网玩的飞机小游戏
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace WindowsApplication2
{
	/// <summary>
	/// Form2 的摘要说明。
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.TextBox textBox1;
		private Form1 form1;
		private wangluowanjia wlwj;
		private wanjia wanjia;
		private Socket socketForClient ;
		private NetworkStream networkStream ;
		private TcpListener tcpListener ;
		private StreamWriter streamWriter ;
		private StreamReader streamReader;
        private TcpClient myclient ;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Timer timer1;
		private System.ComponentModel.IContainer components;

		public Form2()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			

		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form2());

          

		}
		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button4 = new System.Windows.Forms.Button();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(24, 120);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(152, 88);
			this.listBox1.TabIndex = 1;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(200, 120);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "主机";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Enabled = false;
			this.button2.Location = new System.Drawing.Point(200, 160);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(80, 24);
			this.button2.TabIndex = 3;
			this.button2.Text = "开始游戏";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(208, 24);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(72, 24);
			this.button3.TabIndex = 4;
			this.button3.Text = "连接";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(24, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(152, 21);
			this.textBox1.TabIndex = 5;
			this.textBox1.Text = "192.168.1.6";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(24, 224);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(152, 21);
			this.textBox2.TabIndex = 6;
			this.textBox2.Text = "";
			// 
			// button4
			// 
			this.button4.Enabled = false;
			this.button4.Location = new System.Drawing.Point(200, 224);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(80, 24);
			this.button4.TabIndex = 7;
			this.button4.Text = "谈话";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// timer1
			// 
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(320, 266);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.listBox1);
			this.Name = "Form2";
			this.Text = "Form2";
			this.Closed += new System.EventHandler(this.Form2_Closed);
			this.ResumeLayout(false);

		}
		#endregion

	

		private void button3_Click(object sender, System.EventArgs e)
		{
            button1.Enabled=false;
			button2.Enabled=false;
			try
			{
				myclient = new TcpClient ( textBox1.Text , 1234 ) ;
                listBox1.Items.Add ( "连接到服务器" ) ;
				timer1.Start();
			}
			catch
			{
				listBox1.Items.Add ( "没有连接到服务器" ) ;
				return ;
			}
			//创建networkStream对象通过网络套节字来接受和发送数据
			networkStream = myclient.GetStream ( ) ;
			streamReader = new StreamReader ( networkStream ) ;
			streamWriter = new StreamWriter ( networkStream ) ;
            button4.Enabled=true;
			//button2.Enabled=true;
		
		
		
		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			textBox1.Enabled=false;
			button3.Enabled=false;
			//创建一个tcpListener对象,此对象主要是对给定端口进行侦听
			try
			{
				tcpListener = new TcpListener (1234 ) ;
				//开始侦听
				tcpListener.Start ( ) ; 
				//返回可以用以处理连接的Socket实例
				
			}
			catch(Exception ex) 
			{ 
				MessageBox.Show("Can't Start Server"+ex.Message); 
				return; 
			} 
               socketForClient = tcpListener.AcceptSocket ( ) ; 
				if ( socketForClient.Connected )
				{
					listBox1.Items.Add ( "已经和客户端成功连接!" ) ;			
					//创建networkStream对象通过网络套节字来接受和发送数据
					networkStream = new NetworkStream ( socketForClient ) ;
					//从当前数据流中读取一行字符,返回值是字符串
					streamReader = new StreamReader ( networkStream ) ;	
					streamWriter = new StreamWriter ( networkStream ) ;
					button4.Enabled=true;
					button2.Enabled=true;
                    timer1.Start();
				}
		
							
		}
		private void kaishi()
		{
			form1=new Form1();		
			networkStream.WriteByte(1);
			networkStream.Flush();
			form1.Visible=true;
			form1.networkStream=networkStream;
			form1.streamReader=streamReader;
			form1.streamWriter=streamWriter;
			wlwj=new wangluowanjia();
			wanjia=new wanjia();
			form1.dxkz.zhenjia(wanjia);
			form1.dxkz.zhenjia(wlwj);
			form1.streamWriter.WriteLine("q");
			form1.streamWriter.Flush();
			timer1.Stop();
		}
		private void button2_Click(object sender, System.EventArgs e)
		{
			kaishi();
			
           // this.Close();
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
		    streamWriter.WriteLine ( textBox2.Text ) ;
			listBox1.Items.Add("我说:"+textBox2.Text);
            streamWriter.Flush ( ) ;
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			//string s;
			if (networkStream.DataAvailable)
			{
				
                 //  listBox1.Items.Add ( "他说:" + streamReader.ReadLine ( )) ;
				
				if (networkStream.ReadByte()==1)
				{
					kaishi();
				}
				else 
				{
                  listBox1.Items.Add ( "他说:" + streamReader.ReadLine ( )) ;
				}
			}
		}

		private void Form2_Closed(object sender, System.EventArgs e)
		{
			//streamReader.Close ( ) ;
			//streamWriter.Close ( ) ;
           // networkStream.Close ( ) ;
			//socketForClient.Shutdown ( SocketShutdown.Both ) ;
			//socketForClient.Close ( ) ;
			
		}

		
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -