0

I'm working on TCP and UDP just to learn the basics of how networks and protocols work in Java, and the task my professor has set me is to have the following:

A TCP Client that connects to a TCP Server, which communicates with a UDP Client that connects to a UDP Server, which gets/alters data from a Database.

I have everything set up and working, but right now the TCP Server calls the UDP Client as an object, and passes its information through a method call, rather than over the internet.

For TCP I'm using server sockets and input stream / buffered stream readers, and for the UDP I'm using datagram packets. So I'm not really sure how I can send from one to the other directly, or if it's even possible.

Thanks in advance for any help/advice/guidance!

Jona

**EDIT:

What I mean by passing through a method call:

//In TCPServer class
UDPClient udpc = new UDPClient();
String result = udpc.GetFromUDPServer(clientCommand);

//In UDPClient class
public class UDPClient{

    public String GetFromUDPServer(String clientCommand){

        try{

            //Create a datagram socket
            DatagramSocket sock = new DatagramSocket();

            byte[] buffer =  clientCommand.getBytes("UTF-8");

            InetAddress ipAddress = InetAddress.getByName("localhost");

            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ipAddress, 447);

...

Jona
  • 1,023
  • 2
  • 15
  • 39
  • This is pretty unclear. In particular, I have no idea what "passes its information through a method call rather than over the internet" means. – Oliver Charlesworth Oct 25 '14 at 18:20
  • Hope that code clarifies what I meant! – Jona Oct 25 '14 at 18:27
  • I think you may want a clarification from your professor. If you quoted the task directly, it used "communicate" rather than "connect" for this particular link, so it could be that invoking the client in a method is acceptable. You need to ask him/her. – RealSkeptic Oct 25 '14 at 18:32
  • You're right, and I will get clarification on Monday - but *is* it possible to communicate between TCP and UDP in the way I have it set up? – Jona Oct 25 '14 at 18:36
  • Yes. A TCP client connects to the TCP server and sends it a command, then the TCP server uses its own UDP client to send a datagram to the UDP server, reads the response, and sends the information back to the TCP client. That is how you have to do it. – Remy Lebeau Oct 25 '14 at 23:15

0 Answers0