Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
R

Ronny Andersson

@Ronny Andersson
About
Posts
2
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • socket listening error
    R Ronny Andersson

    I wrote a small test prog this morning just for fun. It is a very primitive and small webserver it only sends a greeting back to the webbrowser. Try it out locally on your machine with a webbrowser, then try it out with between two machines. Try changing the port from 80 to something else (I marked the line in red bold in the code) and try calling it again if it doesn't work I agree with you it might be a firewall problem. I've only got one PC at home so I cannot test it between two PCs. God Luck

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;

    namespace TestSocket
    {
    class MiniWebServer
    {
    public static void OnAcceptConnection(IAsyncResult ar)
    {
    Socket serverSock = (Socket)ar.AsyncState;
    Socket clientSocket = serverSock.EndAccept(ar);
    try
    {
    serverSock.BeginAccept(new AsyncCallback(MiniWebServer.OnAcceptConnection),serverSock);
    ClientStateObject cso = new ClientStateObject();
    cso.worksocket = clientSocket;
    clientSocket.BeginReceive(cso.buffer, 0, 256, SocketFlags.None,
    new AsyncCallback(MiniWebServer.OnDataReceived), cso);
    }
    catch(Exception e)
    {
    serverSock.Close();
    Console.WriteLine(e.ToString());
    }
    }

        public static void OnDataReceived(IAsyncResult ar)
        {
            ClientStateObject cso = (ClientStateObject)ar.AsyncState;
            try
            {
                int read = cso.worksocket.EndReceive(ar);
                Console.WriteLine(Encoding.ASCII.GetString(cso.buffer,0,read).ToString());
                if(read == 256)
                {
                    cso.worksocket.BeginReceive(cso.buffer,0,256,SocketFlags.None,
                        new AsyncCallback(MiniWebServer.OnDataReceived), cso);
                }
                else
                {
                    cso.buffer = Encoding.ASCII.GetBytes("Hello from the MiniWebServer");
                    cso.worksocket.BeginSend(cso.buffer, 0, cso.buffer.Length, SocketFlags.None, 
                    new AsyncCallback(OnDataSent), cso);
                }
            }
            catch(Exception e)
            {
                cso.worksocket.Close();
                Console.WriteLine(e.ToString());
            }
       }
    
       public static void OnDataSent(IAsyncResult
    
    C# help security lounge workspace

  • socket listening error
    R Ronny Andersson

    do you call EndAccept in your AcyncCallBack method? Something like this:

    private void MainListenerBeginAccept(IAsyncResult asyncresult)
    {
        Socket s = (Socket)ar.AsyncState;
        Socket s2 = s.EndAccept(ar);
        // use s2 to communicate with client.
    }
    

    Note I haven't tried it out, so it may or may not work. If you like I can have a look at your MainListenerBeginAccept method. Ronny Andersson

    C# help security lounge workspace
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups