News:

Facebook killed the radio star. And by radio star, I mean the premise of distributed forums around the internet. And that got got by Instagram/SnapChat. And that got got by TikTok. Where the fuck is the internet we once knew?

Main Menu

My C# server O.O

Started by Warrior, February 02, 2006, 06:08:50 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Warrior

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

/*
* 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

/*
* 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

/*
* 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

zorm

Making a new thread for every connection is probably a bad idea.
"Frustra fit per plura quod potest fieri per pauciora"
- William of Ockham

Warrior

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

zorm

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

MyndFyre

Quote from: zorm on February 26, 2006, 10:00:59 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.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.