Only receiving first character of string in UDP response

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahmadasghar
    New Member
    • Mar 2010
    • 1

    Only receiving first character of string in UDP response

    Hello,
    I am a beginner at C# and am using it to develop an application to control a robot remotely. To communicate with the robot I am using UDP. I am able to successfully send instructions to it but when I try to receive a response in the form of a string I only receive the first character out of the whole string. The code I am using to communicate using UDP is as follows:

    Code:
    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.2.102"), 54125);
    Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    string start = "start";
    byte[] data = new byte[1024];
    data = Encoding.ASCII.GetBytes(start);
    server.SendTo(data, data.Length, SocketFlags.None, ipep);
    IPEndPoint client = new IPEndPoint(IPAddress.Parse("192.168.2.102"), 54125);
    EndPoint Remote = (EndPoint)client;
    data = new byte[1024];
    int recv = server.ReceiveFrom(data, ref Remote);
    label.Text = Encoding.ASCII.GetString(data, 0, recv);
    server.Close();
    Last edited by tlhintoq; Mar 16 '10, 05:55 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Did you check the MSDN for UDPclient?


      Code:
      // This constructor arbitrarily assigns the local port number.
      UdpClient udpClient = new UdpClient(11000);
          try{
               udpClient.Connect("www.contoso.com", 11000);
      
               // Sends a message to the host to which you have connected.
               Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
      
               udpClient.Send(sendBytes, sendBytes.Length);
      
               // Sends a message to a different host using optional hostname and port parameters.
               UdpClient udpClientB = new UdpClient();
               udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);
      
               //IPEndPoint object will allow us to read datagrams sent from any source.
               IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
      
               // Blocks until a message returns on this socket from a remote host.
               Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); 
               string returnData = Encoding.ASCII.GetString(receiveBytes);
      
               // Uses the IPEndPoint object to determine which of these two hosts responded.
               Console.WriteLine("This is the message you received " +
                                            returnData.ToString());
               Console.WriteLine("This message was sent from " +
                                           RemoteIpEndPoint.Address.ToString() +
                                           " on their port number " +
                                           RemoteIpEndPoint.Port.ToString());
      
                udpClient.Close();
                udpClientB.Close();
      
                }  
             catch (Exception e ) {

      Comment

      Working...