您的位置:程序门 -> .net技术 -> c#



关于tcplistener 程序提示每个套节字地址(协议/网络地址/端口)只允许使用一次


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于tcplistener 程序提示每个套节字地址(协议/网络地址/端口)只允许使用一次[已结贴,结贴人:wqm_44944]
发表于:2007-03-09 09:07:44 楼主
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;
}
}
}
发表于:2007-03-09 09:08:261楼 得分:0
请大家帮忙看一下是那里出了问题
发表于:2007-03-09 09:08:392楼 得分:0
??
发表于:2007-03-09 09:09:363楼 得分:20
看看你机器上的port是否已经被其他程序占用
netstat   -an
发表于:2007-03-09 14:40:584楼 得分:0
好的,就是我要用的端口已经被占用了!谢谢


快速检索

最新资讯
热门点击