70 lines
1.8 KiB
C#
70 lines
1.8 KiB
C#
using System;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
class Client
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
if (args.Length == 0)
|
|
{
|
|
Console.WriteLine("Zadejte IP serveru.");
|
|
return;
|
|
}
|
|
|
|
TcpClient client = new TcpClient();
|
|
|
|
try
|
|
{
|
|
client.Connect(args[0], 65500);
|
|
Console.WriteLine("Připojeno k serveru!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Nepodařilo se připojit: " + ex.Message);
|
|
}
|
|
|
|
NetworkStream stream = client.GetStream();
|
|
|
|
Thread receiveThread = new Thread(() =>
|
|
{
|
|
byte[] buffer = new byte[4096];
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
int read = stream.Read(buffer, 0, buffer.Length);
|
|
if (read == 0)
|
|
break;
|
|
Console.Clear();
|
|
Console.Write(Encoding.UTF8.GetString(buffer, 0, read));
|
|
}
|
|
catch
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
receiveThread.Start();
|
|
|
|
while (true)
|
|
{
|
|
var key = Console.ReadKey(true).Key;
|
|
string move = key switch
|
|
{
|
|
ConsoleKey.W => "W",
|
|
ConsoleKey.S => "S",
|
|
ConsoleKey.A => "A",
|
|
ConsoleKey.D => "D",
|
|
_ => "",
|
|
};
|
|
if (!string.IsNullOrEmpty(move))
|
|
{
|
|
byte[] data = Encoding.UTF8.GetBytes(move);
|
|
stream.Write(data, 0, data.Length);
|
|
}
|
|
}
|
|
}
|
|
}
|