Thursday, April 26, 2012

Now its getting easier

I was just about to do a huge addition of the Zombie Yoga server by adding the ability to send of generic data to be saved into XML. As it turns out, the latest assignment uses the same concept. I often times use header bytes, usually reserved to 8 bytes to let the server/client know how the treat the data. So things such as packet number and packet type are stored within the first 8 bytes, usually through ints (series of chars makes the packets bloated, larger). It a little bit like how the latest assignment is setup, the only difference is that the signifyers are not sitting in the actual queue but are actually part of the end packet.
queues are great especially running the server and client using threads. For all the information necessary to send over, a  queue buffers up any items which need to be sent over from all the different threads and a main send thread handles the send by dequeing the items one by one as soon as they are pushed to the  queue . Its a neat way to keep things organized and avoid stalling the software in case there is any waiting time on sending the packets.
One odd thing I found about sending stuff over a c# socket is that I have to add stall time; I usually have a blocking for loop before each send. If I send the packets out too fast, the receiving end never gets them. Makes me wonder if it has to do with the way Microsoft's .net library handles sockets; maybe it would be a good idea to pull the stream out and write to it instead of write to the socket directly (probably does the same thing, but I cant be sure of that). I know java doesn't suffer of the same problem, at least on the android platform, so there must be a work around.

Saturday, April 14, 2012

C# to Java stuff

Well, it looks like I have a few new discoveries. First of all, I got the network stuff to work, it was a flag I had to define in an XML file within the APP which marks it as "network using" or something like that.

Interesting discovery today. As it turns out, when you send bytes from C# to Java, you lose a byte on the front of the stream. As a result I ended up with 0000 since the front byte never made it to the other side. Also, Java has some awesome byteBuffer.Order() function which lets the bytebuffer know which way to arrange the bytes. The hard part is, how do I feed data back because as far as I can see, converting edian in C# is not as easy as it is in Java. Might as well just write a byte reverser myself.

Friday, April 13, 2012

Socket programming and C++ serialization

I do have to say that I am not that great at C++. I have a job working as a programmer in C# and its usually my weapon of choice. Tackling the same things as I do with C# in C++ is a bit tough. I'm used to a nice debugger which will tell me anything I need to know about the problem. C++ feels like a step back in programmer friendliness but never the less I can appreciate it for what it provides. Speed and control. C++ is something that I really have to get into. The university unfortunately does a pretty horrendous job at teaching C++. I got all the way through data structures and still struggle with syntax especially when it comes to pointers. I understand the concept very well, but I feel like I don't have enough experience when it comes to them.

As for the code for the last assignment, it did take me a while. I do network programming all the time and I understand how to get a buffer of bytes down a socket and deserialized on the other end pretty well. What killed me for this assignments is pointers and its really my responsibility to go back and relearn a lot of that stuff because like I said, C++ is very powerful. I have to say I am really happy we are doing things the more or less manual way. Really shows how "Advanced" concepts are really pretty simple when you break them all the way down to their basic components.

Sockets! Yes, interesting thing came across the other day. Zombie Yoga now has a statistics gathering server which I wrote the other week. It receives data and parses it into XML for easier read by other devices upon request. Now its time to attatch something cool to the server, like say... a phone or a tablet. As easy as this sounds, this is actually quite annoying. One would assume you just use the java tcp socket (Android) and as long as there is nothing blocking communication, you have a green flag to do post TCP handshakes (verify that your actually who you say you are, or else the server will be busy analyzing packets it doesn't know what to do with) and start passing data to it. Well, no go. First of all, C# is little-endian while Java is big-endian. Ok, too lazy to write a function for that, got Guava. Works, all great. I port this code to Android and it refuses to connect. Nothing. Same code, being executed, nothing. Still have to figure out why I'm having communication problems. Hmmm... got an idea of whats wrong. Ill get back to you guys if I come up with something!

Tuesday, April 3, 2012

First Blog Post

Hey guys, first blog post here! This is going to be my game development blog from now on. I had to create one for GAM 490, but I can see me continuing to use this for the distant future.

So, to start this blog off, just wanted to post some source code for you guys. Did a small exercise last night for myself, pushing a file over a socket. The code is very simple, written in linear format without using objects or custom libraries.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;

class Program
{
static void Main(string[] args)
{
Thread listenThread = new Thread(listenCommands);
Thread sendThread = new Thread(send);

listenThread.Start();
sendThread.Start();
}
static void listenCommands()
{
byte[] buffer = new byte[8128];
TcpListener connection = new TcpListener(55);
connection.Start();
TcpClient client = connection.AcceptTcpClient();
//connection.AcceptTcpClient();
string latestMessage = "";
Console.WriteLine("Listening to client connection");

string fileName = "";

while (true)
{
if (client.Connected)
{
//Console.WriteLine("Server accepted connection");
client.Client.Receive(buffer);
latestMessage = ASCIIEncoding.ASCII.GetString(buffer);
latestMessage = latestMessage.Replace('\0', ' ');
latestMessage = latestMessage.Trim();
if (latestMessage == "SENDFILE")
{
Console.WriteLine("SERVER::SENDFILE command recieved... setting up buffer and waiting for file name");
buffer = new byte[8128]; //clear buffer to recieve file name
client.Client.Receive(buffer);//recieve file name
fileName = ASCIIEncoding.ASCII.GetString(buffer);
fileName = fileName.Replace('\0', ' ');
fileName = fileName.Trim();
Console.WriteLine("SERVER::File name recieved... '" + fileName + "'");

buffer = new byte[8128]; //clear the buffer again to recieve the file
client.Client.Receive(buffer); //get the file bytes

FileStream file = File.Create("NEW_" + fileName); //create teh file and obtain the stream
file.Write(buffer, 0, buffer.Length); //write the buffer into the file
Console.WriteLine("SERVER::File was created succesfully. Clearing byte buffer and closing file stream...");
file.Close(); //close the file
fileName = ""; //clear name

}
else
{
Console.WriteLine("SERVER::" + latestMessage);
}
latestMessage = "";
buffer = new byte[8128];
}
}
}
static void send()
{
TcpClient client = new TcpClient("localhost", 55);

if (client.Client.Connected)
{
Console.WriteLine("Client connected...");
}

string message = "";
string fileName = "";


byte[] buffer = new byte[8128];

while (true)
{
message = Console.ReadLine();

Console.WriteLine("CLIENT::input recieved");
if (message.Equals("SENDFILE"))
{
Console.WriteLine("CLIENT::Please provide a file name...");
buffer = ASCIIEncoding.ASCII.GetBytes(message);
client.Client.Send(buffer);//Send "SENDFILE" to server so server knows what to expect
buffer = new byte[8128];
fileName = Console.ReadLine();
buffer = ASCIIEncoding.ASCII.GetBytes(fileName);
client.Client.Send(buffer);
buffer = new byte[8128];//clear the buffer
buffer = File.ReadAllBytes(fileName);
client.Client.Send(buffer);
}
else
{
buffer = ASCIIEncoding.ASCII.GetBytes(message);
client.Client.Send(buffer);
}

}
}
}

I'm pretty excited for GAM 490! I have been messing around with network and socket programming for the last month but really want to see what the code can do when applied to an actual game; having to manage data on all clients. As far as I have gotten to work, the client/servers only manage a byte stream at a time.

LETS DO THIS!