Click here to Skip to main content
13,506,186 members
Rate this:
Please Sign up or sign in to 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.

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

c.Connect("", 80)
nw = c.GetStream()

Dim packet As String = "GET HTTP/1.1" & vbCrLf & _
"Host:" & 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
        Exit While
    End If
    Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, i))
End While
Console.WriteLine("Finished Receiving....")

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.

Posted 28-Dec-12 2:47am
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.

<%@ 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)

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

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

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))


    Dim packet As String = "Hello Server"

    Console.WriteLine("Sending packet containing: ")

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

    Console.WriteLine("Packet sent")

End Sub

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

    End While

End Sub '

Now, after executing the 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 code and behold, it sends the message to the 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
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 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.

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

@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
Dave Kreskowiak 28-Dec-12 10:43am
Don't add a solution to ask a question.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
Sergey Alexandrovich Kryukov 29-Dec-12 20:41pm
Agree. A 5.
Espen Harlinn 30-Dec-12 5:36am
Thank you, Sergey :-D

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100