I'm writing a client-server application in Java in a way that a client and a server could interact with each through a UDP socket, ping-pong through a TCP socket, and users (clients) could chat with each other through TCP sockets.
I have two separate TCP and UDP threads. I have the TCP and UDP functionality for a server fused together (simply starting the TCP and UDP threads). But how do I do this for a client?
I've gone through the following pages here:
1) This guy has TCP and UDP segregated for two separate clients: Java TCP and UDP echo in one server
2) The thread Can two applications listen to the same port? suggests that I can't use both TCP and UDP for the same port number at the same time for the same client. Which brings us to ...
3) ... this page: Listening for TCP and UDP requests on the same port. However, it does not suggest anything on how to implement the client. Plus, he has a thread, not a client (weird) receiving packets from and sending them to the server.
I've been cruising the Web (Google in particular) and couldn't come up with anything. I believe that very few, if any, have addressed this issue.
So, again, my question is: how do I fuse TCP and UDP functionality in the client? I want the server to be able to connect with the client through TCP and UDP sockets, and use TCP between clients. I don't know what to do next.
Any help is appreciated. Thanks in advance.
===========================================
Here's my client code in its current form:
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
MulticastSocket udpSocket = new MulticastSocket(4446);
InetAddress address = InetAddress.getByName("230.0.0.1");
udpSocket.joinGroup(address);
DatagramPacket packet;
// UDP: get a few quotes
for (int i = 0; i < 5; i++) {
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
udpSocket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Quote of the Moment: " + received);
}
udpSocket.leaveGroup(address);
udpSocket.close();
// TCP
try (
Socket tcpSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(tcpSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(tcpSocket.getInputStream()));
) {
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
// AS LONG AS server and client are interacting:
while ((fromServer = in.readLine()) != null) {
System.out.println(fromServer);
if (fromServer.equals("Bye."))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
//System.out.println(fromUser);
out.println(fromUser);
}
}
} catch (UnknownHostException e) {
System.err.println("Unknown host: " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Unable to find I/O for connection to " +
hostName);
System.exit(1);
}
In this case the client receives the datagrams properly in UDP connection, then switches to the TCP functionality as expected. Two problems, though:
1) The server cannot get out of the listening loop once the proper input is typed in (I hardcoded in an "exit" command).
2) The server and the client will maintain a UDP connection only once before switching to TCP.