|
![]() | 作者: kenter1643 [kenter1643]
![]() |
登录 |
程序中我用了命名控件using System.Net.Sockets ; 在class中定义private Socket stSend;的时候调试说错误~~ 错误信息是"Socket"表示“命名空间”,此处应为“类” 我就很奇怪~~ 我的代码如下 using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Net.Sockets ; //使用到TcpListen类 using System.Net ; namespace Socket { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.StatusBar statusBar1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; int port = 8000 ; //定义侦听端口号 private TcpClient tcpc ; //对服务器端创建TCP连接 private Socket stSend ; //创建发送数据套接字 private bool tcpConnect = false ; //定义标识符,用以表示TCP连接是否建立 public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } private void InitializeComponent ( ) { this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.listBox1 = new System.Windows.Forms.ListBox(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.label3 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(24, 20); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(74, 30); this.label1.TabIndex = 0; this.label1.Text = "IP地址:"; // // textBox1 // this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.textBox1.Location = new System.Drawing.Point(94, 18); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(166, 21); this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // button1 // this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button1.Location = new System.Drawing.Point(280, 14); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(62, 28); this.button1.TabIndex = 2; this.button1.Text = "连接"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label2 // this.label2.Location = new System.Drawing.Point(16, 64); this.label2.Name = "label2"; this.label2.TabIndex = 3; this.label2.Text = "发送信息:"; // // textBox2 // this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.textBox2.Location = new System.Drawing.Point(94, 58); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(166, 21); this.textBox2.TabIndex = 4; this.textBox2.Text = ""; // // listBox1 // this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(20, 118); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(336, 160); this.listBox1.TabIndex = 6; // // statusBar1 // this.statusBar1.Location = new System.Drawing.Point(0, 295); this.statusBar1.Name = "statusBar1"; this.statusBar1.Size = new System.Drawing.Size(370, 22); this.statusBar1.TabIndex = 7; this.statusBar1.Text = "无连接"; // // label3 // this.label3.Location = new System.Drawing.Point(14, 94); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(128, 23); this.label3.TabIndex = 8; this.label3.Text = "已经发送的信息:"; // // button2 // this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.button2.Location = new System.Drawing.Point(280, 54); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(62, 28); this.button2.TabIndex = 9; this.button2.Text = "发送"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(370, 317); this.Controls.Add(this.button2); this.Controls.Add(this.statusBar1); this.Controls.Add(this.listBox1); this.Controls.Add(this.textBox2); this.Controls.Add(this.label2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Controls.Add(this.label3); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "Form1"; this.Text = "利用Socket来发送数据"; this.ResumeLayout(false); } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if ( tcpConnect ) { Byte [ ] bySend = new byte [ 4 ] ; //根据字符串“STOP”长度来定义Byte数组 bySend = System.Text.Encoding. Default.GetBytes ( "STOP" ) ; int i = stSend.Send ( bySend ) ; //发送控制码 stSend.Close ( ) ; //关闭套接字 } if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void label1_Click(object sender, System.EventArgs e) { } private void label2_Click(object sender, System.EventArgs e) { } private void textBox1_TextChanged(object sender, System.EventArgs e) { } private void textBox2_TextChanged(object sender, System.EventArgs e) { } private void label3_Click(object sender, System.EventArgs e) { } private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { } private void statusBar1_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e) { } private void Form1_Load(object sender, System.EventArgs e) { } private void button1_Click(object sender, System.EventArgs e) { try { stSend = new Socket ( AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp ) ; //初始化一个Socket实例 IPEndPoint tempRemoteIP = new IPEndPoint ( IPAddress.Parse ( textBox1.Text ) , port ) ; //根据IP地址和端口号创建远程终结点 EndPoint epTemp = ( EndPoint ) tempRemoteIP ; stSend.Connect ( epTemp ) ; //连接远程主机的8000端口号 statusBar1.Text = "成功连接远程计算机!" ; tcpConnect = true ; button1.Enabled = false ; button2.Enabled = true ; } catch ( Exception ) { statusBar1.Text = "目标计算机拒绝连接请求!" ; } } private void button2_Click(object sender, System.EventArgs e) { int iLength = textBox2.Text.Length ; //获取要发送的数据的长度 Byte [ ] bySend = new byte [ iLength ] ; //根据获取的长度定义一个Byte类型数组 bySend = System.Text.Encoding.Default.GetBytes ( textBox2.Text ) ; //按照指定编码类型把字符串指定到指定的Byte数组 int i = stSend.Send ( bySend ) ; //发送数据 listBox1.Items.Add ( textBox2.Text ) ; } } } |
地主 发表时间: 05-02-05 08:46 |
![]() | 回复: kenter1643 [kenter1643] ![]() |
登录 |
希望大家帮忙调试下・~解决问题 |
B1层 发表时间: 05-02-05 09:56 |
![]() | 回复: kenter1643 [kenter1643] ![]() |
登录 |
狂晕~~问题解决・~原因是我把程序命名为socket重复了socket 改名字就可以了 谢谢286前辈的指点 |
B2层 发表时间: 05-02-05 15:53 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号