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

  • Home
  • SEARCH
  • 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 9188205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:56:59+00:00 2026-06-17T19:56:59+00:00

I actually want to write a TCP communication between a Java Server and my

  • 0

I actually want to write a TCP communication between a Java Server and my Android game, to save data to a database. Used to have a Global leadboard.

I actually wrote the server right now and can connect to it with a client i wrote and want to implement later in Android.
Here is some code from the Client:

    public void saveToDatabase(String name , int level, int killpoints){
        try {
            Socket soc = new Socket("localhost", PORT);
            DataOutputStream  out = new DataOutputStream(soc.getOutputStream());
            DataInputStream in = new DataInputStream(new BufferedInputStream(soc.getInputStream()));

            //to call the save statement!
            out.writeInt(0);
            //give the stuff 

            out.writeUTF(name);
            out.writeInt(level);
            out.writeInt(level);

            //close it
            out.close();
            in.close();
            soc.close();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
//          e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
//          e.printStackTrace();
        }
    }

What the server now does is it get the first line and checks what to do depending on the value. At the Server this happens:

public void run() {
        try {
            DataOutputStream out = new DataOutputStream (socket.getOutputStream());
//          DataInputStream  in = new DataInputStream(new InputStreamReader(
//                  socket.getInputStream()));
            DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            Server.textArea.setText(Server.textArea.getText()+"Client connected: "+ clientNumber+ "\n");

            int firstLine = in.readInt(); //get first line for switch

            switch (firstLine) {
            case 0:
//              getting the whole objekt for the database in own lines!
                String name = in.readUTF();
                int level = in.readInt();
                int kp = in.readInt();
                Server.textArea.setText(Server.textArea.getText() + "SAVE: "
                        + name + " Level: " + level + " KP: " + kp + "\n");
//              Server.textArea.setText(Server.textArea.getText() + "SAVE called\n");       
                break;
            case 1:
                // else if return the top10
                Server.textArea.setText(Server.textArea.getText()
                        + "Return top10\n");
                // outp.write("asdf string");
                break;
            default:
                break;
            }

            out.close();
            in.close();
            socket.close();
            Server.textArea.setText(Server.textArea.getText()
                    + "Client disconnected: "+ clientNumber + "\n");
        } catch (Exception e) {
            System.out.println("IO error " + e);
        }
    }

I log everything on serverside to a textarea and acutally get this on serverside:
consolebug

I commit com.saveToDatabase("test", 2, 123651);
If i sent everything as UTF i actually get also some strage stuff:enter image description here

Hope you can help me out or even can tell me if this would work with android too. Thanks alot.
At the moment it does not transfare the string correct.

SAVE:  Level: 1908 KP: 1702065249
Client disconnected: 0
  • 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-17T19:57:00+00:00Added an answer on June 17, 2026 at 7:57 pm

    Your code has too many issues.
    You write an int (writeInt) but you read a byte (read) on the server.
    Also writeUTF puts just a string on the wire but you read a string with a new line.
    Use DataInputStream to read the corresponding DataOutputStream.
    Also close the output stream before the socket.

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

Sidebar

Related Questions

Actually i want that, suppose i have a colorcode #FF3366. How can i examine
Hi every one actually I want to make a com.google.gson.JsonArray of data:[1,2,3,4,5].How can i
I got a question ;) Actually I want to use core-data in different threads.
I have posted 3 queries here. Actually I want to join 1st & 2nd
I want to write a container class that acts like a dictionary (actually derives
I am using Glassfish. I actually want to connect two database at certain time
I want to write a script which can Shutdown remote Ubuntu system. Actually i
Actually i want to call a function of one activity from another activity..i write
Actually i want to know how to store data from my app in the
so actually i'm writing client/server programs using TCP/IP. In this case, client can upload

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.