CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2011
    Posts
    2

    UDP/socket Receive issues

    trying to setup a connection with a UDP client.
    i can connected to it and send to it. Receiving is an issue.
    the receive seems to get data as the buffer size is right.
    i know this because i check with a packet sniffer but yet, when i convert the buffer it returns nothing. i am sure it has something to do in this area "returnData = Encoding.Default.GetString(recBytes, 0, recBytes.Length);"

    udpConn = new UdpClient(HostIP, hostPort);
    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any,0);

    string sendstr = "hello";
    Byte[] sendBytes = Encoding.Default.GetBytes(sendstr);
    udpConn.Send(sendBytes, sendBytes.Length);

    string returnData = "";
    Byte[] recBytes = null;

    recBytes = udpConn.Receive(ref RemoteIpEndPoint);


    returnData += Encoding.Default.GetString(recBytes, 0, recBytes.Length);

    txtconsole.AppendText(Environment.NewLine + "returnData " + returnData.Length);

    returnData = Encoding.Default.GetString(recBytes, 0, recBytes.Length);

    txtconsole.AppendText(Environment.NewLine + "This is the message you received " +
    returnData.ToString());

    txtconsole.AppendText(Environment.NewLine + "This message was sent from " +
    RemoteIpEndPoint.Address.ToString() +
    " on their port number " +
    RemoteIpEndPoint.Port.ToString());

    txtconsole.AppendText(Environment.NewLine + "receiveBytes.Length " +
    recBytes.Length.ToString() +
    " returnData.Length " +
    returnData.Length.ToString());

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: UDP/socket Receive issues

    So what is being sent and what are you seeing show up in the console window?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2011
    Posts
    2

    Re: UDP/socket Receive issues

    console window show nothing. if i break point this line i can see the data in both the "returnData" and recBytes. the raw data has extra \x0 in string.

    returnData = Encoding.Default.GetString(recBytes, 0, recBytes.Length);

    when i figured that data was there i added a replace ()
    returnData = Encoding.Default.GetString(recBytes, 0, recBytes.Length).Replace("\x0",";");

    now the string passes to he console window. now it turns into a differnt question

    what would be the easyest way to remove all the extra "\x0" bits from the byte before i convert to a string. or is the way i am going it ok?

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

Featured