I'm writing a simple remote PC app (mouse-keyboard). Android is client and is connect with WiFi to Java PC Server. I'm using TCP but I see a bit of latency compared to other remote apps. Then I used UDP and didn't see any latency or wrong data. UDP look like it's working well. I'm sending a String to Server like these: "a","B","2". Can I use UDP for keyboard remoting?
Client on Android (TCP):
Sending to Server a String method like these("a","C","5"):
public void commandto(String sip, int port ,String byt) {
try
{
Socket sockClient = new Socket();
sockClient.connect(new InetSocketAddress(sip,port),9000);
//os= new DataOutputStream(sockClient.getOutputStream());
//OutputStream os = sockClient.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sockClient.getOutputStream())),true);
out.println(byt);
out.flush();
Log.w("Client", "Client sent message");
sockClient.close();
}catch (final UnknownHostException e)
{
//
}catch (final IOException e)
{
//
}
}
TCP Server code:
private void cAccept (int portt) throws AWTException {
try{
ss = new ServerSocket(portt);
}catch (IOException e){
lblInf.setText("Could not listen on port :"+portt+" "+e.getMessage());
}
lblInf.setText("Server is started!");
}
while(true){
try{
s = ss.accept();
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String command =br.readLine();
System.out.println(command+"\n");
if(command!=""){
type(command);
}
s.close();
br.close();
}catch (IOException e){
try {
if(s!=null)
s.close();
if(br!=null)
br.close();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}
lblInf.setText("Server is stopped!");
lblInf2.setText("");
}
}
}
Start Server Button code:
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnStart.setEnabled(false);
btnStop.setEnabled(true);
thread = new Thread() {
public void run(){
try {
cAccept(9512);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread.start();
} });
As I've said, I especially see better latency for upper case. If I use UDP, using the following code.
UDP Client sending a String...
private void runUdpClient(String a) {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
InetAddress serverAddr = InetAddress.getByName("192.168.2.100");
DatagramPacket dp;
dp = new DatagramPacket(a.getBytes(), a.length(), serverAddr, 9512);
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
UDP server:
private void runUdpServer() {
while(true){
String lText;
byte[] lMsg = new byte[1000];
DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
DatagramSocket ds = null;
try {
ds = new DatagramSocket(9512);
//disable timeout for testing
//ds.setSoTimeout(100000);
ds.receive(dp);
lText = new String(lMsg, 0, dp.getLength());
if(lText!=""){
type(lText);
System.out.println(lText);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
}
How can I improve the speed? Or should I continue with UDP? If you have any idea or something, I would like to hear it.
EDİT 1 :
I've changed somethings for this answer.
"Then, when I look at your code, i see that you are creating a new TCP socket for every single character that you send."
public class TCPclient extends Activity {
Socket sockClient;
PrintWriter out;
.
.
.
}
and method
public void commandto(String sip, int port ,String byt) {
try
{
sockClient = new Socket();
.
.
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sockClient.getOutputStream())),true);
...
}
}
and I've changed it with this on TCP Server side.
String command =br.readLine();
System.out.println(command+"\n");
if(command!=""){
type(command);
}
-
private void cAccept (int portt) throws AWTException {
.
.
.
while(true){
....
command =br.readLine();
System.out.println(command+"\n");
type(command)
}
}
Already I've using these on TCP Server
public class TCPServer extends JFrame{
...
private ServerSocket ss= null;
private Socket s =null;
private BufferedReader br=null;
...
}
Now is looking better. But I see a bit of latency compared to other remote apps. How can I more improve it for latency?