TCP Client Socket - Receive incoming traffic

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

TCP Client Socket - Receive incoming traffic

Postby Kubax » Wed May 20, 2009 9:37 pm

Hi,

I'm just trying to write my first application in java.

The Application connects to my VDR (Media Center) and send's SVDR Commands like "CHAN +" so seitch channel up.
Now i'm trying to set the Volume, or get the EPG Date, but for this i need to get incoming traffic from the Server.
When i send "VOLU" to the Server, it responses with the Volume set atm.

The application works great at the Moment (Channel up, Channel down and 0 to 9 implemented right now)

could you help me how to get the answeres from the server?

here's my code from the client.java:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.e2esp.socket.test;
  2.  
  3.  
  4.  
  5. import android.content.SharedPreferences;
  6.  
  7. import java.io.BufferedReader;
  8.  
  9. import java.io.BufferedWriter;
  10.  
  11. import java.io.OutputStreamWriter;
  12.  
  13. import java.io.InputStreamReader;
  14.  
  15. import java.io.PrintWriter;
  16.  
  17. import java.net.InetAddress;
  18.  
  19. import java.net.ServerSocket;
  20.  
  21. import java.net.Socket;
  22.  
  23. import android.preference.PreferenceManager;
  24.  
  25.  
  26.  
  27. import android.util.Log;
  28.  
  29.  
  30.  
  31. public class TCPClient implements Runnable {
  32.  
  33.  
  34.  
  35.     public static final String DEFAULT_SERVERIP = "192.168.178.22";
  36.  
  37.     public static final int SERVERPORT = 2001;
  38.  
  39.     public static String message = "";
  40.  
  41.         public SharedPreferences prefs;
  42.  
  43.         public static final String PREFS_NAME = "MyPrefsFile";
  44.  
  45.        
  46.  
  47.     public void run() {
  48.  
  49.          try {
  50.  
  51.                  
  52.  
  53.                  /*SharedPreferences settings = this.
  54.  
  55.                  public String server = settings.getString("SERVERIP","DEFAULT_SERVERIP");
  56.  
  57.                  settings.*/
  58.  
  59.  
  60.  
  61.            InetAddress serverAddr = InetAddress.getByName(DEFAULT_SERVERIP);
  62.  
  63.                  //InetAddress serverAddr = InetAddress.getByName();
  64.  
  65.            
  66.  
  67.            Log.d("TCP", "C: Connecting...");
  68.  
  69.            Socket socket = new Socket(serverAddr, TCPClient.SERVERPORT);
  70.  
  71.            //String message = "CHAN +\n";
  72.  
  73.                try {
  74.  
  75.                 Log.d("TCP", "C: Sending: '" + message + "'");
  76.  
  77.                 PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
  78.  
  79.                 BufferedReader in = new BufferedReader(socket.getInputStream());
  80.  
  81.                 out.println(message);
  82.  
  83.                 Log.d("TCP", "C: Sent.");
  84.  
  85.                   Log.d("TCP", "C: Done.");
  86.  
  87.                  
  88.  
  89.              } catch(Exception e) {
  90.  
  91.                  Log.e("TCP", "S: Error", e);
  92.  
  93.                 } finally {
  94.  
  95.                   socket.close();
  96.  
  97.                 }
  98.  
  99.          } catch (Exception e) {
  100.  
  101.               Log.e("TCP", "C: Error", e);
  102.  
  103.          }
  104.  
  105.     }
  106.  
  107.     }
  108.  
  109.  
  110.  
  111.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Kubax
Freshman
Freshman
 
Posts: 2
Joined: Tue May 19, 2009 11:39 am

Top

Postby Couillette » Fri May 22, 2009 12:35 pm

Hello.

2 ways :
- If the server only send data after the reception of a command, you can use a BufferedReader and the method readLine or a BufferedInputStream. Like you already done here.

You would have a thing like that under the println line out.println(message);
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String message = in.readLine();
  3.  
  4.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


If it doesn"t work, use a BufferedInputStream, if the server doesn't put a new line after the message, the readLine will not pass.

- If the server send state messages or thing like that, you must use a thread to read on the socket.

Bye
Couillette
Freshman
Freshman
 
Posts: 6
Joined: Fri May 22, 2009 11:57 am
Location: France

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests