-
[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.
-
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.
-
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.
-
Re: [2008] winsock and feasibility
-
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.
-
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
-
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
-
Re: [2008] winsock and feasibility
-
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?
-
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
-
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.
-
Re: [2008] winsock and feasibility
Ok I'll see what I can do.
-
Re: [2008] winsock and feasibility
Thanks for your help so far btw
-
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.
-
Re: [2008] winsock and feasibility
Type IP and Type IP endpoint also not defined.
-
Re: [2008] winsock and feasibility
You need to either put this line at the top of your code:
vb Code:
Imports System.Net.Sockets
or use the full name, including namespaces:
vb Code:
Dim listenSocket As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
-
Re: [2008] winsock and feasibility
The same goes for IPEndpoint, altough I believe thats in the System.Net namespace.
-
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...
-
Re: [2008] winsock and feasibility
-
Re: [2008] winsock and feasibility
Well the error you're getting is due to the fact that you've got 2 variables called HostIP:
and
VB.NET Code:
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.
-
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.
-
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:
Dim port As Integer
port = "80"
since port is an integer, you shouldnt assign "80" to it, "80" is a string literal.
-
Re: [2008] winsock and feasibility
hi, still doing backup so cant use vs2k8 but
port = "80.0" ? right?
-
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
-
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.
-
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.
-
Re: [2008] winsock and feasibility
Alright, post your current code :)
-
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
-
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:
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 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 ep As New IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), port)
listenSocket.Bind(ep)
' start listening
listenSocket.Listen(backlog)
'CreateAndListen
End Sub
-
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
-
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.
-
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.
-
Re: [2008] winsock and feasibility
Odd, I have to specify me.visible = 1 onload in the code, why is this?
-
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.
-
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:
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.
-
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
-
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.
-
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")
-
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:wave:
-
Re: [VB2008] .NET sockets