Handling both UDP and TCP input. Multithreading?

So, after over a month of work on the networking for my game, I am nearly done (2k+ lines of code later :)) with networking for a while! It now comes down to one thing: how to manage both TCP and UDP input to the client/server at the same time. I can send and receive UDP and TCP packets just fine, but the process of reading TCP and UDP is where I run into an issue. Both methods for receiving packets are blocking, meaning it stops the entire thread until it received a packet. I already have three threads for each player connection (main connection thread, packet-input thread, packet-output thread). If I try to have both packets being read for in the one packet-input thread, whichever type is read for first blocks the thread from reading for the next type of packet (UDP first means TCP won’t be received until UDP packet is read first, and vice versa). The simplest solution would be to split the packet-input thread into a TCP-input and UDP-input thread, but I’d want to avoid this if possible. I am sure there are other ways out there, I just don’t know them. How should I approach this?

Here’s my current (non-functioning properly) reading code. This code is just for testing purposes, not the complete code :stuck_out_tongue:


//Elsewhere...
DataInputStream input;

try
{
	byte[] udpData = new byte[26];
	byte[] tcpData = new byte[26];

	DatagramPacket udpPacket = new DatagramPacket(udpData, udpData.length);
				
	base.getDatagramSocket().receive(udpPacket);

	//TCP Packet can't be received unless a  UDP Packet is received. After first UDP-
	//-packet is received, a TCP must be received before receiving another UDP, and so on.
	tcpData = new byte[input.available()];
	input.read(tcpData);

         Packet udpPacket = Packet.extractPacketFromData(udpData);
         Packet tcpPacket = Packet.extractPacketFromData(udpData);
}