请帮忙看看这个程序为什么出错?

lgfong 2003-12-24 04:12:27
这是一个SMTP服务器程序,编绎的时候出现两种错误:1.“名称'newClient'在类或命名空间'accep_so.Form2'中不存在。2.找不到类型或命名空间名称'(是否缺少using指令或程序集引用?)

请各位高手帮忙看一下,谢谢!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace accep_so
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.RichTextBox richTextBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private bool control =false;
Thread thread;
private int port;
private TcpListener listener;

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

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

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

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 0;
this.label1.Text = "监听端口:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 2;
this.label2.Text = "客户信息:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 248);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "开始服务";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(168, 248);
this.button2.Name = "button2";
this.button2.TabIndex = 6;
this.button2.Text = "关闭服务";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(64, 40);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(224, 200);
this.richTextBox1.TabIndex = 7;
this.richTextBox1.Text = "richTextBox1";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox1,
this.button2,
this.button1,
this.label2,
this.textBox1,
this.label1});
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
try
{
port =Int32.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("您输入的格式不对!请输入正整数。");
}
try
{
listener=new TcpListener(port);
listener.Start();
thread=new Thread(new ThreadStart(recieve));
thread.Start();
}
catch(Exception ee){MessageBox.Show(ee.Message);}
}

private void Form2_Load(object sender, System.EventArgs e)
{

}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
listener.Stop();
}
catch(Exception ee){MessageBox.Show(ee.Message);}
}
private void recieve()
{
while(true)
{
newClient=listener.AcceptSocket();
if(newClient.Connected)
{
Thread thread=new Thread(new ThreadStart(round));
thread.Start();
}
}
}

