System.Net.Sockets.SocketException:远程主机易做图关闭了一个现有的连接(寻找错误解决答案)
System.Net.Sockets.SocketException:远程主机易做图关闭了一个现有的连接在System.Net.Sockets.Socket.Receive(Byte[] buffer,Int32 offset,Int32 size,SocketFlags socketFlags);
在System.Net.Sockets.Socket.Receive(Byte[] buffer,Int32 size,SocketFlags socketFlags);
在WebProxy.Proxy.Run()位置:行64
行64为: Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
一下是代码,有谁知道是什么原因不?
Proxy.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace WebProxy
{
class Proxy
{
Socket clientSocket;
Byte []read=new Byte [1024];//定义一个空间,存储来自客户端请求数据包
Byte[] Buffer = null;
Encoding ASCII = Encoding.ASCII;//设定编码
Byte [] RecvBytes=new Byte [4096];//定义一个空间,存储Web服务器返回的数据
public Proxy(Socket socket)
{
this.clientSocket = socket;
}
public void Run()
{
string clientmessage = "";//存放来自客户端的HTTP请求字符串
string URL = "";//存放解析出地址请求信息
int bytes = ReadMessage(read,ref clientSocket ,ref clientmessage );
if (bytes ==0)
{
return ;
}
int index1 = clientmessage.IndexOf(' ');
int index2 = clientmessage.IndexOf(' ',index1 +1);
if ((index1 ==-1)||(index2 ==-1))
{
throw new IOException ();
}
string part1 = clientmessage.Substring(index1 +1,index2 -index1 );
int index3 = part1.IndexOf('/', index1 + 8);//检索HTTP:@//
int index4 = part1.IndexOf(' ',index1 +8);
int index5 = index4 - index3;
URL = part1.Substring(index1 +4,(part1 .Length -index5 )-8);
try
{
IPHostEntry IPHost = Dns.Resolve(URL);
Console.WriteLine("远程主机名:" + IPHost.HostName);
string[] aliases = IPHost.Aliases;
IPAddress[] address = IPHost.AddressList;
Console.WriteLine("Web服务器IP地址:" + address[0]);//解析出要访问的服务器地址
IPEndPoint ipEndpoint = new IPEndPoint(address[0], 80);
Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//创建连接Web服务器端的Socket对象
IPsocket.Connect(ipEndpoint);//Socket连Web接服务器
if (IPsocket.Connected)
{
Console.WriteLine("Socket正确连接!");
}
string GET = clientmessage;
Byte[] ByteGet = ASCII.GetBytes(GET);
IPsocket.Send(ByteGet, ByteGet.Length, 0);//代理访问软件对服务器端传送HTTP请求命令
Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);//代理访问软件接收来自Web服务器端的反馈信息
Console.WriteLine("接收到字节数:" + rBytes.ToString());
String strRetPage = null;
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
while (rBytes > 0)
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
}
IPsocket.Shutdown(SocketShutdown.Both);
IPsocket.Close();
SendMessage(clientSocket, strRetPage); //代理服务软件往客户端传送接收到的信息
}
catch (Exception exc2)
{
Console.WriteLine(exc2 .ToString ());
}
}
//接收客户端的HTTP请求数据
private int ReadMessage(byte [] ByteArray,ref Socket s,ref String clientmessage)
{
int bytes = s.Receive(ByteArray ,1024,0);
string messageformclient = Encoding.ASCII.GetString(ByteArray );
clientmessage = (String)messageformclient;
return bytes;
}
using System;
using System.Collections.Generic;
using System.Text;
using System ;
using System.Net ;
using System.Net.Sockets ;
using System.Text ;
using System.IO ;
using System.Threading ;
using WebProxy;
namespace WebProxy
{
class Program
{
static void Main(string[] args)
{
const int port = 8000;
//定义端口号
TcpListener tcplistener = new TcpListener(port);
Console.WriteLine("侦听端口号: " + port.ToString());
tcplistener.Start();
//侦听端口号
while (true)
{
Socket socket = tcplistener.AcceptSocket();
//并获取传送和接收数据的Scoket实例
Proxy proxy = new Proxy(socket);
//Proxy类实例化
Thread thread = new Thread(new ThreadStart(proxy.Run));
//创建线程
thread.Start();
//启动线程
}
}
}
}
private void SendMessage(Socket s, string message)
{
Buffer =new Byte [message .Length +1];
int length = ASCII.GetBytes(message,0,message .Length,Buffer ,0);
Console.WriteLine("传送的字节数"+length .ToString ());
s.Send(Buffer ,length,0);
}
}
}
--------------------编程问答-------------------- up --------------------编程问答-------------------- 对方的进程异常中止就会引发这个异常!
你要捕获这个异常并处理。 --------------------编程问答-------------------- 怎么去 捕获这个异常呢 --------------------编程问答-------------------- 我刚好碰到过这个问题!在ReadMessage用一个
try
{
int bytes = s.Receive(ByteArray ,1024,0);
string messageformclient = Encoding.ASCII.GetString(ByteArray );
clientmessage = (String)messageformclient;
return bytes;
}
catch(SocketException e)
{
messageBox.Show(e.Message);
} --------------------编程问答-------------------- 路过,不懂,帮顶一下 --------------------编程问答-------------------- 对方SOCKET关闭提示异常 --------------------编程问答-------------------- 遇到相同问题,帮顶! --------------------编程问答-------------------- 兄弟有答案了,分享一下,谢谢 --------------------编程问答-------------------- 有可能不是代码的问题。
而是安全策略配置的问题。
--------------------编程问答--------------------
怎么配置?? --------------------编程问答-------------------- 可能是防火墙的问题。关闭防火墙、代理等试试 --------------------编程问答-------------------- 可能出现这个异常的原因很多
如LSSSS所说的 安全策略,防火墙,代理等等
不一定是server断开连接的原因
补充:.NET技术 , C#