1

I am trying to build a server which can echo the input from either a TCP or a UDP client.

So far the best I could come up with is this:

import java.net.*;
import java.io.*;

public class EchoServerMultiProtocol {
    public static void main(String[] args) throws IOException {

        String clientSentence;
        String capitalizedSentence;

        while (true) {
            /*******************************************************************
             * * Handle TCP * *
             *******************************************************************/
            ServerSocket TCP_Socket = new ServerSocket(6789);

            Socket connectionSocket = TCP_Socket.accept();

            BufferedReader inFromClient = new BufferedReader(
                    new InputStreamReader(connectionSocket.getInputStream()));

            DataOutputStream outToClient = new DataOutputStream(
                    connectionSocket.getOutputStream());

            clientSentence = inFromClient.readLine();

            capitalizedSentence = clientSentence.toUpperCase() + '\n';

            outToClient.writeBytes(capitalizedSentence);

            /*******************************************************************
             * * Handle UDP * *
             *******************************************************************/
            DatagramSocket UDP_Socket = new DatagramSocket(9876);

            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];

            DatagramPacket receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);

            UDP_Socket.receive(receivePacket);

            clientSentence = new String(receivePacket.getData());

            InetAddress IPAddress = receivePacket.getAddress();

            int port = receivePacket.getPort();

            capitalizedSentence = clientSentence.toUpperCase();

            sendData = capitalizedSentence.getBytes();

            DatagramPacket sendPacket = new DatagramPacket(sendData,
                    sendData.length, IPAddress, port);

            UDP_Socket.send(sendPacket);

        }
    }
}

What happens is that if I send a message from the TCP client the program works as expected but from the UDP client nothing happens. I'm not very well versed in client/server communication so any help would be appreciated.

The client codes are below but I doubt they are the source of the problem.

TCP Client

import java.io.*;
import java.net.*;

class EchoClientTCP {

    public static void main(String argv[]) throws Exception {
        String sentence;
        String modifiedSentence;

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
                System.in));

        Socket clientSocket = new Socket("127.0.0.1", 6789);

        DataOutputStream outToServer = new DataOutputStream(
                clientSocket.getOutputStream());

        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        sentence = inFromUser.readLine();

        outToServer.writeBytes(sentence + '\n');

        modifiedSentence = inFromServer.readLine();

        System.out.println("FROM SERVER: " + modifiedSentence);

        clientSocket.close();

    }
}

UDP Client

import java.io.*;
import java.net.*;

class EchoClientUDP {
    public static void main(String args[]) throws Exception {

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
                System.in));

        DatagramSocket clientSocket = new DatagramSocket();

        InetAddress IPAddress = InetAddress.getByName("127.0.0.1");

        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];

        String sentence = inFromUser.readLine();

        sendData = sentence.getBytes();

        DatagramPacket sendPacket = new DatagramPacket(sendData,
                sendData.length, IPAddress, 9876);

        clientSocket.send(sendPacket);

        DatagramPacket receivePacket = new DatagramPacket(receiveData,
                receiveData.length);

        clientSocket.receive(receivePacket);

        String modifiedSentence = new String(receivePacket.getData());

        System.out.println("FROM SERVER:" + modifiedSentence);

        clientSocket.close();

    }
}

Thank you for your help.

Marxley
  • 120
  • 1
  • 6

3 Answers3

5

Your program blocks waiting for a TCP message, so it never sees any UDP.

You will need, at least, two threads, one for each.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Actually, he'll need one thread to wait for new incoming TCP connections and one thread per connection to wait for input on each established TCP connection. – David Schwartz Jan 21 '13 at 19:00
  • Thank you for your input. The problem is this was a past examination question and it barred me from using threads. Is there a way to check if the incoming message is sent using TCP or UDP? – Marxley Jan 21 '13 at 19:00
  • 1
    If you don't put the relevant information into the question, you won't get a useful answer. – bmargulies Jan 21 '13 at 19:03
  • 1
    There might be an NIO solution for multiplexed waiting. The simple answer to your question about checking is 'no'. – bmargulies Jan 21 '13 at 19:04
  • Thanks that helps me a lot in narrowing my search. – Marxley Jan 21 '13 at 20:05
5

Accept is a blocking call. It never gets to the part of the code with the UDP socket.

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
1

Please just make your life easy and use Netty. In a couple of lines of code you have an efficient thread model, support for most common protocols and a great framework to handle messages.

Mirko Adari
  • 5,083
  • 1
  • 15
  • 23