Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8205395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:13:23+00:00 2026-06-07T08:13:23+00:00

This might be a complex problem with my application but I’ll do my best

  • 0

This might be a complex problem with my application but I’ll do my best to describe it as accurately as I can.

I am making an Android Client and making use of a couple of helper classes someone else handed to me at work. The helper Android classes are called TcpClient.java and PVDCAndroidClient.java. PVDCAndroidClient.java makes use out of the TcpCLient, using a tcpCLient object to connect via serverIP and port.

Here is PVDCAndroidClient.java:

    public class PVDCAndroidClient {

    // constants
    public static final String DEFAULT_LOGIN_URI = "http://me.net:8000/";


    private TcpClient tcpClient = null;
    private UdpClient udpClient = null;
    private boolean connected = false;
    private boolean loggedin = false;
    private static SimpleDateFormat sdf;
    private String loginURI = DEFAULT_LOGIN_URI;
    private int getUserNumber;
    TcpMessageListener listener = null;

    /**
     * Connects to proxy server, blocks until complete or timeout
     * @param serverIP
     * @param port
     */
    public void connect(String serverIP, int port)
    {
        try
          {
            if(serverIP.length() != 0 && port != 0)
            {
              tcpClient = new TcpClient();

              tcpClient.addTcpListener(listener);
              tcpClient.connect(serverIP, port);

            }
          }
        catch(Exception e)
        {
            e.printStackTrace();
            Log.d("Could not connect to server, possbile timeout...", "error");
        }



    }

    ///// Make login function a blocking call

    // Default login, use last location as login location
    public boolean login(String fName, String lName, String password)
    {
        return this.login(fName, lName, password, "last location");
    }

    public boolean login(String fName, String lName, String password, String region)
    {
        return this.login(fName, lName, password, "last location", 128, 128, 20);
    }

    public boolean login(String fName, String lName, String password, String region, int loginX, int loginY, int loginZ)
    {            sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
                 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        // Check passed values
            // x, y and z should be between [0, 256]
            // strings should not be null or empty(except lName which can be empty)
        if((loginX >= 0 && loginX <= 256) && (loginY >=0 && loginY <= 256) && (loginZ >= 0 && loginZ <= 256))
        {
            if(fName.length() != 0 && fName != null)
            {


                // Construct packet xml structure

                // Send request and wait until reply or timeout

                // return false if timeout (or throw exception?)
                // if not timeout, read result packet and determine return value

                // getUserNumber = tcpClient.getUserNum();

                StringWriter stringWriter = new StringWriter();


                XmlSerializer serializer = Xml.newSerializer();

                try {

                    serializer.setOutput(stringWriter);

                    // Indentation is not required, but helps with reading
                    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

                    // TODO: Remove hardcoding of strings, make either constants or place in strings.xml
                    // TODO: Move the header construction to other method as it is fairly constant other than request num and no need to repeat that much code
                    serializer.startTag("", "pvdc_pkt");

                        serializer.startTag("", "pvdc_header");




                            // TODO: replace this with a string unique to the system
                            serializer.startTag("", "ID");
                                serializer.text("838393djdjdjd");
                            serializer.endTag("", "ID");

                            // TODO: replace this with actual user number from server
                            serializer.startTag("", "user_num");
                                serializer.text("22");//get userNum from above
                            serializer.endTag("", "user_num");

                            // TODO: add a request number counter to increment this on each request
                            serializer.startTag("", "request_num");
                                serializer.text("1");
                            serializer.endTag("", "request_num");

                            serializer.startTag("", "DateTime");
                                serializer.text(sdf.toString()); //utc time variable.
                            serializer.endTag("", "DateTime");

                        serializer.endTag("", "pvdc_header");

                        serializer.startTag("", "pvdc_content");
                            serializer.attribute("", "type", "requestlogin");

                            serializer.startTag("", "name");
                                serializer.attribute("", "fname", fName);
                                serializer.attribute("", "lname", lName);
                            serializer.endTag("", "name");

                            serializer.startTag("", "password");
                                serializer.text(password);
                            serializer.endTag("", "password");

                            serializer.startTag("", "server");
                                serializer.text(this.loginURI);
                            serializer.endTag("", "server");

                            serializer.startTag("", "location");
                                serializer.attribute("", "region", region);
                                serializer.text(loginX + ";" + loginY +";" + loginZ);
                            serializer.endTag("", "location");

                        serializer.endTag("", "pvdc_content");

                    serializer.endTag("", "pvdc_pkt");

                    // Finish writing
                    serializer.endDocument();
                    // write xml data out
                    serializer.flush();

                    // 
                    sendLogin(stringWriter);

                } catch (Exception e) {
                    Log.e("Exception", "error occurred while creating xml file");
                    return false;
                }
                // Print out xml for debugging
                Log.d("PVDCAndroidClient Login", stringWriter.toString().trim());

            }
            else
            {
                Log.d("Error in name checking", "fName either blank or null");
            }

         }
        else
        {
            Log.d("login coordinates X,Y, or Z not between 0-256", "Coordinates Error");
        }

        return true;
       }


    // moveString should contain the properly formatted movement command(s)[see above move request description]
    public void sendLogin(StringWriter stringWriter)
    {
        tcpClient.sendMessage(stringWriter.toString());
    }
}

