Jump to content
  • Advertisement
Sign in to follow this  
miruim

[.net] Simplest Non-Threading Socket Loop (C#)?

This topic is 3447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'd like to make a loop for my MUD, but I don't know much about Sockets nor all the Synchronous/Asynchronous stuff. What would be the design of a simple loop that would include getting/sending info to every open socket in a given moment? Cam somebody write me up a simple game loop including the socket commands for the Asynchronous method? (To what I've understood, Asynchronous is the only solution if you're not Threading each socket right?) Thanks!

Share this post


Link to post
Share on other sites
Advertisement
If you are writing a MUD this is not just something you can gloss over. You are just going to have to suck it up and learn it.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
If you are writing a MUD this is not just something you can gloss over. You are just going to have to suck it up and learn it.

theTroll


I'd have to agree. I managed to get mine up and running pretty quick, but over time I learned it had several key deficiencies. It would appear to be working fine, but every now and then, under circumstances difficult (but not impossible) to reproduce, something would go wrong.

Even now, I'm not 100% positive that I have it right, but it appears to be working okay and I threw some pretty vigorous tests at it.

So don't get discouraged, but don't expect it to be a walk in the park. We're here to help as you need it, though.

Good luck!

Share this post


Link to post
Share on other sites
It's been awhile since I've done much with Sockets. I remember them being something of a pain, and I haven't touched them since. If you're looking to simplify things though, why not just make an AJAX browser based MUD? It would simplify the networking end immensely.

Share this post


Link to post
Share on other sites
That's one idea. Being able to make the MUD fancier with small graphics or maybe even sounds is very interesting, even though I'd prefer to keep it mainly text-based.

Even though I've already taken a brief look at the inside of some AJAX books on some bookstores, I don't know much about it.

How long would it take to know enough for coding up a server and a site to handle that?

Share this post


Link to post
Share on other sites
To deviate slightly, I never understood why people put graphical content in MUDs. It defeats the purpose. I always look at MUDs being the gaming equivalent of books. You'd never see ASCII art in a book. Why in a MUD?

That said, I'm all for graphical utilities. A roundtime bar and a room exit compass are invaluable tools I'd not want to play without.

Share this post


Link to post
Share on other sites
As far as the AJAX thing goes, I'd actually been tinkering with code to make an AJAX text adventure.

I think I might turn this into a tutorial at some point. For now, I'll post the code.

The server side is ASP.NET/C#. However, there's hardly any server side code at all. Most of the code is javascript. I'm sure it would be real easy to accomplish the same server side thing in PHP.

At any rate, all the server side code below does is send out a response to a command sent from javascript/Ajax.


namespace Advent
{
public partial class _Default : System.Web.UI.Page
{
private string CurrentSessionID
{
get
{
return Request["SessionID"];
}
}

private System.Web.SessionState.HttpSessionState CurrentSession
{
get
{
if (String.IsNullOrEmpty(CurrentSessionID))
{
return null;
}

if (Application[CurrentSessionID] != null)
{
return (System.Web.SessionState.HttpSessionState)Application[CurrentSessionID];
}

return null;
}
}

private string UserCommand
{
get
{
if (Request["command"] == null)
{
return "";
}

return (string)(Request["command"]).Trim().ToUpper();
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (UserCommand.Length > 0)
{
Response.Clear();
Response.Write("Result of command \"" + UserCommand + "\" from Session " + CurrentSessionID);
Response.End();
}
else
{
Application[Session.SessionID] = Session;
string startupScript = "";
startupScript += "var sessionID = '" + Session.SessionID + "';" + "\r\n";
//startupScript += "alert(sessionID)";
Page.ClientScript.RegisterStartupScript(typeof(Page), "startup", "<script>\r\n" + startupScript + "\r\n</script>");
}
}
}
}



Here's the client side.
It's a little more involved.
Basically, the markup contains a
, used for the text ("You are in a forest. A dog has bitten you. etc.) and a textbox (<input type="text" />) where the user enters their commands.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Advent._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function CheckEnter(ev)
{
var keyCode = null;
if(window.event)
{
keyCode = window.event.keyCode;
}
else if(ev)
{
keyCode = e.which;
}
else
{
return true;
}

if(keyCode == 13)
{
SendCommand();
return false;
}

return true;
}

function SendCommand()
{
var tbText = document.getElementById('tbCommand');
var command = Trim(tbText.value);
tbText.value = '';

if(command.length > 0)
{
var url = 'default.aspx?Command=' + escape(command) + '&SessionID=' + sessionID;
var oAjax = GetAjaxObject();
oAjax.onreadystatechange = function() { if(oAjax.readyState == 4) { ProcessCommandResponse(oAjax); } }
oAjax.open("POST", url, true);
oAjax.send('asdf');
}
}

function ProcessCommandResponse(oAjax)
{
var dvLog = document.getElementById('dvLog');
dvLog.innerHTML += oAjax.responseText + '<br />';
}

function GetAjaxObject()
{
var oAjax = null;
try
{
oAjax = new XMLHttpRequest();
}
catch(ex)
{
oAjax = new ActiveXObject("Microsoft.XMLHTTP");
}

return oAjax;
}

function Trim(str)
{
return str.replace(/^\s+|\s+$/g,"");
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div id="dvLog" style="background-color:Black; color:#00FF00; width:640px; height:480px; font-family:Courier New;">
</div>
<input id="tbCommand" style="width:640px;" type="text" onkeypress="return CheckEnter(event);" />
</form>

<script type="text/javascript">
</script>

</body>
</html>




When the user types something in the textbox, it checks to see what character was typed. If that character is the Enter key (ASCII 13), then it sends it off to the Server using Ajax. (Normally when you hit enter on a textbox, a postback is performed. Since we don't want a postback, we return false in this case, which cancels the brower's normal action.)

To send the command off to the server, we first create an Ajax object. Depending on which browser you're using, the command to do so is different. So, we wrap both in a try-catch. Whichever one succeeds is the correct command for the user's browser.

After you have an Ajax object, set the target url (passing the command that the user typed within that url), set a callback function, and send it to the server.

oAjax.onreadystatechange = function() { if(oAjax.readystate==4) { alert('Received data from the server!!!'); } }

This callback is called every time the Ajax object's "readystate" is changed. The only one you really care about is readystate 4. This means that the server has responded.

oAjax.responseText contains the response from the server.


Anyway, I'm sure that's a lot to chew on.
Like I said, I'll turn this into a tutorial at some point. For now, let me know if any (or all) of it doesn't make sense.

Share this post


Link to post
Share on other sites
There is a good book on the subject here:

TCP/IP Sockets in C#: Practical Guide for Programmers


http://www.amazon.com/TCP-IP-Sockets-Practical-Programmers/dp/0124660517/ref=sr_1_6?ie=UTF8&s=books&qid=1242345884&sr=8-6

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!