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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:16:45+00:00 2026-05-25T01:16:45+00:00

I am testing socket programming on Android however I am having a problem. The

  • 0

I am testing socket programming on Android however I am having a problem. The client is basically launched through the main activity with a basic function of sending a message to the server and getting a reply.

the client activity:

package com.test.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class socketActivity extends Activity implements OnClickListener {

String input;
private EditText et;
private ObjectOutputStream oos;
private TextView tv;
private String message;
private ObjectInputStream ois;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try{

        InetAddress host = InetAddress.getLocalHost();
        Socket socket = new Socket(host.getHostName(),7777);
        //send to server

        oos = new ObjectOutputStream(socket.getOutputStream());

        et = (EditText) findViewById(R.id.text);
        Button sendButton = (Button) findViewById(R.id.button);
        sendButton.setOnClickListener(this);

        //read from server
        ois = new ObjectInputStream(socket.getInputStream());


        //System.out.println("Message from Server: "+message);
        tv = (TextView) findViewById(R.id.textView);

    }

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


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.button:
        input = et.getText().toString();
        try {
            oos.writeObject(input);
            ois.close();
            oos.close();
            message = (String) ois.readObject();
            tv.setText("Message from Server: "+message);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    }
}



}

The Server class launched separately from JCreator listening to port 7777:

import java.io.*;
import java.lang.*;
import java.net.*;

public class server {

private ServerSocket server;
private int port = 7777;

public server()
{
    try{
        server = new ServerSocket(port);
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }


}

public static void main(String args[])
{
    server example = new server();
    example.handleConnection();
}

public void handleConnection()
{
    System.out.println("Waiting for client message...");

    while(true)
    {
        try{
            Socket socket = server.accept();
            new ConnectionHandler(socket);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

}

ConnectionHandler class which the Server accesses:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public class ConnectionHandler implements Runnable {

private Socket socket;

public ConnectionHandler(Socket socket)
{
    this.socket = socket;

    Thread t = new Thread(this);
    t.start();
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try{

        //receive from client
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        String message = (String) ois.readObject();

        System.out.println("Message from client: "+message);

        if(message.equals("check"))
        {
            System.out.println("Checking for malicious interference...");
            Thread.sleep(5000);
            System.out.println("Status: Files are good.");
        }

        //send response to client
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());


        if(message.equals("check"))
        {
            oos.writeObject("Checking...");
        }

        else oos.writeObject("Invalid input");



        ois.close();
        oos.close();
        socket.close();

        System.out.println("Waiting for client message...");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

I have tested the code on JCreator and Eclipse through Java application not Android app and it worked perfect. However when I try doing it through the activity, it’s not working.

Any ideas?

  • 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-25T01:16:45+00:00Added an answer on May 25, 2026 at 1:16 am

    In Android, localhost would point to the phone/emulator itself. It doesn’t point to your server. The following line below is pointing to your Android device/emulator and not your server. You need to get the actual IP of the server to get it working from your Android

    
    
        InetAddress host = InetAddress.getLocalHost();
        Socket socket = new Socket(host.getHostName(),7777);
    
    

    It works through Java App, because when you execute it from the context of Java Application, the localhost is pointing to the same server location.

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

Sidebar

Related Questions

I am testing my socket programs at home, in local network. Server and client
I've been testing PHP socket listening, and ran into the aforementioned problem. My test
Based on my reading and testing, with asynchronous sockets, the socket itself can be
I wrote a socket client in java that 24/7 connected to a socket server
So I have a simple socket server on an android emulator. When I'm only
I am testing out a Socket Server application in c and I am getting
I am using Selenium for web application testing. After having finished all the testing,
We are currently testing a Telecom application over IP. We open a Raw Socket
I'm trying to get socket.io running with my SSL certificate however, it will not
I have a Java app, connecting through TCP socket to a server developed 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.