Here is the actual TcpClient.java:

    public class TcpClient {

    public interface TcpMessageListener{

        public void onMessage(TcpClient client, String message);
    }

    private Socket socket = null;
    private PrintWriter out = null;
    private BufferedReader in = null;
    private Thread listenThread = null;
    private boolean listening = false;
    private int userNum = -1;
    private List<TcpMessageListener> listeners = new ArrayList<TcpMessageListener>();


    public int getUserNum()
    {
        return this.userNum;
    }

    public TcpClient() {

    }

    public void addTcpListener(TcpMessageListener listener)
    {
        synchronized(this.listeners)
        {
            this.listeners.add(listener);
        }
    }

    public void removeTcpListener(TcpMessageListener listener)
    {
        synchronized(this.listeners)
        {
            this.listeners.remove(listener);
        }
    }

    public boolean connect(String serverIpOrHost, int port) {
        try {
            socket = new Socket(serverIpOrHost, port);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            this.listenThread = new Thread(new Runnable(){

                public void run() {
                    int charsRead = 0;
                    char[] buff = new char[4096];
                    while(listening && charsRead >= 0)
                    {
                        try {
                            charsRead = in.read(buff);
                            if(charsRead > 0)
                            {
                                Log.d("TCPClient",new String(buff).trim());
                                String input = new String(buff).trim();
                                synchronized(listeners)
                                {
                                    for(TcpMessageListener l : listeners){
                                        l.onMessage(TcpClient.this, input);
                                    }
                                }

                                if (input.toLowerCase().contains("<user_num>")){
                                    int index = input.toLowerCase().indexOf("<user_num>");
                                    index += "<user_num>".length();
                                    int index2 = input.toLowerCase().indexOf("</user_num>");
                                    userNum = Integer.parseInt(input.substring(index,  index2));

                                }
                            }
                        } catch (IOException e) {
                            Log.e("TCPClient", "IOException while reading input stream");
                            listening = false;
                        }
                    }
                }

            });

            this.listening = true;
            this.listenThread.setDaemon(true);
            this.listenThread.start();

        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            return false;
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection");
            return false;
        } catch (Exception e) {
            System.err.println(e.getMessage().toString());
            return false;
        }
        return true;
    }

    public void sendMessage(String msg) {
        if(out != null)
        {
            out.println(msg);
            out.flush();
        }
    }

    public void disconnect() {
        try {
            if(out != null){
                out.close();
                out = null;
            }
            if(in != null){
                in.close();
                in = null;
            }
            if (socket != null) {
                socket.close();
                socket = null;
            }
            if(this.listenThread != null){
                this.listening = false;
                this.listenThread.interrupt();
            }
            this.userNum = -1;
        } catch (IOException ioe) {
            System.err.println("I/O error in closing connection.");
        }
    }
}

LASTLY, here is what I have been coding today and cannot seem to get this to work. I don’t get any blatant exceptions, just a warning on Logcat, that says, “Couldn’t get I/O for the connection”.

    public class AndroidClientCompnt extends Activity {

    private TcpClient myTcpClient = null;
    private UdpClient udpClient;
    private static final String IP_ADDRESS_SHARED_PREFS = "ipAddressPref";
    private static final String PORT_SHARED_PREFS = "portNumberPref";
    private String encryptPassLoginActivity;
    private String getIpAddressSharedPrefs;
    private String getPassword, getName, getRegionSelect, getGridSelect;
    private String fName, lName;
    private SharedPreferences settings;
    private boolean resultCheck = false;
    private int portNum;
    PVDCAndroidClient client;
    private String name;
    private CharSequence[] getView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);

        Intent intent = getIntent();
        // getting object's properties from LoginActivity class.

        getName = intent.getStringExtra("name");
        getPassword = intent.getStringExtra("password");
        getRegionSelect = intent.getStringExtra("regionSelect");
        getGridSelect = intent.getStringExtra("gridSelect");

        Log.d("VARIABLES", "getName = " + getName + "getPassword" + getPassword
                + "getRegionSelect = " + getRegionSelect + ".");
        setResult(Activity.RESULT_CANCELED);

        client = new PVDCAndroidClient();
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Take care of getting user's login information:

        // grid selected as well? sometime?

        settings = PreferenceManager.getDefaultSharedPreferences(this);

        getIpAddressSharedPrefs = settings.getString(IP_ADDRESS_SHARED_PREFS,
                "");
        portNum = Integer.parseInt(settings.getString(PORT_SHARED_PREFS, ""));
        Log.d("SHARED" + getIpAddressSharedPrefs + "port " + portNum, "");

        if (getIpAddressSharedPrefs.length() != 0 && portNum != 0) {

            try {
                // first connect attempt.

                client.connect(getIpAddressSharedPrefs, portNum);
                resultCheck = client.isConnected();

                // here is where I want to call Async to do login
                // or do whatever else.

                UploadTask task = new UploadTask();
                task.execute();

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Could not connect.",
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        } else {
            Toast.makeText(getApplicationContext(),
                    "Ip preference and port blank", Toast.LENGTH_LONG).show();
        }

        finish();
    }

    private class UploadTask extends AsyncTask<String, Integer, Void> {
        @Override
        protected void onPreExecute() {
            Toast.makeText(getApplicationContext(), "Loading...",
                    Toast.LENGTH_LONG).show();
        }

        @Override
        protected Void doInBackground(String... names) {

            // encrypting user's password with Md5Hash class.
            try {
                encryptPassLoginActivity = MdHashing
                        .MD5(getPassword.toString());
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (resultCheck == true) {

                String[] firstAndLast;

                String spcDelmt = " ";

                firstAndLast = name.toString().split(spcDelmt);

                fName = firstAndLast[0];
                lName = firstAndLast[1];

                // set up the tcp client to sent the information to the
                // server.
                client.login(fName, lName, encryptPassLoginActivity,
                        getRegionSelect, 128, 128, 20);

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            Intent goToInWorld = new Intent(
                    AndroidClientCompnt.this.getApplicationContext(),
                    PocketVDCActivity.class);
            startActivity(goToInWorld);
            Toast.makeText(getApplicationContext(), "Connected",
                    Toast.LENGTH_LONG).show();

        }

    }

}

I know this is a super long post and I am asking a lot but if anyone could take a look at this I would very much appreciate it. I’ve been at this all day, trying to make use of these helper classes I got and can’t get it to work. It also doesn’t help that I’m not too experienced in this client/server stuff. Any nudges in the right direction or an accepted solution would REALLY mean something to me.

Thank you kindly,

Have a good evening.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:13:25+00:00Added an answer on June 7, 2026 at 8:13 am

    1) Make sure you have the following permission in your Android-Manifest file:

     <uses-permission android:name="android.permission.INTERNET/> 
    

    w/o this you definitely won’t be making any tcp/ip connections.

    2) You will want to run the code in debug mode, and place breakpoints where the connection information
    is set and also what results are at several points. In other words you need to dig deeper.

    If you are somewhat new to coding there is no better investment of time than in running the debugger and stepping line by line through the code. Code only comes to life inside a debugger, where you can see the values of variables and results. So set several breakpoints, step through and you will see more. It is more difficult to debug where there are threads however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a simple question but I've searched and searched and can't find
This might be a very basic question but it confuses me. Can two different
Has anyone got any tips for this problem i'm facing. Its not complex, but
This might seem like a very easy question for some of you folks, but
This might sound like a stupid question but just trying to learn something here.
This might have to be broken down a bit but what i want to
This might be a general mvp places and activities question, but the show case
This might sound rudimentary but, I have created tables by running queries in Workbench
this question might sound a little bit weired... But I will try to explain:
Okay, this might be a very silly beginner question, but: I've got an ClassA,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.