private void round()
{
bool mailfrom=false;
bool rcptto=false;
bool getMail=false;
StreamWriter mail=null;
int queue=0;
string commString= null;
string bigComm=null;
string command=null;
string parameter=null;
string hostName=null;
string mailbox=null;
NetworkStream netStream=new NetworkStream(newClient);
string from=null;
sendFeedback(ref netStream,"220 SMTP Server Ready!");

while(true)
{
//接受信息++++++
if(getMail==false)
{
commString= readCommand(ref netStream);
bigComm=bigCommand (commString);
command=splitCommand(bigComm);
parameter=splitParameter(bigComm);
command=command.ToLower();
parameter=parameter.ToLower();
hostName=Dns.GetHostName();
richTextBox1.AppendText("command:"+command+" "+"parameter:"+parameter+"\r\n");
}
if(command=="helo")
{
try
{
sendFeedback(ref netStream,"250 "+hostName);
mailfrom=false;
rcptto=false;
}
catch
{
...全文
44 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
13880079673 2003-12-24
  • 打赏
  • 举报
回复
上面由于一时激动,说错了,在头部加Socket newClient ,而不是Socket newClient = listener.AcceptSocket();
13880079673 2003-12-24
  • 打赏
  • 举报
回复
那儿搞来的代码,这么多错误

port没有定义
newClient也没有定义

把头部的private int port; 改为 private int port = 100;
并且头部也要加上Socket newClient = listener.AcceptSocket();
这样编译至少能过
myhex 2003-12-24
  • 打赏
  • 举报
回复
一定要在顶部加,
代码改成Socket newClient = listener.AcceptSocket();还是会出错
huhan3 2003-12-24
  • 打赏
  • 举报
回复
或在form2的顶部加个
Socket newClient;
试试
huhan3 2003-12-24
  • 打赏
  • 举报
回复
我查到了,应该是返回一个Socket对象

也就是说,把你的代码
newClient=listener.AcceptSocket();

改为
Socket newClient = listener.AcceptSocket();
试试
huhan3 2003-12-24
  • 打赏
  • 举报
回复
通过你这句代码:newClient=listener.AcceptSocket();
说明你要把listener对象的AcceptSocket()方法作为一个对象来处理,这个对象起名为newClient,你直接给newClient对象赋值,在此之前却没有任何对newClient对象的定义。你得先引用newClient类型类所在的命名空间,然后定义一个名为newClient的对象。

具体是什么命名空间,我也不知道,我没用过网络通信方面的类,你自己查查文档吧。
不过也有可能根本没有相应的命名空间,你得自己写相应的类:(,你就得查查listener.AcceptSocket();这个方法返回的是什么东西了。
lgfong 2003-12-24
  • 打赏
  • 举报
回复
能详细说一下添加什么类型的引用吗?
polarlm 2003-12-24
  • 打赏
  • 举报
回复
需要添加引用
lgfong 2003-12-24
  • 打赏
  • 举报
回复
接上贴:
sendFeedback (ref netStream,"421 unsuccessful,connection will be Closed");
newClient.Close();
}
}
else if(command=="mail")
{
try
{
mailfrom=true;
from=parameter;
rcptto=false;
sendFeedback(ref netStream, "250 "+parameter+"...sender OK!");
}
catch{sendFeedback(ref netStream,"502 performed unsuccessful");}
}
else if(command=="rcpt")
{
bool round =true;
int i=0;
try
{
if(mailfrom==true)
{
rcptto=true;
string host=splitServer(parameter);
mailbox=splitMailbox(parameter);
if(host==hostName)
{
bool mailboxExists=Directory.Exists("e:\\smtp\\mailbox\\"+mailbox);
if(mailboxExists==true)
{
while((round==true)&&(i<1024))
{
bool mailExists=File.Exists("e:\\smtp\\mailbox\\"+mailbox+"\\"+"mail"+i.ToString()+".txt");
if(mailExists==false)
{
queue=i;
mail=new StreamWriter("e:\\smpt\\mailbox\\"+mailbox+"\\"+"mail"+queue.ToString()+".txt",false,System.Text.Encoding.UTF8);
mail.Write("\r\n");
mail.Close();
round=false;
}
i++;
}
sendFeedback(ref netStream,"250 "+parameter);
if(round==true)
{
sendFeedback(ref netStream,"452 "+parameter+"space insufficient");
}
}
else
{
sendFeedback(ref netStream,"550 "+parameter+"does not exist");
}
}
else
{
//下面可添加代码调动转发功能
//上面可添加代码调用转发功能
}
}
else
{
sendFeedback(ref netStream,"503 mail from command needed");
}
}
catch{sendFeedback(ref netStream,"502 performed unsuccessful");}
}
else if(command=="data"|| getMail==true)
{
try
{
if((mailfrom==true)&&(rcptto==true))
{
bool round=true;
if(getMail==false)
{
sendFeedback(ref netStream,"354 input:end with<CR><LF>.<CR><LF>");
getMail=true;
}
while(round==true)
{
string message=readMail(ref netStream);
int checkEnd=message.IndexOf("\r\n.\r\n");
if(checkEnd==-1)
{
mail=new StreamWriter("e:\\smtp\\mailbox\\"+mailbox+"\\"+"mail"+queue.ToString()+".txt",true,System.Text.Encoding.UTF8);
mail.Write(message);
mail.Close();
}
else
{
mail=new StreamWriter("e:\\smtp\\mailbox\\"+mailbox+"\\"+"mail"+queue.ToString()+".txt",true,System.Text.Encoding.UTF8);
mail.Write(message);
mail.Close();
round=false;
getMail=false;
}
}
sendFeedback(ref netStream,"250 OK");
}
else{sendFeedback(ref netStream,"503 MAIL or RCPT command needed");}
}
catch{sendFeedback(ref netStream,"502 performed unsuccessful");}
}
else if(command=="help")
{
thread.Resume();
try
{
sendFeedback(ref netStream,"211 free smtp server ..and other help message.");}
catch
{
sendFeedback(ref netStream,"502 performedunsuccessful,try again");
}
}
else if(command=="quit")
{
try
{
control=true;
sendFeedback(ref netStream,"211 connection closed");
newClient.Close();
}
catch{sendFeedback(ref netStream,"502 performed unsuccessful,try again");}
}
else if(getMail==false)
{
sendFeedback(ref netStream,"500 unrecognized command");
}
}

}
private string readCommand (ref NetworkStream NetStream)
{
byte[] byteMessage=new byte[1024];
NetStream.Read(byteMessage,0,byteMessage.Length);
string command=System.Text.Encoding.UTF8.GetString(byteMessage);
return command;
}
private void sendFeedback(ref NetworkStream NetStream,string ToSend)
{
string stringToSend = ToSend+"\r\n";
byte[] arrayToSend=System.Text.Encoding.UTF8.GetBytes(stringToSend.ToCharArray());
NetStream.Write(arrayToSend,0,arrayToSend.Length);
NetStream.Flush();
}
private string readMail (ref NetworkStream NetStream)
{
byte[] byteArray=new Byte[1024];
NetStream.Read(byteArray,0,byteArray.Length);
string mailMessage=System.Text.Encoding.UTF8.GetString(byteArray);
return mailMessage;
}
private string splitServer (string aimString)
{
string[] aim=new string[2];
char[] a=new char[]{'@'};
aim=aimString.Split(a);
int y=aim[1].IndexOf(">");
if(y!=-1){
string server=aim[1].Substring(0,aim[1].Length-1);
return server;
}
else{
return aim[1];}
}
private string splitMailbox(string aimString)
{
string[] aim=new string[2];
char[] a=new char[]{'@'};
aim=aimString.Split(a);
int y=aim[0].IndexOf("<");
if(y!=-1)
{
string mailbox=aim[0].Substring(y+1,aim[0].Length-y-1);
return mailbox;
}
else{
int x=aim[0].IndexOf(":");
string mailbox=aim[0].Substring(x+1,aim[0].Length-x-1);
return mailbox;
}
}
private string splitCommand(string aimString)
{
string[] aim=new string[2];
int x=aimString.IndexOf(" ");
if(x!=-1)
{
char[] a=new char[]{' '};
aim=aimString.Split(a);
return aim[0];}
else{return aimString;}
}
private string splitParameter(string aimString)
{
string[] aim=new string[2];
int x=aimString.IndexOf(" ");
if(x!=-1)
{
string para=aimString.Substring(x+1,aimString.Length -x-1);
return para;
}
else{return "";}
}
private string bigCommand(string commString)
{
int x=commString.IndexOf("\r\n");
string command=commString.Substring(0,x);
return command;
}
}
}

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