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!

1 comment: