网络端口为什么阻塞了

blackant2 2005-10-25 08:33:48
string text1 = Dns.GetHostName();
IPHostEntry entry1 = new IPHostEntry();
entry1 = Dns.Resolve(text1);
IPAddress address1 = entry1.AddressList[0];
//IPAddress address1=IPAddress.Parse("127.0.0.1");
this.Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.Server.Bind(new IPEndPoint(address1,23344));
this.Server.Listen(200);
this.Server.BeginAccept(new AsyncCallback(this.acConnection), this.Server);
这是开始调用acConnection的部分,其中acConnection声明如下
private void acConnection(IAsyncResult ar)
{


Console.WriteLine("acConnection23344 connectioned!");
Socket server = (Socket) ar.AsyncState;
Socket client = server.EndAccept(ar);
try
{
byte[] bufferslength=new byte[2];
client.Receive(bufferslength,0,2,SocketFlags.Partial);
byte [] buffers=new byte[2000];
int bufferlength1=client.Receive(buffers);
//Console.WriteLine("bufferlength1:{0},buffers[0][1]={1}",bufferlength1,bufferslength[1]*256+bufferslength[0]);
if(bufferlength1 >2 &&(bufferlength1==bufferslength[1]*256+bufferslength[0]))
{
//Console.WriteLine("Received:{0}",BitConverter.ToString(buffers,0,bufferlength1));
//Console.WriteLine("bufferlength1={0:x2};buffers[0]={1:x2}",bufferlength1,buffers[0]);
byte[] bytereceived=new byte[bufferlength1/2];
for(int i=0;i<bytereceived.Length;i++){
bytereceived[i]=Convert.ToByte(buffers[i*2]^0x0b);
}
//Console.WriteLine("Received2:{0}",BitConverter.ToString(bytereceived));
byte[] bytedereceived=Base64.Base64Decode(Encoding.ASCII.GetString(bytereceived));
//Console.WriteLine("deReceived:{0}",BitConverter.ToString(bytedereceived));
String strde=Encoding.ASCII.GetString(bytedereceived);
//Console.WriteLine("deReceived:{0}",strde);
//Regex reg=new Regex(@"([\S]*[\s]*)([\S]{32})([\S]+)",RegexOptions.Multiline);
Regex reg=new Regex(@"([\S]{32})([\s]*)([\S]*)([\s]*)([\S]*)");
Match m=reg.Match(strde);
//Console.WriteLine("m.Groups.Count:{0}",m.Groups.Count);
if(m.Groups.Count==6){
//for(int i=1;i<6;i++){
//Console.WriteLine("index[{0}].Captures={1};Value={2}",i,m.Groups[i].Captures,m.Groups[i].Value);
//}
String strKey=m.Groups[5].Value.Trim();
String strAccount=m.Groups[3].Value.Trim();
byte[] byteDeKey=Base64.Base64Decode(strKey);
//Console.WriteLine("bytedeKey:{0}",BitConverter.ToString(byteDeKey));
//Console.WriteLine("Account:{0}",strAccount);
SqlConnection conn=new SqlConnection(this.Connstring);
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;
SqlParameter param=new SqlParameter("@account",strAccount);
param.SqlDbType=SqlDbType.NVarChar;
param.Size=50;
param.Direction=ParameterDirection.Input;
cmd.Parameters.Add(param);
conn.Open();
cmd.CommandText="update account set logindate=getdate() where account=@account";
cmd.ExecuteNonQuery();
cmd.CommandText="select account from account where account=@account and datediff(ss,validatedate,Getdate())<0";
SqlDataReader sdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(sdr.Read())
{
//byte[] byteDeKey=Base64.Base64Decode(strKey);
//Console.WriteLine("bytedeKey:{0}",BitConverter.ToString(byteDeKey));
//Console.WriteLine("Account:{0}",strAccount);
byte[] byte2=new byte[]{0x18,0x00,0x0d,0xFC,0x33,0x10,0x00,0xEC,0xB5,0xBE,0x03,0xD4,0x00,0xED,0x00,0x51,0x00,0x06,0x47,0x45,0xC7,0x4D,0xff,0x99,0xff,0x25};
byte[] byte3=new byte[]{0x40,0x00,0xB7,0x8E
,0xC1,0x51,0xC1,0x65,0xC2,0x53,0xB5,0x05,0xC6,0x5C,0xC6,0x33,0xC3,0xEC,0xB7,0x3F
,0xB0,0x54,0xC3,0x16,0xC3,0xA7,0xC0,0x22,0xB5,0xCD,0xC6,0xCC,0xB2,0x8F,0xB7,0x60
,0xB7,0xD4,0xC9,0xF3,0xC7,0x4E,0xC2,0x4A,0xC7,0x60,0xB7,0x3D,0xC5,0xCB,0xC3,0xEE
,0xB7,0x2F,0xC7,0x68,0xB4,0x16,0xC0,0x75,0xB3,0x93,0xB4,0x6D,0xC2,0x35
};
byte[] bytekeys=new byte[8];
bytekeys[0]=byteDeKey[1];
bytekeys[1]=byteDeKey[2];
bytekeys[2]=byteDeKey[3];
bytekeys[3]=byteDeKey[4];
bytekeys[4]=0xe2;
bytekeys[5]=0xd7;
bytekeys[6]=0x0f;
bytekeys[7]=0x93;
//Console.WriteLine("bytekeys:{0}",BitConverter.ToString(bytekeys));
blowfish bf=new blowfish();
byte[] bytede1=bf.Initialize(bytekeys);
byte[] byte1=new byte[bytede1.Length*2];
for(int i=0;i<bytede1.Length;i++)
{
byte1[i*2]=bytede1[i];
}
//Console.WriteLine("byte1:{0}",BitConverter.ToString(byte1));
//Console.WriteLine("byte2:{0}",BitConverter.ToString(byte2));
//Console.WriteLine("byte3:{0}",BitConverter.ToString(byte3));
int relength=2+byte1.Length+byte2.Length+byte3.Length;

byte[] rebyte=new byte[relength+2];
rebyte[0]=Convert.ToByte(relength%256);
rebyte[1]=Convert.ToByte(relength/256);
rebyte[2]=Convert.ToByte(byte1.Length%256);
rebyte[3]=Convert.ToByte(byte1.Length/256);
Array.Copy(byte1,0,rebyte,2+2,byte1.Length);
Array.Copy(byte2,0,rebyte,2+2+byte1.Length,byte2.Length);
Array.Copy(byte3,0,rebyte,2+2+byte1.Length+byte2.Length,byte3.Length);
//client.Send(new byte[]{Convert.ToByte(relength),0x00});
//Console.WriteLine("rebyte:{0}",BitConverter.ToString(rebyte));
client.Send(rebyte);
client.BeginSend(rebyte,0,rebyte.Length,SocketFlags.None,null,null);
}
else{
//Console.WriteLine("not register");
byte[] returns=new byte[]{0x02,0x00,0x02,0x00};
client.BeginSend(returns,0,returns.Length,SocketFlags.None,null,null);
}
sdr.Close();
conn.Close();
}
else{
//Console.WriteLine("m.Groups.Count={0}",m.Groups.Count);
}

}
else{
//Console.WriteLine("Error received");
}

}
catch(Exception excep){
//Console.WriteLine(excep.ToString());
}
finally
{
client.Shutdown(SocketShutdown.Both);
client.Close();
server.BeginAccept(new AsyncCallback(this.acConnection),this.Server);
Console.WriteLine("connection end");
}
}
...全文
595 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ravelin 2005-10-26
  • 打赏
  • 举报
