All pastes #963848 Raw Edit

Something

public text v1 · immutable
#963848 ·published 2008-03-30 21:12 UTC
rendered paste body
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class server
{
    public static void Main()
    {
        startup();
    }

    public static void startup()
    {
        string data;
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 1337);

        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        newsock.Bind(ipep);
        newsock.Listen(10);
        Console.WriteLine("Waiting for a client...");

        Socket client = newsock.Accept();

        NetworkStream networkstream = new NetworkStream(client);
        StreamReader recive = new StreamReader(networkstream);

        IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
        Console.WriteLine("Connected with {0} at port {1}", newclient.Address, newclient.Port);

        sendline("connection accepted", networkstream, false);
        while (true)
        {
            try
            {
                data = recive.ReadLine();
            }
            catch (IOException)
            {
                break;
            }

            if (data == "test")
            {
                sendline("test", networkstream, false);
            }
            else if (data == "shutdown")
            {
                sendline("server is shutting down", networkstream, true);
                break;
            }
            else
            {
                sendline("that command doesnt exist", networkstream, false);
            }
        }
        Console.WriteLine("Disconnected");

        recive.Close();
        networkstream.Close();
    }

    public static void sendline(string line, NetworkStream stream, bool shutdown)
    {
        StreamWriter send = new StreamWriter(stream);
        if (line != "")
        {
            send.WriteLine(line);
            send.Flush();
        }
        if (shutdown)
        {
            send.Close();
        }
    }
}