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 6811203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:17:54+00:00 2026-05-26T20:17:54+00:00

I have a problem with my android application which should connect to java tcp

  • 0

I have a problem with my android application which should connect to java tcp server. I’m getting Connection refused error.

server.java

public class Server
{
public static void main(String argv[]) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(6789);
System.out.println("welcome into server");
while(true)
{

Socket connectionSocket = welcomeSocket.accept();
if (connectionSocket != null)
{
System.out.println(connectionSocket);
Client client = new Client(connectionSocket);
client.start();
}
}
}
}

class Client extends Thread

private Socket connectionSocket;
private String clientSentence;
private String ans;
private String temak="test";
private String capitalizedSentence;
private BufferedReader inFromClient;
private DataOutputStream outToClient;

public Client(Socket c) throws IOException
{
connectionSocket = c;
}

public void run() 
{
try
{ 
inFromClient = new BufferedReader(new nputStreamReader(connectionSocket.getInputStream()));
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println(clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
ans = inFromUser.readLine();
outToClient.writeBytes(capitalizedSentence);
if("hej".equals(clientSentence)){
outToClient.writeBytes(ans + " tester hej"+ "\n");
}
else{
outToClient.writeBytes(ans+"\n");
}
}
catch(IOException e)
{
System.out.println("Errore: " + e);
}
}
}

snippet from client on android:

String sentence = null; 
String modifiedSentence; 
String tempus;
try {

Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Log.d("pre","TCP Connected.");

outToServer.writeBytes(sentence + 'n');
modifiedSentence = inFromServer.readLine();
Log.d("sentence ", modifiedSentence);
Log.d("post","TCP Success !!!");

clientSocket.close();

} catch (Exception e) {
Log.d("error","TCP Error: " + e.toString());
}

program is trying to connect on localhost on port 6789

  • 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-05-26T20:17:55+00:00Added an answer on May 26, 2026 at 8:17 pm

    “localhost” it’s not your pc/mac/whatever use you pc ip or special ip (10.0.2.2)
    for more check: https://developer.android.com/studio/run/emulator-commandline.html#emulatornetworking

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

Sidebar

Related Questions

I have an interesting problem being reported to me from an android application I
I need an Android application which should be able to fetch data from the
I have built a multi-touch application which is based on a Java EE backend
I have an Android application that generates some HTML which is rendered locally, in
I am developing an android application in which i have to store a bitmap
I am developing an android application in which i have to implement custom rating
I have the following problem: I am working on an Android application where at
I have a service running in my Android application which contains a HashMap that
Project Description: I have to make an application in android which gets the Latitude
I have an Android application, which uses javax.crypto to encrypt some text data in

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.