Created
February 23, 2017 01:59
-
-
Save realduke2000/67cbd9fc52e42bc8d28735744eca291b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.IO; | |
using System.Threading; | |
namespace Tcp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
#if DEBUG | |
Console.WriteLine("attach debugger"); | |
Console.ReadKey(true); | |
#endif | |
StartTcp(args); | |
} | |
catch(Exception ex) | |
{ | |
Usage(); | |
#if DEBUG | |
Console.WriteLine(ex.ToString()); | |
#endif | |
} | |
} | |
static string cmdInput() | |
{ | |
Console.SetCursorPosition(0, Console.WindowHeight); | |
string cmd = Console.ReadLine() + "\n"; | |
Console.SetCursorPosition(0, 0); | |
return cmd; | |
} | |
static TcpClient tcpClient = null; | |
static void OutputScreen() | |
{ | |
try | |
{ | |
byte[] buffer; | |
while (tcpClient.Connected) | |
{ | |
// read and echo | |
int len = tcpClient.Available; | |
if (len > 0) | |
{ | |
// CAN READ | |
buffer = new byte[len]; | |
int read = tcpClient.GetStream().Read(buffer, 0, len); | |
string recv = Encoding.Default.GetString(buffer, 0, read); | |
if (Console.CursorLeft == 2) | |
{ | |
// Cheating, assume cursorLeft==2 mean we have writen ">>" | |
// Clear line and give back later | |
Console.CursorLeft = 0; | |
Console.Write(" "); | |
Console.CursorLeft = 0; | |
Console.Write(recv); | |
Console.Write(">>"); | |
} | |
else | |
{ | |
Console.Write(recv); | |
} | |
} | |
} | |
} | |
catch(Exception ex) | |
{ | |
#if DEBUG | |
Console.WriteLine(ex.ToString()); | |
#endif | |
} | |
Console.WriteLine("Output session is disconnected..."); | |
} | |
static void InputCmd() | |
{ | |
// write | |
// BUGBUG: user are not able to send special key | |
// Force append '\n' | |
byte[] buffer = null; | |
while (tcpClient.Connected) | |
{ | |
Console.Write(">>"); | |
string send = Console.ReadLine() + "\n"; | |
buffer = Encoding.Default.GetBytes(send); | |
tcpClient.GetStream().Write(buffer, 0, buffer.Length); | |
} | |
} | |
static void StartTcp(string[] args) | |
{ | |
if (args.Length < 2) | |
{ | |
Usage(); | |
return; | |
} | |
int port = -1; | |
IPAddress ip = null; | |
if (!int.TryParse(args[1], out port)) | |
{ | |
Usage(); | |
return; | |
} | |
if (IPAddress.TryParse(args[0], out ip)) | |
{ | |
tcpClient = new TcpClient(); | |
tcpClient.Connect(ip,port); | |
} | |
else | |
{ | |
tcpClient = new TcpClient(); | |
tcpClient.Connect(args[0], port); | |
} | |
try | |
{ | |
Console.Clear(); | |
Thread tOutput = new Thread(new ThreadStart(OutputScreen)); | |
tOutput.Start(); | |
Thread tInput = new Thread(new ThreadStart(InputCmd)); | |
tInput.Start(); | |
tOutput.Join(); | |
tInput.Join(); | |
} | |
finally | |
{ | |
tcpClient.Close(); | |
} | |
} | |
static void Usage() | |
{ | |
Console.WriteLine("Usage: Tcp.exe [ip|hostname] [port]"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment