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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:23:15+00:00 2026-05-31T15:23:15+00:00

the following is my code. Editor: Eclipse, Platform: Windows. Its a chat application where

  • 0

the following is my code. Editor: Eclipse, Platform: Windows.

Its a chat application where 2 android emulators connect through a tcp socket.

UI consists of a send button, a text view and a text box.

Problem: As soon as I type text and hit send, the application crashes.

server port is 8000.
So my redirection is redir add tcp:8081:8000 and redir add tcp:8082:8000.

I donno what is wrong in my code. Please suggest me somthing I need to change.

public class HelloandroidActivity extends Activity 
{
/** Called when the activity is first created. */
public int serverport=8000;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   final EditText nameField = (EditText) findViewById(R.id.editText1);             
   final Button button2 = (Button) findViewById(R.id.button1);  
   Integer severport=8000;
   new Server().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,severport);
   button2.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
          final String name = nameField.getText().toString();
          final TextView tv = (TextView) findViewById(R.id.textView1);
          //tv.setText(name);

          String s=null;

        new Client().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,s);
     } 
          });// end onclicklis  

     }//end oncreate



 class Server extends AsyncTask <Integer, String, String>
{
public InetAddress byIpAsName ;
int r=0;
@Override
protected String doInBackground(Integer... serverport)  {
    //i[0]=serverport;
    Integer[] sp=serverport;
    BufferedReader in=null;
    ServerSocket s=null;
     r=sp[0];
    String cIn="";
    try {
        //byIpAsName = InetAddress.getByName("10.2.2.15");
        s=new ServerSocket(r);
    while(true)
    {
        Socket client = s.accept();
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String line=in.readLine();
        cIn=null;
        while(line!=null){cIn=cIn.concat(line);}

    }//while    

    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        s.close();
        in.close();
        } 
    catch (IOException e) {
        e.printStackTrace();
    }
    return cIn;


}//end inBackground
//@SuppressWarnings("null")
protected void onPostExecute(String... cIn)
{

  }//onpost execute

  }//server class
    public class Client extends AsyncTask<String, String, String>
    {
     PrintWriter out = null;
    BufferedReader in=null;
    String sIn=null;
     //Server s1=new Server();
    //int q=s1.r;
    TelephonyManager tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    String portStr = tel.getLine1Number().substring(tel.getLine1Number().length() - 4);
    int q = Integer.parseInt(portStr);
   Socket socket;
   @Override
   protected String doInBackground(String... params) {
   try 
   {
     //q=8080;
     InetAddress byIpAsName1=InetAddress.getByName("10.0.2.2");
     socket = new Socket(byIpAsName1, q);
     out = new PrintWriter(socket.getOutputStream(), true);
     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     String line=in.readLine();
      sIn=null;
     while(line!=null){sIn=sIn.concat(line);}
    } 
    catch (IOException e) {
    e.printStackTrace();
    }//catch
   return sIn;
    }//in background
    protected void onPostExecute(String... sIn)
    {
    String c=null;
    final TextView tv = (TextView) findViewById(R.id.textView1);
   c=c.concat(sIn[0]);
   tv.setText(c);
    }
     }  

  }//main class
  • 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-31T15:23:16+00:00Added an answer on May 31, 2026 at 3:23 pm

    From your logcat, what is important is this line:

    03-16 23:12:23.434: E/AndroidRuntime(571): java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10040 nor current process has android.permission.READ_PHONE_STATE.
    

    This indicates that in order to run your code, you need the READ_PHONE_STATE permission in the android manifest.xml.

    Add this line to the manifest, outside of the <application> tag but inside the <manifest> tag.

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

    If this does not solve the issue, the problem could be related to this answer.

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

Sidebar

Related Questions

I'm slowly working through an Android learning book and was given the following code
The following code will add the categories selector widget to the WordPress Page editor
Following code iterates through many data-rows, calcs some score per row and then sorts
I can't seem to run the following code in Eclipse. I do have a
So I wrote the following code in linux(Ubuntu) using the emacs text editor it
I have the following code in which I am using the application context to
I am using the following code: SharedPreferences.Editor edit = mPrefs.edit(); edit.putString(UUID, UUID.randomUUID().toString()); edit.commit(); //edit.apply();
I'm using the following code to render an editor for my model using ASP.NET
I am using the following code to convert contents in Editor(Ajax control) to pdf,
Our project in eclipse approximately shows the following folders: application - src - JRE

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.