Marxley
Marxley

Reputation: 120

Java TCP and UDP echo in one server

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.

Upvotes: 1

Views: 8673

Answers (3)

Mirko Adari
Mirko Adari

Reputation: 5103

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.

Upvotes: 1

Robert S. Barnes
Robert S. Barnes

Reputation: 40578

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

Upvotes: 5

bmargulies
bmargulies

Reputation: 100133

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

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

Upvotes: 5

Related Questions