public static int sendInitialMessage(int size, Int32 port, string address) { TcpClient client = null; string textToSend = size.ToString(); Console.WriteLine("connection setup"); while (true) { if (token.IsCancellationRequested) { try { token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { Console.WriteLine("Operation Canceled"); } return(-1); } try { client = new TcpClient(address, port); if (client.Connected) { Console.WriteLine("connection established"); break; } } catch (SocketException) { Console.WriteLine("Connection not established"); } } try { NetworkStream nwStream = client.GetStream(); byte[] bytesToSend = System.Text.ASCIIEncoding.ASCII.GetBytes(textToSend); //---send the text--- nwStream.Write(bytesToSend, 0, bytesToSend.Length); byte[] bytesToRead = new byte[client.ReceiveBufferSize]; //int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize); //Console.WriteLine("Received : " + System.Text.Encoding.ASCII.GetString(bytesToRead, 0, bytesRead)); } catch (System.IO.IOException e) { Console.WriteLine("Initial connection closed: " + e.Message); } client.Close(); return(0); }
private static void ListenToServer(int port) { UdpClient udpListener = new UdpClient(port); byte[] data = new byte[1024]; IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); TcpClient client; while (true) { data = udpListener.Receive(ref sender); string[] splitData = SplitData(data); client = new TcpClient(splitData[0], Int32.Parse(splitData[1])); NetworkStream ns = client.GetStream(); byte[] bytes2 = new byte[1024]; ns.Read(bytes2, 0, bytes2.Length); string msg = Encoding.ASCII.GetString(bytes2).Replace("\0", string.Empty); ns.Flush(); if (msg.Equals("Please enter your password\r\n")) { byte[] myWriteBuffer = Encoding.ASCII.GetBytes(splitData[2] + "\r\n"); ns.Write(myWriteBuffer, 0, myWriteBuffer.Length); ns.Flush(); } else { ns.Close(); client.Close(); } byte[] bytes3 = new byte[1024]; ns.Read(bytes3, 0, bytes3.Length); msg = Encoding.ASCII.GetString(bytes3).Replace("\0", string.Empty); ns.Flush(); if (msg.Equals("Access granted\r\n")) { byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Hacked by " + splitData[3].TrimEnd() + "\r\n"); ns.Write(myWriteBuffer, 0, myWriteBuffer.Length); ns.Flush(); } ns.Close(); client.Close(); } }
public static void sendTCPTransfer(int size, Int32 port, string address, bool isNagle, CancellationToken _token) { //---data to send to the server--- Byte[] msg = byteGenerator(size); NetworkStream nwStream = null; TcpClient client = null; //---create a TCPClient object at the IP and port no.--- try { client = new TcpClient(address, port); client.Client.NoDelay = isNagle; nwStream = client.GetStream(); } catch (SocketException) { Console.WriteLine("connection not established for: " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()); return; } System.Console.WriteLine(address + ":" + port.ToString() + " TCP: " + size.ToString() + " bytes" + " transfer UP"); while (true) { if (_token.IsCancellationRequested) { nwStream.Close(); client.Close(); break; } try { nwStream.Write(msg, 0, size); Thread.Sleep(10); } catch (System.IO.IOException) { System.Console.WriteLine("connection died"); return; } } }