回复
关注中。。。。。。。。
。。
rtdb 2005-10-26
  • 打赏
  • 举报
回复
再有就是,SERVER端一定要有自我检测与恢复机制。
zhy0101 2005-10-26
  • 打赏
  • 举报
回复
实在看不明白,看样子是同一台机子自己连自己。前面的代码是在什么方法里。把错误贴出来吧。
另外,建议你把最后finally里的
server.BeginAccept(new AsyncCallback(this.acConnection),this.Server);
放到方法外面,直觉这里可能有问题。
wangsaokui 2005-10-25
  • 打赏
  • 举报
回复
大约每24个小时会有2-3次服务器端端口会被阻塞住
把Exception贴出来看看
另:楼主程序的结构很不好,中间处理直接写一个函数就完了,不需要全部放在这里
cansum396 2005-10-25
  • 打赏
  • 举报
回复
学习
blackant2 2005-10-25
  • 打赏
  • 举报
回复
对了忘了说症状
大约每24个小时会有2-3次服务器端端口会被阻塞住
再也连不上去了
只能重启服务
应该是哪儿的问题
高手请指点指点
Re:CCNA_CCNP 思科网络认证 PAT NAT 端口或地址转换 与端口映射======================# 本章课程大纲        公网地址和私网地址        NAT应用场景        静态NAT  :static  地址转换        动态NAT  :dynamic地址转换        PAT        :端口地址转换        端口映射 :port map        在Windows上同时实现的NAT和端口映射 # 私网地址三类 A类:10.0.0.0                                255.0.0.0(1网段) B类:172.16.0.0 -172.31.0.0         255.255.0.0(16网段) C类:192.168.0.0-192.168.255.0  255.255.255.0(255网段) # NAT 的使用场景        NAT的最初的目的是允许把私有IP地址映射到公网地址,以减缓IP地址空间的消耗。        当一个组织更换它的互联网服务提供商ISP,但不想更改内网配置方案时,NAT同样很有用途。        以下是适于使用NAT的多种情况:         企业内网接入Internet节省公网地址         单向访问         大方向:内网访问互联网(互联网上主机不能够访问内网主机)         小方向:同单位实现两个网段之间单向访问(涉密部门能够访问其他部门,反之不可)         增加一个网段          避免在主干路由器增加到这个网段的路由         在Windows上实现的NAT和端口映射 # 网络地址转换的类型        下面介绍一下NAT的三种类型。         静态NAT 是为了在私网地址和公网地址间,允许一对一映射而设计的。         或者IPv4和IPv6之间的转换(典型)         不节省公网地址,故公网地址的利用效率不高,         无任何安全性,外网可以通过公网地址直接攻击内网主机,好像只增加路由器的工作         适用场景类似代理,可以较方便的更换主机,而无需修改路由器的配置         故应用不够广泛...         动态NAT 可以实现映射一个未注册 IP地址到注册IP地址池中的一个注册IP地址。         多对一,或多对多         比较PAT优势:避免被误认为攻击而被封ip地址         不太节省地址,应用不广泛         复用是最流行的NAT配置类型,也被称为端口地址映射(PAT)。         通过使用PAT,可实现上千个用户仅通过一个真实的全球 IP地址连接到Internet。         缺点:增加延迟,消耗路由器性能 # 端口映射(port mapping) 允许Internet上的计算机通过企业路由器的公网IP地址访问到内网的服务器------------------------------------------------         

110,535

社区成员

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

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

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