[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

UDP connection

I have an application that needs to recieve UDP Calls.  On the visual control in delphi I set the remote and local host properties to "LocalHost" and the local port to 44966 and the remote port to 44967.

Then using a sample application from http://www.torry.net/vcl/internet/sockets/tcp_udp.zip I try to send some text to my application with no success. I simply get no response on either of the applications trying to communicate

Any help?
0
Willemvanzyl
Asked:
Willemvanzyl
  • 3
  • 2
2 Solutions
 
Geert GruwezOracle dbaCommented:
you need 2 different computers as the port is blocked when reading or writing
it's a 1 way communication port

this is the sending of the UDP message

var
  Recipient,SendText: string;

Recipient := 'otherpc@44967';
SendText := 'Message';

Udp := TIdUDPClient.Create(Nil);
try
  with Udp do
  begin
    Host := Copy(Recipient, 0, Pos('@', Recipient) -1);
    Port := StrToInt(Copy(Recipient, Length(Host) + 2, Length(Recipient)));
    Send(SendText);
  end;
finally
  FreeAndNil(Udp);
end;

this is the receving part:
place IdUdpServer on form
UDP_RECEIVER = TIdUdpServer
UDP_RECEIVER.Port := 44967;

procedure TForm1.UDP_RECEIVERRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
  S: TStringStream;
  Msg: String;
begin
  Msg := '';
  if AData.Size > 0 then
  begin
    s := TStringStream.Create('');
    try
      S.CopyFrom(AData, AData.Size);
      Msg := Trim(S.DataString);
    finally
      FreeAndNil(S);
    end;
  end;
  if Msg <> '' then
    ShowMessage(Msg);
end;
0
 
WillemvanzylAuthor Commented:
...some more background

The Sender is a windows Service that can either be on a server or on the local PC itself. The process works as follows:

Service (Sender) intercepts a print job, and with UDP, notifies a Popup app (reciever), the popup app displays a form that the user has to complete, and then sends a UDP message back to the service to either continue the print job or cancel it.

Wil try out your sample code, thanx
0
 
JohnjcesCommented:
I am not certain why you are using two different ports for your UDP transmission.

As Geert points out in his sample, the listening and transmitting ports must be the same port.

If you have a UDP Client Socket and UDP Server Socket, they have to be using the same port. Sounds like you are using different ports for different tasks. Make certain that each task uses its correct UDP port.

I have never had a problem using a Client and Server UDP socket on my local host for local testing. UDP is one way, so as an example use port 45678 from client to server and for two way, an additional client and server socket on port 45679 for the reverse communication.

I hope that made some sense.

John
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
WillemvanzylAuthor Commented:
This is my first UDP development, so me and my co developer are real beginners in regard to UDP. We did start using the same port and then switched to multiple ports. Thanks for the info regarding this. Will implement and revert
0
 
JohnjcesCommented:
Let us know and don't forget to close the Q.

john
0
 
WillemvanzylAuthor Commented:
sorted thanx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now