Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sending Screen capture at real time, what to use TCP or UDP?

843790Aug 17 2009 — edited Aug 17 2009
Hello,

I am doing a program where server captures the screen and sends it to client who displays it.
The server has to send some per second, maybe 3-5 would be good.

Should i use TCP or UDP for this?

Momentaly i tried UPD.

in the example below i used SEND_AT_TIME = 2012;
is that ok to send 2012 bytes in one packet or should it be higher or smaller?

the method compress only uses Deflater to compress the size, its effect is 1%-3% of the total size
private void sendData() {
		BufferedImage img = robot.createScreenCapture(screenSize);
		Graphics2D g = sharedScreen.createGraphics();	
		g.scale(sharedScreenSize.getWidth()/screenSize.getWidth(),sharedScreenSize.getHeight()/screenSize.getHeight());
		g.drawImage(img,0,0,null);
		g.dispose();
		
		short[] dataBuffer = ((DataBufferUShort)sharedScreen.getRaster().getDataBuffer()).getData();
		
		byte[] toSend = new byte[SEND_AT_TIME-4];
		ByteBuffer headerB = ByteBuffer.wrap(toSend);
				
	    byte[] comprToSend = new byte[SEND_AT_TIME];
	    ByteBuffer comprGoing = ByteBuffer.wrap(comprToSend);
	    
		for(int i = 0; i < dataBuffer.length; i += (SEND_AT_TIME-12)/2) {
		
			headerB.clear();
			headerB.putInt(i);

			int amount = (dataBuffer.length-i < (SEND_AT_TIME-12)/2)?dataBuffer.length-i:(SEND_AT_TIME-12)/2;
			headerB.putInt(amount);
		
			
			for(int j = 0; j < (SEND_AT_TIME-12)/2 && j+i < dataBuffer.length; j++)
				headerB.putShort(dataBuffer[j+i]);
			
			byte[] compressedData = compress(toSend);
			if(comprGoing.position() == 0) {
				
				if(compressedData.length < SEND_AT_TIME-4) {
					comprGoing.putShort((short)compressedData.length);
					comprGoing.put(compressedData);
					if(comprGoing.position() < SEND_AT_TIME-32) {
						continue;
					}
				}
				else {
					comprGoing.putShort((short)-toSend.length);
					comprGoing.put(toSend);
				}
			}
			else {
				
				if(comprGoing.position() + compressedData.length < SEND_AT_TIME-4) {
					comprGoing.putShort((short)compressedData.length);
					comprGoing.put(compressedData);
					if(comprGoing.position() < SEND_AT_TIME-32) {
						continue;
					}
				}
			}
			
			
			comprGoing.putShort((short)0);
			DatagramPacket packet = new DatagramPacket(comprToSend, comprToSend.length, udpAddr, 4446);
			try {
				
				udp.send(packet);
				Thread.sleep(1);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			finally {
				comprGoing.clear();
			}
		}
		
		if(comprGoing.position() != 0) {
			DatagramPacket packet = new DatagramPacket(comprToSend, comprToSend.length, udpAddr, 4446);
			try {
				
				udp.send(packet);
				Thread.sleep(1);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			finally {
				comprGoing.clear();
			}
		}
	}
the next method would be that i would use TCP and send only the lines of picture which changed, i cant do this in UDP, because i dont know if the lined arrived.

So my question is what should and shouldnt i use to send a screen at real time?

Thanks for help,

Juraj
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2009
Added on Aug 17 2009
1 comment
164 views