Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, i want to download the webpage of a website using UDP protocol. I only know how to do this using TCP protocol using TCPClient. But this time around, i need to do the same task using UDP protocol and i am clueless. Pls any help will be appreciated.

Here is the sample code of what i am trying to implement. But this example uses TCP protocol. But i need to do the same thing using UDP protocol.

VB
Dim c As New TcpClient
Dim i As Integer
Dim nw As NetworkStream

c.Connect("www.google.com", 80)
nw = c.GetStream()

Dim packet As String = "GET http://www.google.com/ HTTP/1.1" & vbCrLf & _
"Host: www.google.com" & vbCrLf & _
"Connection: Close" & vbCrLf & vbCrLf

Dim toSend As Byte() = Encoding.ASCII.GetBytes(packet)
nw.Write(toSend, 0, toSend.Length)

'reading response
Dim buffer(1024) As Byte
Dim received As String = ""
While True
    i = nw.Read(buffer, 0, buffer.Length)
    If i = 0 Then
        c.Close()
        Exit While
    End If
    Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, i))
End While
Console.WriteLine("Finished Receiving....")
Console.ReadLine()


If you can help me re-write the sample code to use UDP protocol instead of the TCP, it will be greatly appreciated. And if also i need to use raw sockets and set the protocol to UDP. Pls any help will be greatly appreciated.

Thanks
Posted

HTTP is layered on top of the TCP protocol - so you will not be able to use UDP unless you also write a server.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Dec-12 20:41pm    
Agree. A 5.
—SA
Espen Harlinn 30-Dec-12 5:36am    
Thank you, Sergey :-D
@Espen Harlinn thank you for your reply. I really appreciate it. So, what you are saying in essence is that i also need to write a server that will "receive" the HTTP header request sent from the client and later process it. If i got it right, what language can i use to write the server application? can i use PHP or ASP.NET
 
Share this answer
 
Comments
Dave Kreskowiak 28-Dec-12 10:43am    
Don't add a solution to ask a question.
If you're talking about a web site that you do NOT control, you cannot use UDP as HTTP is built on top of TCP, not UDP.

If you're controlling the web server and server-side code that the client is supposed to be getting web pages from, then you can write the web server using any language you want. Though, I hardly see the point of writing a HTTP server on top of UDP since any mistake in transmitting the HTML will render the page useless.
 
Share this answer
 
Comments
Joeadeoye 29-Dec-12 9:42am    
thank you very much for your reply. When i was coding, i figured out that "any data sent via UDP can only be retrieved using UDP". I discovered this by creating a TCPListener to listen to port 9201 on my local machine and i wrote another program using UdpClient to connect to 127.0.0.1 port 9201 and i sent some data. After sending this data, the TCP listener i created just keep listening, i.e. it does not accept any incoming connection despite the fact that i am connecting to the port its listening to using UdpClient.

So, when i discovered this, i created another UdpClient in another project and bind the port 9201 and read the input and i got the message and everything works fine. So, after performing all this exercise, i concluded that before i can send data to a server using UDP, i must write a script that uses UdpClient and bind the port 9201 and read response and this script will be placed on the web server.

Now, i am pretty sure that ASP.NET can be used to write the server script but i am not too good at ASP.NET but i am good at PHP, so i dont know if PHP can be used to write the server script using UDP. If i can use PHP, pls help me with sample code and if it is also ASP.NET, help me with sample code.

Thanks
Dave Kreskowiak 29-Dec-12 10:04am    
You've got a lot of learning to do...

ASP.NET cannot be used to do this as it's used to write web SITES, not servers. ASP.NET does everything over HTTP, which only runs on top of TCP.

PHP can be used, so long as you've got a PHP runtime on the server. You're on your own there. I never use PHP for anything.

You also have to learn TCP/IP networking and the differences between transport and application protocols.
thank you @dave i know i have a lot to learn and i'm looking for who to teach me. I installed Microsoft Visual Web Developer IDE on my machine which can be used to write ASP.NET website. I wrote this code yesterday inside the IDE in the Deafult.aspx page.

VB
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Net.Sockets" %>
<%@ Import Namespace="System.Net" %>
<script  runat="server" >
    Protected Sub pgLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Dim recClient As UdpClient
        Dim ipEndPt As New IPEndPoint(IPAddress.Any, 0)
        recClient = New UdpClient(9201)
       
        Dim receivedByte As Byte() = recClient.Receive(ipEndPt)
        Dim strReturned As String = Encoding.ASCII.GetString(receivedByte)
        'Response.Write(strReturned)

        'Dim port As Integer = 9201
        'Dim udpClient As New UdpClient
        recClient.Connect("127.0.0.1", 9200)
        
        Dim boSend As Byte() = Encoding.ASCII.GetBytes(strReturned)
        recClient.Send(boSend, boSend.Length)
        Response.Write("Message sent!")
        recClient.Close()
        
    End Sub

i also wrote another program using VB.NET IDE. Here is the code:

VB
Shared client As New UdpClient
Shared receivePoint As IPEndPoint


Public Sub Main()
    receivePoint = New IPEndPoint(New IPAddress(0), 0)

    client = New UdpClient(9200)

    Dim thread As Thread = New Thread(New ThreadStart(AddressOf WaitForPackets))

    thread.Start()


    Dim packet As String = "Hello Server"

    Console.WriteLine("Sending packet containing: ")

    Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(packet)
    client.Connect("127.0.0.1", 9201)
    client.Send(data, data.Length)

    Console.WriteLine("Packet sent")
    'WaitForPackets()

End Sub

Public Sub WaitForPackets()
    While True
        Console.WriteLine("receiving.........")
        Dim data As Byte() = client.Receive(receivePoint)
        Console.WriteLine("Packet received:" & _
           vbCrLf & "Length: " & data.Length & vbCrLf & _
           System.Text.Encoding.ASCII.GetString(data))

    End While

End Sub '


Now, after executing the asp.net code, it opens in my browser and started loading..(infinite loading) and i concluded in my mind that "it is waiting for client to connect". While the page is loading in my browser, i run the second vb.net code and behold, it sends the message to the asp.net code and gets it back as response.

Now, the reason why i concluded in my earlier post that ASP.NET can work is because of the little exercise i performed which i just explained above. Now that i know that ASP.NET cannot be used, is there any other language that can be used to build udp servers aside from PHP?

To cut my long story short, what i am trying to implement is a client (local) that will send message to a udp server (remote) via UDP protocol.

Any help will be appreciated.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900