internal void SendCommand(int timeout, string command) { CheckDisposed(); if (null == command) { throw new ArgumentNullException("command", "Value cannot be null."); } command = AppendCRLF(command); SetProgress(true); _response = null; try { _socket.SendTimeout = timeout; byte[] cmdBytes = _encoding.GetBytes(command); int startPos = 0; while (startPos < cmdBytes.Length) { startPos += _socket.Send(cmdBytes, startPos, cmdBytes.Length - startPos); } OnCommandSent(command); } catch (SocketException e) { CheckDisposed(); CheckTimeoutException(e); throw; } catch (Exception) { CheckDisposed(); throw; } catch { CheckDisposed(); throw; } finally { SetProgress(false); } }
private void btnRequest_Click(object sender, System.EventArgs e) { SocketEx sock = null; try { Uri reqUri = new Uri(txtURL.Text); string host = reqUri.Host; int port = reqUri.Port; string path = reqUri.PathAndQuery; sock = new SocketEx(_proxyType, _proxyServer, _proxyPort, _proxyUser, _proxyPwd); //configure preauthenticate sock.PreAuthenticate = _preAuthenticate; sock.Connect(host, port); string cmd = "GET " + path + " HTTP/1.0\r\n" + "Host: " + host + "\r\n\r\n"; sock.Send(_usedEnc.GetBytes(cmd)); //simple reading loop //read while have the data try { byte[] data = new byte[32 * 1024]; while (true) { int dataLen = sock.Receive(data); if (0 == dataLen) { break; } txtRes.Text += _usedEnc.GetString(data, 0, dataLen); } } catch (Exception ex) { txtRes.Text += Environment.NewLine + ex.ToString(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception caught!"); } if (null != sock) { sock.Close(); } }
public TrackerResponse MakeWebRequest(Uri uriQuest, string httpProtocol, string headers) { Encoding encoder = Encoding.GetEncoding(0x4e4); Socket = new SocketEx(Proxy, ProxyServer, ProxyPort, ProxyUser, ProxyPassword); Socket.SetTimeout(0x30d40); Socket.PreAuthenticate = false; log.Info($"Connecting to {uriQuest.Host}:{uriQuest.Port}"); for (int i = 0; i < 5; i++) { try { Socket.Connect(uriQuest.Host, uriQuest.Port); log.Info("Connected Successfully"); break; } catch (Exception ex) { log.Warn(ex); log.Warn("Failed connection attempt: " + i); } } if (!Socket.Connected) { log.Error("Unable to connect. Quitting..."); return(null); } log.Info("======== Sending Command to Tracker ========"); string cmd = "GET " + uriQuest.PathAndQuery + " " + httpProtocol + "\r\n" + headers.Replace("{host}", uriQuest.Host) + "\r\n"; Socket.Send(encoder.GetBytes(cmd)); try { byte[] data = new byte[32 * 1024]; using (MemoryStream memStream = new MemoryStream()) { int dataLen = Socket.Receive(data); while (dataLen > 0) { memStream.Write(data, 0, dataLen); dataLen = Socket.Receive(data); } if (memStream.Length == 0) { log.Info("Error : Tracker Response is empty"); return(null); } TrackerResponse trackerResponse = new TrackerResponse(memStream); memStream.Close(); Socket.Close(); if (trackerResponse.doRedirect) { return(MakeWebRequest(new Uri(trackerResponse.RedirectionURL), httpProtocol, headers)); } log.Info("======== Tracker Response ========"); log.Info(trackerResponse.Headers.ToString()); if (trackerResponse.Dico == null) { log.Warn("*** Failed to decode tracker response :"); log.Warn(trackerResponse.Body); } return(trackerResponse); } } catch (Exception ex) { Socket.Close(); log.Error(ex); return(null); } }
/// <exception cref="System.IO.IOException"></exception> internal virtual void Send(NameServicePacket request, NameServicePacket response, int timeout) { int nid = 0; int max = NbtAddress.Nbns.Length; if (max == 0) { max = 1; } lock (response) { while (max-- > 0) { try { lock (_lock) { request.NameTrnId = GetNextNameTrnId(); nid = request.NameTrnId; response.Received = false; _responseTable.Put(nid, response); EnsureOpen(timeout + 1000); int requestLenght = request.WriteWireFormat(_sndBuf, 0); _socket.Send(_sndBuf, 0, requestLenght, new IPEndPoint(request.Addr, _lport)); if (_log.Level > 3) { _log.WriteLine(request); Hexdump.ToHexdump(_log, _sndBuf, 0, requestLenght); } } if (_waitResponse) { long start = Runtime.CurrentTimeMillis(); while (timeout > 0) { Runtime.Wait(response, timeout); if (response.Received && request.QuestionType == response.RecordType) { return; } response.Received = false; timeout -= (int)(Runtime.CurrentTimeMillis() - start); } } } catch (Exception ie) { throw new IOException(ie.Message); } finally { //Sharpen.Collections.Remove(responseTable, nid); if (_waitResponse) { _responseTable.Remove(nid); } } if (_waitResponse) { lock (_lock) { if (NbtAddress.IsWins(request.Addr) == false) { break; } if (request.Addr == NbtAddress.GetWinsAddress()) { NbtAddress.SwitchWins(); } request.Addr = NbtAddress.GetWinsAddress(); } } } } }