Page 1 of 3 123 LastLast
Results 1 to 40 of 81

Thread: [resolved] resolved by atheist .NET sockets

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Resolved [resolved] resolved by atheist .NET sockets

    hi,

    Just a couple of questions as I have just moved from Vb6 to Visual Studio 2008.

    I'm doing VB.net as I am most familiar with it. I have two questions, firstly, is it feasible to use .NET 3.5. I mean is it necessary if I can acheive all aims with .net 2.0.

    I am just thinking of compatiblity here. How can I change my app to 2.0 without redesigning it.

    I'm really new to this so be simple.

    Also, I want to make a program that runs on port 80 and returns the same page when it is requested for. This would be C:\alert.html. This is to redirect requests. (Winsock) I do not want to open a new connection each time.

    Thank you.
    Last edited by samtheman; Apr 10th, 2008 at 05:47 PM. Reason: Title and coding change

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    Quote Originally Posted by samtheman
    hi,

    Just a couple of questions as I have just moved from Vb6 to Visual Studio 2008.

    I'm doing VB.net as I am most familiar with it. I have two questions, firstly, is it feasible to use .NET 3.5. I mean is it necessary if I can acheive all aims with .net 2.0.

    I am just thinking of compatiblity here. How can I change my app to 2.0 without redesigning it.
    If you're not using anything .NET 3.5-specific, then changing your application to 2.0 would be fairly simple.
    Quote Originally Posted by samtheman
    I'm really new to this so be simple.

    Also, I want to make a program that runs on port 80 and returns the same page when it is requested for. This would be C:\alert.html. This is to redirect requests. (Winsock) I do not want to open a new connection each time.

    Thank you.
    Whats the actual question? In .NET, you should use the System.Net.Sockets.TcpListener class to listen on a specific port.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Sorry,

    No idea how to do that. Do I add a reference or what? I'm sorry this is a completely different environment for me.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Anyone, any ideas?

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    You shouldnt need to add a reference, because it should be added by default. Simply declare and use the TcpListener like any other class.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [2008] winsock and feasibility

    What atheist is trying to say is forget Winsock it should not be used in .Net. Instead .Net has a set Sockets class' you should use theres a little learning curve to get your head around it but gives you more power and flexibility than Winsock.

    Have a search for .Net Sockets.

    Pino

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Hi,

    Thanks, I can see about sending byte streams but I really cannot find anything to what I want to do.

    Basically I want to run a WebServer on 127.0.0.0 (localhost) on port 80, and whenever it is accessed, return the same page, C:\alert.html.

    So, I'm not sure how to do this but:

    Code:
    Public Sub Listen ( _
        backlog As Integer
     backlog= "80"
    )
    
    Dim instance As Socket
    Dim backlog As Integer
    
    instance.Listen(backlog)
    Not sure about this, from http://msdn2.microsoft.com/en-us/library/system.net.sockets.socket.listen.aspx

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    any ideas?

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    On the page you linked too, there's an example on listening to a specific local port. Have you tried it?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    I'm just not getting this, how do I respond with a web page though?

    Code:
      ' create the socket
       Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
       ' bind the listening socket to the port
       Dim hostIP As IPAddress = Dns.Resolve(IPAddress.Any.ToString()).AddressList(0)
       Dim ep As New IPEndPoint(hostIP, port)
       listenSocket.Bind(ep)
    
       ' start listening
       listenSocket.Listen(backlog)
    End Sub 'CreateAndListen
    Where do I enter port number (80)? How do I respond with a page if it occurs/is opened.

    Thanks

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    Well, after calling the Listen method, the socket will listen for incoming connection attempts.
    Now though, you have to call its Accept (Or AcceptAsync) method, its a blocking method that will halt the execution of any code until you receive a connection attempt, in which case it returns a socket for the newly created connection.
    Since this is a blocking method, you'd do best to call it on a worker thread, OR use the AcceptAsync method.

    Once you've got the socket for the newly created connection you use its receive method to receive the HTTP request. Then you use the Send method to send a HTTP response. Give it a try, I'll help you if you get stuck.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Ok I'll see what I can do.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Thanks for your help so far btw

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) type socket is not defined, the example comes up with errors.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Type IP and Type IP endpoint also not defined.

  16. #16
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    You need to either put this line at the top of your code:
    vb Code:
    1. Imports System.Net.Sockets
    or use the full name, including namespaces:
    vb Code:
    1. Dim listenSocket As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  17. #17
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    The same goes for IPEndpoint, altough I believe thats in the System.Net namespace.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Localhost variable hostip already declared in current block

    Imports System.Net.Sockets
    Imports System.Net

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim instance As Socket
    Dim backlog As Integer

    Dim hostip As String
    Dim port As Integer




    port = "80"

    ' create the socket
    Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    ' bind the listening socket to the port
    Dim hostIP As IPAddress = Dns.Resolve(IPAddress.Any.ToString()).AddressList(0)
    Dim ep As New IPEndPoint(hostIP, port)
    listenSocket.Bind(ep)

    ' start listening
    listenSocket.Listen(backlog)
    'CreateAndListen
    End Sub
    End Class

    not sure now...

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Ideas Atheist?

    thanks

  20. #20
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    Well the error you're getting is due to the fact that you've got 2 variables called HostIP:
    VB.NET Code:
    1. Dim hostip As String
    and
    VB.NET Code:
    1. Dim hostIP As IPAddress = Dns.Resolve(IPAddress.Any.ToString()).AddressList(0)
    Its an error with a very simple solution, simply rename one of these variables to something unique.

    You also need to go to the options (Tools menu -> Options), there expand the node "Projects and solutions" and click "VB Defaults". Set Option Strict ON.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Why option strict on, I will get back to you with code tommorow because I am doing backup of server now.

    Thanks.

  22. #22
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    Quote Originally Posted by samtheman
    Why option strict on, I will get back to you with code tommorow because I am doing backup of server now.

    Thanks.
    You should always write your code with option strict on. It teaches you to write your code properly, and greatly decreases the chance for surprising errors in the future.
    For example, this is very wrong, and would be caught by Option Strict:
    VB.NET Code:
    1. Dim port As Integer
    2. port = "80"
    since port is an integer, you shouldnt assign "80" to it, "80" is a string literal.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    hi, still doing backup so cant use vs2k8 but

    port = "80.0" ? right?

  24. #24
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    Quote Originally Posted by samtheman
    hi, still doing backup so cant use vs2k8 but

    port = "80.0" ? right?
    Not quite
    port = 80
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    oh no brackets, sorry in vb6 you always needed a parenthesees. I see there's a few changes in VB.net now.

    I am still backing up (just burning a few BD-r's now) as I have just finished a 300gb backup.

    Will work on program tommorow.

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    k dude i really am confused here as to how i respond to the sockets, my listening doesn't even work right.

  27. #27
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    Alright, post your current code
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    lol thx

    Code:
    Imports System.Net.Sockets
    Imports System.Net
    
    Public Class Form1
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim instance As Socket
    Dim backlog As Integer
    
    Dim hostip As String
    Dim port As Integer
    
    
    
    
    port = "80"
    
    ' create the socket
    Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
    ' bind the listening socket to the port
    Dim hostIP As IPAddress = Dns.Resolve(IPAddress.Any.ToString()).AddressList(0)
    Dim ep As New IPEndPoint(hostIP, port)
    listenSocket.Bind(ep)
    
    ' start listening
    listenSocket.Listen(backlog)
    'CreateAndListen
    
    Socket.Response fso.open "C:\Alert.html", 'byte stream input????
    End Sub
    End Class

  29. #29
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] winsock and feasibility

    I dont see you calling the Accept method anywhere.
    Here, I've cleaned the code up for you a bit. Not added anything.

    VB.NET Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim instance As Socket
    3.         Dim backlog As Integer
    4.  
    5.         Dim port As Integer
    6.         port = 80
    7.  
    8.         ' create the socket
    9.         Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    10.  
    11.         ' bind the listening socket to the port
    12.         Dim ep As New IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), port)
    13.         listenSocket.Bind(ep)
    14.  
    15.         ' start listening
    16.         listenSocket.Listen(backlog)
    17.         'CreateAndListen
    18.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

    Also instance variable is not used warning. Obviously I am running IIS on my Server Box which I am developing on, and so I changed port to 79 to make sure conflicts are not in effect. Still receive A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    There is no definition of backlog just stating that it is a variable. Basically, I want to run a small web server returning the same page on Port 80. This will only ever require one connection, however, the page may be open at multiple times. This is for Hosts redirect.

  32. #32

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Also for some reason, (Fixed error now) I cannot see the Form Window when I run the app, even tried making a new form. Yes, I will want it invisible later, but I was testing the ListenSocket.Accept() with a Msgbox.

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Odd, I have to specify me.visible = 1 onload in the code, why is this?

  34. #34

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [2008] winsock and feasibility

    Wow, sorry for so many posts, but the form appears to be disabled, as I hover over it I get an hourglass and the CPU is 100%. Looks like a wrong turn somewhere. Here's the code I got:

    (Wow now it's resumed), though it is a bit freezy

    Code:
    
    Imports System.Net.Sockets
    Imports System.Net
    
    Public Class webFilter
    
        Private Sub webFilter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Visible = 1
            Dim instance As Socket
            Dim backlog As Integer
            Dim port As Integer
            port = 79
            ' create the socket
            Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            ' bind the listening socket to the port
            Dim ep As New IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), port)
            listenSocket.Bind(ep)
            ' start listening
            listenSocket.Listen(backlog)
            'CreateAndListen
            listenSocket.Accept()
            MsgBox("Hi testing example")
            'Try this?
            '#####listenSocket.BeginSend()
            'Still use FileSystemObject, open file
            '#####listenSocket.BeginSendFile()
        End Sub
    
    End Class
    Thanks, I am getting there.

  35. #35
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [VB2008] .NET sockets

    The reason why the form freezes upon executing the Accept method is because it is a blocking method. All code will halt there until a connection has been made and returned, or if there's some time out. When the form is frozen, try navigating here in your browser:
    localhost:79
    The form will probably "unfreeze", because the accept method returns. However you need to handle what it returns;
    vb Code:
    1. instance = listenSocket.Accept()
    Then you need to send whatever data you wish to send through the instance socket. Note however that you'll have to send a HTTP response back, and not just the actual file.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  36. #36

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [VB2008] .NET sockets

    so i scrap the listenSocket.Accept() on its own and declare it with instance = listenSocket.Accept()

    Hmm that doesn't make much sense, we have defined instance as the accept function but we never call it.

    You were right about the 79 ( I was doing that last night to unfreeze the form)

    If Instance = 1


    Just pulled this off the web, what is Console.Writeline (does it write to label or straight to form)

    Code:
    Imports System
    Imports System.IO
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    Imports Microsoft.VisualBasic
    
    
    Class MyTcpListener
    
        Public Shared Sub Main()
    
        Dim server As TcpListener
        server=nothing
            Try
                ' Set the TcpListener on port 13000.
             Dim port As Int32 = 13000
             Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
    
             server = New TcpListener(localAddr, port)
    
             ' Start listening for client requests.
             server.Start()
    
             ' Buffer for reading data
                Dim bytes(1024) As Byte
                Dim data As String = Nothing
    
             ' Enter the listening loop.
             While True
                Console.Write("Waiting for a connection... ")
    
                ' Perform a blocking call to accept requests.
                ' You could also user server.AcceptSocket() here.
                Dim client As TcpClient = server.AcceptTcpClient()
                Console.WriteLine("Connected!")
    
                data = Nothing
    
                ' Get a stream object for reading and writing
                Dim stream As NetworkStream = client.GetStream()
    
                Dim i As Int32
    
                ' Loop to receive all the data sent by the client.
                i = stream.Read(bytes, 0, bytes.Length)
                While (i <> 0) 
                   ' Translate data bytes to a ASCII string.
                   data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                        Console.WriteLine("Received: {0}", data)
    
                   ' Process the data sent by the client.
                   data = data.ToUpper()
                        Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
    
                   ' Send back a response.
                   stream.Write(msg, 0, msg.Length)
                        Console.WriteLine("Sent: {0}", data)
    
                   i = stream.Read(bytes, 0, bytes.Length)
    
                End While
    
                ' Shutdown and end connection
                client.Close()
             End While
          Catch e As SocketException
             Console.WriteLine("SocketException: {0}", e)
          Finally
             server.Stop()
          End Try
    
          Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
          Console.Read()
       End Sub 'Main
    
    End Class 'MyTcpListener
    your one is different, using .net socket instead of tcplistener

  37. #37

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [VB2008] .NET sockets

    Sorry noticed that it is irrellevant (a client receive), console.write is for direct write to form?

    listenSocket.Send() but how do I sent the html back, I am thinking of opening the file, (Still use FSO?) and sending it through a bytestream.

  38. #38

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [VB2008] .NET sockets

    Code:
    Private Sub webFilter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Visible = 1
            Dim instance As Socket
            Dim backlog As Integer
            Dim port As Integer
            port = 79
            ' create the socket
            Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            ' bind the listening socket to the port
            Dim ep As New IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), port)
            listenSocket.Bind(ep)
            ' start listening
            listenSocket.Listen(backlog)
            'CreateAndListen
            instance = listenSocket.Accept
            instance.Accept()
            MsgBox("Hi")

  39. #39
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199

    Re: [VB2008] .NET sockets

    I suggest that if you are interested in socket programming, that you read these two articles from start to finish... then look through the code (it's c#, but easy enough to convert if u need it in VB:P).

    They explain everything u'll need to know to get up and running.. and the code works really well.

    Part 1:
    http://www.codeguru.com/Csharp/Cshar...cle.php/c7695/

    Part 2:
    http://www.codeguru.com/csharp/cshar...cle.php/c8781/

    nJoy
    If the facts don't fit the theory, change the facts. --Albert Einstein

  40. #40

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: [VB2008] .NET sockets

    il give it a try

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width