protected virtual void Dispose(bool disposing) { lock (this) { if (!_disposed) { _disposed = true; if (disposing) { } try { if (null != _socket) { _socket.Close(); } if (null != _lock) { _lock.Close(); } } catch (Exception) { } } } }
private async void Connect(IPAddress ip) { if (connection?.Connected ?? false) { var result = MessageBox.Show("이미 서버에 연결되어 있습니다."); return; } IPEndPoint remoteEp = new IPEndPoint(ip, 52217); try { Socket server = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await server.ConnectAsync(remoteEp); connection = new SocketEx(server); await connection.SendMessageAsync(new CS_Login { UserName = "******" }); await HandleReceiveAsync(); user = new User("yhj"); } catch (Exception ex) { MessageBox.Show($"서버 연결에 실패하였습니다. {ex.Message}"); connection?.Close(); connection = null; } }
protected override void Dispose(bool disposing) { lock (this) { if (!_disposed) { _disposed = true; if (disposing) { } try { if (null != _stream) { _stream.Close(); } if (null != _socket) { _socket.Close(); } } catch { } } } }
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(); } }
private async void Connect(IPAddress ip) // 이 작업 끝나기 전까지는 다른 작업 안함. { if (connection?.Connected ?? false) { var result = MessageBox.Show("Already connected. Reconnect?", "", MessageBoxButtons.YesNo);// 재 연결 하시겠습니까? if (result == DialogResult.No) { return; } connection.Disconnect(false); connection.Close(); connection = null; } //IPAddress ipAddress = IPAddress.Parse("221.143.21.37"); IPEndPoint remoteEP = new IPEndPoint(ip, 52217); // Create a TCP/IP socket. try { Socket server = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await server.ConnectAsync(remoteEP); //richTextBox1.Text += $"Connected: {server.Connected} \n"; connection = new SocketEx(server); await connection.SendMessageAsync(new CS_Login { UserName = "******", }); await HandleReceiveAsync(); } catch (Exception ex) { richTextBox1.Text += "Error: " + ex.Message; connection?.Close(); connection = null; } }
/// <exception cref="System.IO.IOException"></exception> protected internal override void DoDisconnect(bool hard) { try { foreach (var ssn in Sessions) { ssn.Logoff(hard); } Out.Close(); In.Close(); Socket.Close(); } finally { Digest = null; Socket = null; TconHostName = null; } }
internal virtual void TryClose() { lock (_lock) { if (_socket != null) { _socket.Close(); _socket = null; } _thread = null; if (_waitResponse) { _responseTable.Clear(); } else { _autoResetWaitReceive.Set(); } } }
private async void Connect(IPAddress ip) { if (connection?.Connected ?? false) { var result = MessageBox.Show("Already connected. Reconnect?", "", MessageBoxButtons.YesNo); if (result == DialogResult.No) { return; } connection.Disconnect(false); connection.Close(); connection = null; } IPEndPoint remoteEP = new IPEndPoint(ip, 52217); // Create a TCP/IP socket. try { Socket server = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await server.ConnectAsync(remoteEP); connection = new SocketEx(server); await connection.SendMessageAsync(new CS_Login { UserName = "******", }); await HandleReceiveAsync(); } catch (Exception ex) { ChattingListTextBox.Text += "Error: " + ex.Message; connection?.Close(); connection = null; } }
/// <exception cref="System.IO.IOException"></exception> internal virtual void Ssn139() { Name calledName = new Name(Address.FirstCalledName(), 0x20, null ); do { Socket = new SocketEx(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (LocalAddr != null) { Socket.Bind2(new IPEndPoint(LocalAddr, LocalPort)); } Socket.Connect(new IPEndPoint(IPAddress.Parse(Address.GetHostAddress()), 139), SmbConstants.ConnTimeout); Socket.SoTimeOut = SmbConstants.SoTimeout; Out = Socket.GetOutputStream(); In = Socket.GetInputStream(); SessionServicePacket ssp = new SessionRequestPacket(calledName, NbtAddress.GetLocalName ()); Out.Write(Sbuf, 0, ssp.WriteWireFormat(Sbuf, 0)); if (Readn(In, Sbuf, 0, 4) < 4) { try { Socket.Close(); } catch (IOException) { } throw new SmbException("EOF during NetBIOS session request"); } switch (Sbuf[0] & 0xFF) { case SessionServicePacket.PositiveSessionResponse: { if (Log.Level >= 4) { Log.WriteLine("session established ok with " + Address); } return; } case SessionServicePacket.NegativeSessionResponse: { int errorCode = In.Read() & 0xFF; switch (errorCode) { case NbtException.CalledNotPresent: case NbtException.NotListeningCalled: { Socket.Close(); break; } default: { Disconnect(true); throw new NbtException(NbtException.ErrSsnSrvc, errorCode); } } break; } case -1: { Disconnect(true); throw new NbtException(NbtException.ErrSsnSrvc, NbtException.ConnectionRefused ); } default: { Disconnect(true); throw new NbtException(NbtException.ErrSsnSrvc, 0); } } }while ((calledName.name = Address.NextCalledName()) != null); throw new IOException("Failed to establish session with " + Address); }
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); } }