Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4554790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:08:04+00:00 2026-05-21T17:08:04+00:00

I am trying to hit a webserver using a simple Socket rather than using

  • 0

I am trying to hit a webserver using a simple Socket rather than using the wrappers made available. For whatever reason, I can’t get it to receive the response. What is wrong with the code below:

class Program
{
    static void Main(string[] args)
    {
        SocketConnection connection;
        if (SocketConnection.TryCreate("http://www.bing.com/", out connection))
        {
            connection.Execute().WaitOne();
        }
    }
}

public class StateObject
{
    public Socket WorkingSocket
    {
        get;
        set;
    }

    public const int BufferSize = 256;
    public byte[] buffer = new byte[BufferSize];
    public StringBuilder sb = new StringBuilder();
}

public class SocketConnection
{
    private Uri uri;
    private IPAddress address;

    private ManualResetEvent waithandle = new ManualResetEvent(false);
    private const int bytesInKilobyte = 1024;
    private byte[] recieveBuffer = new byte[bytesInKilobyte];
    private string recievedText = String.Empty;

    public string Result
    {
        get;
        private set;
    }

    private SocketConnection() { }

    public static bool TryCreate(string url, out SocketConnection connection)
    {
        Uri uri = null;

        bool result = Uri.TryCreate(url, UriKind.Absolute, out uri);
        if (result)
        {
            connection = new SocketConnection { uri = uri };
        }
        else
        {
            connection = null;
        }

        return result;
    }

    public WaitHandle Execute()
    {
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IPv4);
        socket.DontFragment = true;
        socket.SendBufferSize = 0;
        IPEndPoint endpoint = new IPEndPoint(Dns.Resolve(this.uri.Host).AddressList[0], this.uri.Port);
        socket.BeginConnect(endpoint, this.OnEndConnect, new StateObject { WorkingSocket = socket });
        return this.waithandle;
    }

    private void OnEndConnect(IAsyncResult result)
    {
        StateObject state = (StateObject)result.AsyncState;
        state.WorkingSocket.EndConnect(result);

        StringBuilder sb = new StringBuilder();
        sb.Append("GET ");
        sb.Append(this.uri.PathAndQuery);
        sb.Append(" HTTP/1.1\r\n\r\n");

        byte[] dataToSend = Encoding.ASCII.GetBytes(sb.ToString());
        ArraySegment<byte> segment = new ArraySegment<byte>(dataToSend);
        state.WorkingSocket.BeginSend(new[] { segment }, SocketFlags.None, this.OnEndSend, state);
    }

    private void OnEndSend(IAsyncResult result)
    {
        StateObject state = (StateObject)result.AsyncState;
        int count = state.WorkingSocket.EndSend(result);

        state.WorkingSocket.BeginReceive(recieveBuffer, 0, recieveBuffer.Length, SocketFlags.None, new AsyncCallback(this.OnEndReceive), state);
    }

    private void OnEndReceive(IAsyncResult result)
    {
        StateObject state = (StateObject)result.AsyncState;
        SocketError error;

        int count = state.WorkingSocket.EndReceive(result, out error);
        recievedText += Encoding.UTF8.GetString(this.recieveBuffer, 0, count);
        if (count == recieveBuffer.Length)
        {
            state.WorkingSocket.BeginReceive(recieveBuffer, 0, recieveBuffer.Length, SocketFlags.None, out error, this.OnEndReceive, state);
        }
        else
        {
            this.Result = recievedText;
            this.waithandle.Set();
            // done with request
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:08:04+00:00Added an answer on May 21, 2026 at 5:08 pm

    The issue was with the socket I was creating. It needed to be a streamed tcp connection rather than a raw ipv4.

    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.