Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual C#中的套接字。需要帮助!_C#_Sockets - Fatal编程技术网

Visual C#中的套接字。需要帮助!

Visual C#中的套接字。需要帮助!,c#,sockets,C#,Sockets,我来自乌克兰,英语很差,但我不确定我的问题是否有答案 我从[这里][1]举了一个例子,但我有一个例外,GZip神奇数字是无效的,为什么 public long WriteUrl() { long num1 = 0; bool saveItAtCache = false; bool existsAtCache = false; byte[] cachedFile = null; string ext =

我来自乌克兰,英语很差,但我不确定我的问题是否有答案

我从[这里][1]举了一个例子,但我有一个例外,GZip神奇数字是无效的,为什么

    public long WriteUrl()
    {
        long num1 = 0;
        bool saveItAtCache = false;
        bool existsAtCache = false;
        byte[] cachedFile = null;
        string ext = Path.GetExtension(_url).ToLower();
        if (!_url.Contains(".php") && ".gif.jpg.swf.js.css.png.html".IndexOf(ext) != -1 && ext != "")
        {
            saveItAtCache = true;
            cachedFile = cache.GetFile(_url);
            existsAtCache = (cachedFile != null);
        }
        if (existsAtCache)
        {
            writeSuccess(cachedFile.Length, null);
            socket.Send(cachedFile);
        }
        string host = new Uri(_url).Host;
        IPHostEntry ipAddress = Dns.GetHostEntry(host);
        IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80);
        using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
        {
            s.Connect(ip);
            using (NetworkStream n = new NetworkStream(s))
            {
                if (HttpRequestType == "GET")
                {
                    SendRequest(n, new[] { socketQuery});
                }
                Dictionary<string, string> headers = new Dictionary<string, string>();
                while (true)
                {
                    string line = ReadLine(n);
                    if (line.Length == 0)
                    {
                        break;
                    }
                    int index = line.IndexOf(':');
                    if (!headers.ContainsKey(line.Substring(0, index)))
                    {
                        headers.Add(line.Substring(0, index), line.Substring(index + 2));
                    }
                }

                string contentEncoding;
                if (headers.TryGetValue("Content-Encoding", out contentEncoding))
                {
                    Stream responseStream = n;
                    if (contentEncoding.Equals("gzip"))
                    {
                        responseStream = new GZipStream(responseStream, CompressionMode.Decompress, true);
                    }
                    else if (contentEncoding.Equals("deflate"))
                    {
                        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
                    }

                    var memStream = new MemoryStream();

                    var respBuffer = new byte[4096];
                    try
                    {
                        int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                        //int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                        while (bytesRead > 0)
                        {
                            memStream.Write(respBuffer, 0, bytesRead);
                            bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                        }
                    }
                    finally
                    {
                        responseStream.Close();
                    }
                    string str = encoding.GetString(memStream.ToArray());
                    ManageCookies(headers["Set-Cookie"], _headers["Host"]);
                    cachedFile = encoding.GetBytes(str);
                    if (saveItAtCache)
                    {
                        cache.Store(_url, cachedFile);
                    }
                    writeSuccess(cachedFile.Length, headers["Set-Cookie"]);
                    socket.Send(cachedFile);
                    num1 = str.Length;
                }
                else
                {
                    while (true)
                    {
                        string line = ReadLine(n);
                        if (line == null)
                        {
                            break;
                        }
                        num1 = line.Length;
                    }
                }
            }
        }

        return num1;
    }
public long WriteUrl()
{
长num1=0;
bool saveItache=false;
bool existsAtCache=false;
字节[]cachedFile=null;
string ext=Path.GetExtension(_url).ToLower();
如果(!\u url.Contains(“.php”)&&&“.gif.jpg.swf.js.css.png.html”.IndexOf(ext)!=-1&&ext!=”)
{
saveItache=true;
cachedFile=cache.GetFile(\u url);
existsAtCache=(cachedFile!=null);
}
如果(existsAtCache)
{
writeSuccess(cachedFile.Length,null);
socket.Send(缓存文件);
}
字符串host=newURI(url).host;
IPHostEntry ipAddress=Dns.GetHostEntry(主机);
IPEndPoint ip=新IPEndPoint(ipAddress.AddressList[0],80);
使用(sockets=newsocket(ip.AddressFamily,SocketType.Stream,ProtocolType.Tcp))
{
s、 连接(ip);
使用(网络流n=新网络流)
{
if(HttpRequestType==“GET”)
{
SendRequest(n,new[]{socketQuery});
}
字典头=新字典();
while(true)
{
字符串行=读取行(n);
如果(line.Length==0)
{
打破
}
int index=line.IndexOf(“:”);
if(!headers.ContainsKey(line.Substring(0,index)))
{
添加(第行子字符串(0,索引),第行子字符串(索引+2));
}
}
字符串内容编码;
if(headers.TryGetValue(“内容编码”,out contentEncoding))
{
流响应流=n;
if(contentEncoding.Equals(“gzip”))
{
responseStream=新的GZipStream(responseStream,压缩模式。解压缩,真);
}
else if(contentEncoding.Equals(“deflate”))
{
responseStream=新的放气气流(responseStream,压缩模式。解压缩);
}
var memStream=new MemoryStream();
var respBuffer=新字节[4096];
尝试
{
int bytesRead=responseStream.Read(respBuffer,0,respBuffer.Length);
//int bytesRead=responseStream.Read(respBuffer,0,respBuffer.Length);
而(字节读取>0)
{
memStream.Write(respBuffer,0,bytesRead);
bytesRead=responseStream.Read(respBuffer,0,respBuffer.Length);
}
}
最后
{
responseStream.Close();
}
string str=encoding.GetString(memStream.ToArray());
ManageCookies(标题[“设置Cookie”],_标题[“主机”]);
cachedFile=encoding.GetBytes(str);
if(saveItache)
{
cache.Store(url,cachedFile);
}
writeSuccess(cachedFile.Length,头[“设置Cookie]”);
socket.Send(缓存文件);
num1=结构长度;
}
其他的
{
while(true)
{
字符串行=读取行(n);
如果(行==null)
{
打破
}
num1=直线长度;
}
}
}
}
返回num1;
}
在这些行中

                string str = encoding.GetString(memStream.ToArray());
                ManageCookies(headers["Set-Cookie"], _headers["Host"]);
                cachedFile = encoding.GetBytes(str);

将字节数组转换为字符串,然后再转换回字节数组。因为原始数据是gzip或jpg或其他任何东西,而不是真正的字符串,所以这种转换可能会把它搞砸。我根本看不到你在使用str,所以把它拿出来(需要长度时使用cachedFile.Length而不是str.Length)。

你的链接在这个问题上不起作用。为什么你确定客户端请求的格式正确?什么客户端使用GZIP上传?