Click here to Skip to main content
15,886,872 members
Home / Discussions / C#
   

C#

 
GeneralRe: Browse For Folder Pin
Abdul Rahman Hamidy8-Nov-08 21:49
Abdul Rahman Hamidy8-Nov-08 21:49 
AnswerRe: Browse For Folder Pin
Giorgi Dalakishvili5-Nov-08 0:46
mentorGiorgi Dalakishvili5-Nov-08 0:46 
AnswerRe: Browse For Folder Pin
J4amieC5-Nov-08 0:48
J4amieC5-Nov-08 0:48 
QuestionComparer [modified] Pin
ezazazel5-Nov-08 0:04
ezazazel5-Nov-08 0:04 
AnswerRe: Comparer Pin
Guffa5-Nov-08 2:51
Guffa5-Nov-08 2:51 
QuestionReceive replies to a UDP broadcast Pin
Olivier Sannier4-Nov-08 23:39
Olivier Sannier4-Nov-08 23:39 
AnswerRe: Receive replies to a UDP broadcast Pin
Mark Salsbery5-Nov-08 5:22
Mark Salsbery5-Nov-08 5:22 
GeneralRe: Receive replies to a UDP broadcast Pin
Olivier Sannier5-Nov-08 22:28
Olivier Sannier5-Nov-08 22:28 
Actually, no, this is not possible as one cannot listen on the same port as the sender socket.
I fiddle around a bit more and eventually found a solution: Using Send was the culprit, once I used SendTo, it worked with Receive. However I had to set the options on the socket to avoid an exception being triggered when calling SendTo. So in the end, the code looks like this:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
    // The data that needs to be sent
    byte[] data = new byte[122];

    // Fill the data here

    // Send the data
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
    socket.SendTo(data, new IPEndPoint(IPAddress.Broadcast, port));

    // Wait for responses
    for (int i = 0; i < 100; i++)
    {
        int availableBytes = socket.Available;
        if (availableBytes > 0)
        {
            byte[] buffer = new byte[availableBytes];
            socket.Receive(buffer, 0, availableBytes, SocketFlags.None);

            // Interpret the buffer
            
            // Finally, return the response
            return ASCIIEncoding.ASCII.GetString(buffer);
        }

        System.Threading.Thread.Sleep(100);
    }
    return "Nothing found";
}
finally
{
    socket.Close();
}


With this, I get the answers to the broadcast request I sent.

Cheers
Olivier
QuestionDrag and Drop TreeView Pin
Paul Unsworth4-Nov-08 23:36
Paul Unsworth4-Nov-08 23:36 
AnswerRe: Drag and Drop TreeView Pin
cyber-drugs5-Nov-08 0:22
cyber-drugs5-Nov-08 0:22 
GeneralRe: Drag and Drop TreeView Pin
Paul Unsworth5-Nov-08 0:53
Paul Unsworth5-Nov-08 0:53 
GeneralRe: Drag and Drop TreeView Pin
cyber-drugs5-Nov-08 1:04
cyber-drugs5-Nov-08 1:04 
GeneralRe: Drag and Drop TreeView [modified] Pin
DaveyM695-Nov-08 1:27
professionalDaveyM695-Nov-08 1:27 
AnswerRe: Drag and Drop TreeView Pin
Paul Unsworth5-Nov-08 1:53
Paul Unsworth5-Nov-08 1:53 
QuestionHow to comunicate between two different appliaction/forms Pin
WinSolution4-Nov-08 22:47
WinSolution4-Nov-08 22:47 
AnswerRe: How to comunicate between two different appliaction/forms Pin
Giorgi Dalakishvili4-Nov-08 22:53
mentorGiorgi Dalakishvili4-Nov-08 22:53 
QuestionRe: How to comunicate between two different appliaction/forms Pin
WinSolution5-Nov-08 18:33
WinSolution5-Nov-08 18:33 
AnswerRe: How to comunicate between two different appliaction/forms Pin
Giorgi Dalakishvili5-Nov-08 19:25
mentorGiorgi Dalakishvili5-Nov-08 19:25 
AnswerRe: How to comunicate between two different appliaction/forms Pin
Giorgi Dalakishvili7-Nov-08 0:46
mentorGiorgi Dalakishvili7-Nov-08 0:46 
GeneralRe: How to comunicate between two different appliaction/forms Pin
WinSolution7-Nov-08 19:36
WinSolution7-Nov-08 19:36 
QuestionHow do I crop / convert an Image that has Indexed Pixel Format Pin
saberbladez4-Nov-08 22:26
saberbladez4-Nov-08 22:26 
GeneralRe: How do I crop / convert an Image that has Indexed Pixel Format Pin
cyber-drugs5-Nov-08 0:26
cyber-drugs5-Nov-08 0:26 
GeneralRe: How do I crop / convert an Image that has Indexed Pixel Format Pin
saberbladez5-Nov-08 14:32
saberbladez5-Nov-08 14:32 
QuestionTrimEnd? Pin
dec824-Nov-08 22:21
dec824-Nov-08 22:21 
AnswerRe: TrimEnd? Pin
Ashfield4-Nov-08 22:25
Ashfield4-Nov-08 22:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.