Skip to content

Instantly share code, notes, and snippets.

@j0z
Created June 7, 2012 19:03
Show Gist options
  • Save j0z/2890856 to your computer and use it in GitHub Desktop.
Save j0z/2890856 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
namespace Alexandria
{
class Client
{
TcpClient tcpClient = new TcpClient(AlexandriaMain.currentServer, 8080);
File file = new File("test.png", "");
public void fileGet(string fileName)
{
NetworkStream ns = tcpClient.GetStream();
StreamWriter sw = new StreamWriter(ns);
sw.Write("GET:" + fileName);
sw.Flush();
byte[] lengthBytes = new byte[16];
int read = ns.Read(lengthBytes, 0, 15);
int length = BitConverter.ToInt32(lengthBytes, 0);
byte[] buf = new byte[length];
ns.Read(buf, 0, buf.Length);
file.Write(buf);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Sockets;
using System.Net;
namespace AlexandriaServer
{
class Server
{
//IPAddress localIP = IPAddress.Broadcast;
//static readonly IPAddress Any;
TcpListener tcpListener = new TcpListener(8080);
//File file;
char[] seperators = { ':' };
string fileLocation = "\\files";
public void runServer()
{
Console.WriteLine("Starting server...");
tcpListener.Start();
Console.WriteLine("Server Started!");
//Byte[] bytes = new Byte[16];
//Byte[] data = new Byte[16];
bool fileComplete = false;
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Client Connected!");
NetworkStream stream = tcpClient.GetStream();
byte[] incomingData = new byte[tcpClient.ReceiveBufferSize];
int bytesRead = stream.Read(incomingData, 0, System.Convert.ToInt32(tcpClient.ReceiveBufferSize));
string command = Encoding.ASCII.GetString(incomingData, 0, bytesRead);
Console.WriteLine(command);
string[] commands = command.Split(seperators);
foreach (string x in commands)
{
Console.WriteLine(x);
}
if (commands[0].Contains("GET"))
{
Console.WriteLine("Getting File! \n" + commands[1]+"test");
//file = new File(commands[1], fileLocation);
FileStream fs = new FileStream(commands[1].Replace("\n", ""), FileMode.Open);
int currentPos = 0;
while (currentPos < fs.Length)
{
byte[] bytes = new byte[16];
int data = fs.Read(bytes, currentPos, 16);
Console.WriteLine(bytes.ToString());
stream.Write(bytes, 0, 16);
currentPos += 16;
if (currentPos >= fs.Length)
fileComplete = true;
}
if (fileComplete)
{
Console.WriteLine("File Complete!");
stream.Close();
}
}
}
}
public void sendFile(string fileName)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment