Ok gettin this thread back on subject and why I started it in the first place:
I have a really simple proof of concept lua socket example that I threw in an alias named testConnect. My goal is to establish a TCP socket connection from MUSH client to a simple TCP server I wrote in C# and thus message the MUSH client from the server. After the socket connection is established, I'd like for the socket to perpetually listen for any messages from the server. I've been able to establish this socket connection between MUSH and my custom TCP C# server, but am struggling to progress beyond that. I've encountered 2 issues.
1. if I run it in an alias and use the while loop to receive messages, MUSH basically hangs in that while loop. Is there a way to run an alias or this method in a parallel Lua thread that doesn't impact MUSHclient and its UI.
2. I can't seem to get LuaSocket to receive multiple messages or at least output them until I sever the socket connection by shutting down the TCP server.
I found this thread where another user seemed to encounter a similar issue with respect to the whole blocking issue: http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=11344
<aliases>
<alias
script="testConnect"
match="testconnect"
enabled="y"
send_to="12"
sequence="100"
>
</alias>
</aliases>
Below is the Lua in my script file that the alias calls.
function testConnect(name,line,wildscards)
local socket = require("socket")
host = "localhost"
port = 3000
Note("Attempting connection to host '" ..host.. "' and port " ..port.. "...")
c = assert(socket.connect(host, port))
Note("Connected!")
--l = io.read()
l = "this is from MUSHClient test";
assert(c:send(l.."\n"))
l, e = c:receive()
while not e do
Note(l)
l, e = c:receive()
end
Note("end of testConnect()")
end
The below is a simple C# TCP server.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Collections;
namespace ClanServer
{
public class ArcticChar
{
public ArcticChar(NetworkStream pArg)
{
socketStream = pArg;
}
public int currentMV { get; set; }
public int maxMV { get; set; }
public int currentHP { get; set; }
public int maxHP { get; set; }
public int charClass { get; set; }
public int charName { get; set; }
public NetworkStream socketStream { get; set; }
}
//http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server
class ClanServer2
{
private TcpListener tcpListener;
private Thread listenThread;
private ArrayList clientList;
public ClanServer2()
{
this.tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 3000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
Console.WriteLine("Awaiting new connections, server started...");
}
private void ListenForClients()
{
this.tcpListener.Start();
clientList = new ArrayList();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
Console.WriteLine("New connection received...");
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
clientList.Add(new ArcticChar(clientStream));
byte[] message = new byte[4096];
int bytesRead;
byte[] outboundMsg = new byte[4096];
outboundMsg = new ASCIIEncoding().GetBytes("here welcome message from server\r\n");
int bytesSent = outboundMsg.Length;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
clientStream.Write(outboundMsg, 0, bytesSent);
clientStream.Flush();
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
Console.WriteLine("the client has disconnected from the server");
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
//System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
Console.WriteLine(encoder.GetString(message, 0, bytesRead));
}
tcpClient.Close();
}
public static int Main(String[] args)
{
ClanServer2 server = new ClanServer2();
return 0;
}
}
}
|