72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
|
|
namespace ConsoleApplication2
|
|
{
|
|
class TCPServer
|
|
{
|
|
public static void Listen()
|
|
{
|
|
while (Program.running) {
|
|
TcpListener server = null;
|
|
try
|
|
{
|
|
Int32 port = 13754;
|
|
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
|
|
server = new TcpListener(localAddr, port);
|
|
server.Start();
|
|
Byte[] bytes = new Byte[256];
|
|
String data = null;
|
|
|
|
//Listening Loop
|
|
while (true)
|
|
{
|
|
Console.Write("Waiting on a connection...");
|
|
TcpClient client = server.AcceptTcpClient();
|
|
Console.WriteLine("Connected");
|
|
data = null;
|
|
|
|
NetworkStream stream = client.GetStream();
|
|
|
|
int i;
|
|
|
|
//Loop to receive all the data sent by the client
|
|
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
|
|
{
|
|
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
|
|
Console.WriteLine("Recieved: {0}", data);
|
|
|
|
data = data.ToLower();
|
|
|
|
data = NetParse.parse(data);
|
|
|
|
//Proccess the data
|
|
//data = Drives.DisplayDriveInfo();
|
|
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
|
|
|
|
//Send back a response
|
|
stream.Write(msg, 0, msg.Length);
|
|
Console.WriteLine("Sent: {0}", data);
|
|
}
|
|
client.Close();
|
|
}
|
|
}
|
|
catch (SocketException e)
|
|
{
|
|
Console.WriteLine("SocketException: {0}", e);
|
|
}
|
|
finally
|
|
{
|
|
server.Stop();
|
|
}
|
|
Console.WriteLine("\nHit enter to continue");
|
|
Console.Read();
|
|
}
|
|
}
|
|
}
|
|
}
|