读书人

对于TcpListener 程序提示每个套节字地

发布时间: 2011-12-20 22:26:40 作者: rapoo

关于TcpListener 程序提示每个套节字地址(协议/网络地址/端口)只允许使用一次
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.Runtime.InteropServices;

namespace FtpServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FtpServerForm : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.ListBox lbClients;
private System.Windows.Forms.TextBox tbSocketPort;
private System.Windows.Forms.TextBox tbClientsNum;
public const string ROOTDIR = "c:\\ ";
//服务器可以支持最多的连接数
public const int MAXNUM = 100;
//clients 数组保存当前在线
//internal 允许同一个命名空间中的类访问
// Hashtable 表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。
internal static Hashtable client = new Hashtable();
//该服务器默认的监听端口号
public TcpListener tcpListener;
//开始服务的标志
internal static bool SocketServiceFlag = false;
//服务器的ip地址
internal string ip;
internal int port;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public FtpServerForm()
{
//
// 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()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.lbClients = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.tbSocketPort = new System.Windows.Forms.TextBox();
this.tbClientsNum = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//


// groupBox1
//
this.groupBox1.Controls.Add(this.btnStop);
this.groupBox1.Controls.Add(this.btnStart);
this.groupBox1.Controls.Add(this.tbClientsNum);
this.groupBox1.Controls.Add(this.tbSocketPort);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.lbClients);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1 ";
this.groupBox1.Size = new System.Drawing.Size(472, 472);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "ftp服务器设置 ";
this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
//
// lbClients
//
this.lbClients.ItemHeight = 12;
this.lbClients.Location = new System.Drawing.Point(8, 16);
this.lbClients.Name = "lbClients ";
this.lbClients.Size = new System.Drawing.Size(224, 436);
this.lbClients.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(248, 46);
this.label1.Name = "label1 ";
this.label1.TabIndex = 1;
this.label1.Text = "Socket端口号: ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(248, 96);
this.label2.Name = "label2 ";
this.label2.TabIndex = 2;
this.label2.Text = "当前在线用户: ";
//
// tbSocketPort
//
this.tbSocketPort.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbSocketPort.Location = new System.Drawing.Point(360, 48);
this.tbSocketPort.Name = "tbSocketPort ";
this.tbSocketPort.TabIndex = 3;
this.tbSocketPort.Text = "21 ";
this.tbSocketPort.TextChanged += new System.EventHandler(this.tbSocketPort_TextChanged);
//
// tbClientsNum
//
this.tbClientsNum.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbClientsNum.Location = new System.Drawing.Point(360, 96);
this.tbClientsNum.Name = "tbClientsNum ";
this.tbClientsNum.TabIndex = 4;
this.tbClientsNum.Text = " ";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(264, 168);
this.btnStart.Name = "btnStart ";
this.btnStart.Size = new System.Drawing.Size(184, 24);
this.btnStart.TabIndex = 5;
this.btnStart.Text = "启动FTP服务器 ";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(264, 216);
this.btnStop.Name = "btnStop ";
this.btnStop.Size = new System.Drawing.Size(184, 24);
this.btnStop.TabIndex = 6;


this.btnStop.Text = "停止FTP服务器 ";
//
// FtpServerForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 485);
this.Controls.Add(this.groupBox1);
this.MaximumSize = new System.Drawing.Size(496, 512);
this.Name = "FtpServerForm ";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Sever--U ";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.FtpServerForm_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FtpServerForm());
}

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

}

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

}

private void tbSocketPort_TextChanged(object sender, System.EventArgs e)
{
this.btnStart.Enabled = (this.tbSocketPort.Text != " ");
}

public void btnStart_Click(object sender, System.EventArgs e)
{
Console.WriteLine( "FTP服务器启动....... ");
port = getValidPort(tbSocketPort.Text);
if(port <0)
{
return;
}
//获取本机局域网ip地址
IPAddress[] AddressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;

if(AddressList.Length> 0)
{
ip = AddressList[0].ToString();

try
{
IPAddress ipAdd = IPAddress.Parse(ip);
//创建服务器套节字
tcpListener = new TcpListener(ipAdd,port);
//开始监听服务器端口
tcpListener.Start();
//启动一个新的线程,执行方法this.StartSocketListen;
//以便在一个独立的进程中执行确认与客户端Socket连接的操作
FtpServerForm.SocketServiceFlag = true;
Thread thread = new Thread(new ThreadStart(this.StartSocketListen));
thread.Start();
this.btnStart.Enabled = false;
this.btnStop.Enabled = true;
this.tbSocketPort.Enabled = false;
}
catch(Exception ex)
{
Console.WriteLine( "不能启动服务器 "+ex.ToString());
MessageBox.Show(ex.Message, " ");
}
}

}
private void StartSocketListen()
{
//暂时无内容
}
private int getValidPort(string port)
{
int lport;
try
{
if(port == " ")
{
throw new ArgumentException( "端口为空,不能启动服务器 ");
}
lport = System.Convert.ToInt32(port);
}
catch(Exception e)
{
Console.WriteLine( "无效的端口号: "+e.ToString());


return -1;

}
return lport;
}
}
}


[解决办法]
看看你机器上的port是否已经被其他程序占用
netstat -an

读书人网 >C#

热点推荐