Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Simple Particle Engine for a shooter game
Snake / Nibbles clone in C and Ncurses
Please improve
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Popular
Parsing of XML data has high CPU usage
Snake / Nibbles clone in C and Ncurses
Please improve
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Active Record getting unique records
Simple Particle Engine for a shooter game
Breadth first cartesian product iterator
php refactoring
first BST
Pastable version of
C# Socket
<pre class='prettyprint' language='cs'>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Net.Sockets; using System.Collections; namespace LPT_5.HTTPSocket { class Socket { private System.Net.Sockets.Socket msSock = null; private String msHost = null; private int miPort = 80; private Hashtable mhHeaders=null; public Socket(String sHost, int iPort) { msHost = sHost; miPort = iPort; //Initialise Standard Headers mhHeaders = new Hashtable(); AddHeader("Connection","Close"); //We wont need to do any pipelining AddHeader("Host", sHost); } public void AddHeader(String sKey, String sValue) { if(!mhHeaders.ContainsKey(sKey)) mhHeaders.Add(sKey,sValue); } private void RemoveHeader(String sKey) { if(mhHeaders.ContainsKey(sKey)) mhHeaders.Remove(sKey); } public String GetURL(String sURL) { msSock = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); msSock.Connect(msHost, miPort); String sReq = "GET " + sURL + " HTTP/1.1"; sReq = TagHeaders(sReq); msSock.Send(System.Text.Encoding.ASCII.GetBytes(sReq)); Hashtable htHeadParams = new Hashtable(); int iResult = ReceiveHeader(ref htHeadParams); String sBody = null; if(htHeadParams.ContainsKey("Content-Length")) { int iLen = Convert.ToInt32((String)htHeadParams["Content-Length"],10); int iOffs = 0; byte[] bBuff = new byte[iLen]; while (iOffs < iLen) { iOffs += msSock.Receive(bBuff,iOffs,iLen-iOffs,SocketFlags.None); } sBody += System.Text.Encoding.ASCII.GetString(bBuff); } msSock.Close(); return sBody; } public byte[] GetRaw(String sURL) { msSock = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); msSock.Connect(msHost, miPort); String sReq = "GET " + sURL + " HTTP/1.1"; sReq = TagHeaders(sReq); msSock.Send(System.Text.Encoding.ASCII.GetBytes(sReq)); Hashtable htHeadParams = new Hashtable(); if(ReceiveHeader(ref htHeadParams) == 200) //If we got the data with no problems { int iLen = Convert.ToInt32((String)htHeadParams["Content-Length"], 10); byte[] bBuff = new byte[iLen]; if (htHeadParams.ContainsKey("Content-Length")) { int iOffs = 0; while (iOffs < iLen) { iOffs += msSock.Receive(bBuff, iOffs, iLen - iOffs, SocketFlags.None); } } msSock.Close(); return bBuff; } return null; } private String TagHeaders(String sReq) { IDictionaryEnumerator deEnumer = mhHeaders.GetEnumerator(); while(deEnumer.MoveNext()) { sReq += "\r\n" + deEnumer.Key + ": " + deEnumer.Value; } return sReq + "\r\n\r\n"; } private int ReceiveHeader(ref Hashtable htHash) { String sHead = null; byte[] buff = new byte[1]; /* We dont know the size of the header, but we do know that header and body are separated by a double newline, so read a byte at a time until we receive double newline. */ do { msSock.Receive(buff, 0, 1, SocketFlags.None); sHead += (char)buff[0]; } while (!sHead.Contains("\r\n\r\n")); int iResult = ProcessHeaders(sHead,ref htHash); return iResult; } private int ProcessHeaders(String sHead, ref Hashtable htHash) { //Split wont work with \r\n, so just regex away the carriage return String sToSplit = Regex.Replace(sHead,"\r",""); String[] sParams = sToSplit.Split('\n'); foreach (String sItem in sParams) { String[] sKeyVal = sItem.Split(':'); if(sKeyVal.Length >= 2) { String sKey = sKeyVal[0].Trim(); String sVal = sKeyVal[1].Trim(); if (htHash.ContainsKey(sKey)) htHash[sKey] += ',' + sVal; //Group multiple headers else htHash.Add(sKey, sVal); } } String[] sResultParams = sParams[0].Split(' '); return Convert.ToInt32(sResultParams[1]); //Return the HTTP Response Code } } }</pre> <a href="http://www.refactormycode.com/codes/813-c-socket" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>