Author Topic: My C# server O.O  (Read 4415 times)

0 Members and 1 Guest are viewing this topic.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
My C# server O.O
« on: February 02, 2006, 06:08:50 pm »
I've been working on a server in C# to get my skills up. So I wrote a multiuser multithreaded server using TcpServer and TcpClient

I'm not going to post all the code but I will post where most of it is

Networking.cs
Code: [Select]
/*
 * User: Nelson
 * Date: 1/28/2006
 * Time: 9:07 PM
 */

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Server
{
/// <summary>
/// Description of Networking.
/// </summary>
class Networking
{
TcpListener server;
ServerUsers users;

public void Net_Init()
{
server = new TcpListener(1460); // Depreciated, fix later
users = new ServerUsers(); // 100 Users
while (true)
{
server.Start();

if (server.Pending())
{
TcpClient newConnection = server.AcceptTcpClient();
Console.WriteLine("Server has recieved a connection.");

users.AddUser(newConnection);
}
}
}
}
}

ServerUsers.cs
Code: [Select]
/*
 * User: Nelson
 * Date: 1/29/2006
 * Time: 3:04 AM
 */

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Server
{
/// <summary>
/// Description of ServerUsers.
/// </summary>
public class ServerUsers
{
public ServerUsers()
{
// Initiate some misc objects here
}

public void AddUser(TcpClient c)
{
/* What we are going to do here
* is spawn a thread to the protocol
* handler which is a temporary place
* holder and it should fill in a
* user information structure which
* we canthen use to access thier information
* while they are chatting.
*/

/* Additional notes: Once that is done an array
  * of UserInfo entries is kept here I guess
  */
 
Protocol tmpProto = new Protocol(c);
Thread myThread = new Thread(new ThreadStart(tmpProto.Run));
myThread.Start();

return;
}
}
}

Protocol.cs
Code: [Select]
/*
 * User: Nelson
 * Date: 1/30/2006
 * Time: 1:14 AM
 */

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Server
{
/// <summary>
/// Description of Protocol.
/// </summary>
public class Protocol
{
private TcpClient myClient;
private StreamReader SR;
private StreamWriter SW;

public Protocol(TcpClient c)
{
myClient = c;
SR = new StreamReader(myClient.GetStream());
SW = new StreamWriter(myClient.GetStream());
}

public void Run()
{
Console.WriteLine("Node spawned new thread, ready to commence protocol.");
SW.WriteLine("Hello world from C# Server!");
SW.Flush();
}
}
}

I'm going to extend this to include a protocol (probably textbased, may go binary using SW.Write())
I'm actually surprised at how simple it was.

Looking for suggestions/comments from people like Myndfyre and other .NET guru's
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline zorm

  • Hero Member
  • *****
  • Posts: 591
    • View Profile
    • Zorm's Page
Re: My C# server O.O
« Reply #1 on: February 26, 2006, 10:00:59 pm »
Making a new thread for every connection is probably a bad idea.
"Frustra fit per plura quod potest fieri per pauciora"
- William of Ockham

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: My C# server O.O
« Reply #2 on: February 26, 2006, 10:13:21 pm »
I don't know of a better way. (Really havn't written many servers) but I assumed that was the way to go about it.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline zorm

  • Hero Member
  • *****
  • Posts: 591
    • View Profile
    • Zorm's Page
Re: My C# server O.O
« Reply #3 on: February 26, 2006, 11:19:32 pm »
http://www.runuo.com/files/uploads/runuo-releases/RunUO-1.0.0-Source.zip is a good example of a server. Most of the code in the Network folder is clean enough to be understandable.
"Frustra fit per plura quod potest fieri per pauciora"
- William of Ockham

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: My C# server O.O
« Reply #4 on: February 27, 2006, 04:41:14 pm »
Making a new thread for every connection is probably a bad idea.

Depends on server load (although high load will probably be a quick DoS), but .Select() is better.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.