Exemple #1
0
        public string GetSocketId(string link)
        {
            var socket = new MySocket(link, AddressFamily.InterNetwork,
                                      SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(_ip);


            var splitLink = link.Split("\r\n\r\n");
            var imgSrc    = splitLink[0];

            imgSrc = Regex.Replace(imgSrc, @"\t|\n|\r", "");
            var extension = Path.GetExtension(imgSrc);

            if (!Directory.Exists(@"D:\images"))
            {
                var dir = Directory.CreateDirectory(@"D:\images");
            }

            //imgSrc = imgSrc.Trim();
            Console.WriteLine($"IMGSRC {imgSrc}");

            var requestBuilder = new StringBuilder();

            requestBuilder
            .Append($"GET {imgSrc} HTTP/1.1\r\nHost: unite.md\r\n")
            .Append("Accept-Language: en, ru, ro\r\n")
            .Append("Connection: keep-alive\r\n")
            .Append("Accept: text/html\r\n")
            .Append("User-Agent: CSharpTests\r\n\r\n");

            var byteCount = socket.Send(Encoding.UTF8.GetBytes(requestBuilder.ToString()));

            Console.WriteLine($"Byte count {byteCount}");

            var dataInBytes         = new byte[socket.ReceiveBufferSize * 4];
            int responseSizeInBytes = socket.Receive(dataInBytes);
            //Console.WriteLine($"Receive Buffer size: {dataInBytes.Length} | Response size in bytes: {responseSizeInBytes}");
            //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            var resp = string.Empty;

            //byte[] sizeBuf = new byte[4];
            //socket.Receive(sizeBuf, 0, sizeBuf.Length, 0);
            //var size = BitConverter.ToInt32(sizeBuf, 0);
            //byte[] buffer;
            //while(size > 0)
            //{
            //	if(size < socket.ReceiveBufferSize)
            //	{
            //		buffer = new byte[size];
            //	}
            //	else
            //	{
            //		buffer = new byte[socket.ReceiveBufferSize];
            //	}
            //	var rec = socket.Receive(buffer, 0, buffer.Length, 0);
            //	size -= rec;

            //	resp += Encoding.ASCII.GetString(buffer, 0, buffer.Length);
            //}

            //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            //var respBytes = new List<byte>();

            //while (socket.Receive(dataInBytes) > 0)
            //{
            //	resp += dataInBytes;
            //	respBytes.AddRange(dataInBytes);
            //}

            for (int i = 0; i < responseSizeInBytes; i++)
            {
                resp += $"{Convert.ToChar(dataInBytes[i]).ToString()}";
            }

            var index = resp.IndexOf("\r\n\r\n");

            resp = resp.Trim();
            //var index = FindIndex(respBytes, Encoding.UTF8.GetBytes("\r\n\r\n"));
            //if (index != -1)
            //{

            if (Directory.Exists(@"D:\images"))
            {
                using var outStream = new FileStream(Path.Combine(@"D:\images", $"{Path.GetFileNameWithoutExtension(imgSrc)}{Guid.NewGuid().ToString()}{extension}"), FileMode.Create);
                using var writer    = new BinaryWriter(outStream);
                for (int i = index + 4; i < resp.Length; i++)
                {
                    writer.Write((byte)resp[i]);
                    //writer.Write(respBytes[i]);
                }
            }

            //}
            AddSocket(socket);
            //socket.Close();

            return(socket.Id);
        }
Exemple #2
0
 public void AddSocket(MySocket s)
 {
     _socketCollection.Add(s);
 }