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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:27:37+00:00 2026-06-16T12:27:37+00:00

I am pretty new to android and java programming and I need your help.

  • 0

I am pretty new to android and java programming and I need your help. I want to create an Android client and a server on my PC (Windows 7). I checked with putty (a program which imitates a client (without programming mistakes^^)) whether my server is programmed without mistakes. Thereby I recognized that my server is programmed correctly.

Here you can see my well-working server:

  public class MyServer {
  public static void main (String[] args) throws IOException {

  ServerSocket serverSocket = null;

  try {
      serverSocket = new ServerSocket(4449);
      System.out.println("Listening on port: 4449");
  } catch (IOException e){
      System.err.println("Could not listen on port 4449.");
      System.exit(1);
  }

  Socket clientSocket = null;

  try {
      clientSocket = serverSocket.accept();
   System.out.println("Got connection.");
  } catch (IOException e) {
      System.err.println("Accept failed: 4449.");
      System.exit(1);
  }

  BufferedReader in = null;
  PrintWriter out = null;
  try {
      in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

      out = new PrintWriter(clientSocket.getOutputStream(), true);
  } catch (IOException e) {
      System.err.println("Read failed");
      e.printStackTrace();
  }


  try {

      System.out.println("message: " + in.readLine());
      out.println("hab was bekommen!");
  } catch (IOException e) {
      System.err.println("Can't get a message from Client.");
      e.printStackTrace();

  }
  }
  }

I also tried to create an Android client for this server, but i didn’t managed it.

Here is my Mainactivity:

public class AndroidClient extends Activity {

EditText textOut;
TextView textIn;
TextView problems;

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

    textOut = (EditText)findViewById(R.id.textout);
    Button buttonSend = (Button)findViewById(R.id.send);
    textIn = (TextView)findViewById(R.id.textin);
    problems = (TextView)findViewById(R.id.problems);
    buttonSend.setOnClickListener(buttonSendOnClickListener);
}

Button.OnClickListener buttonSendOnClickListener 
= new Button.OnClickListener() {


    @Override
    public void onClick(View arg0) {
        //TODO Auto-generated method stub
        Socket client = null;


        BufferedReader in = null;
        PrintWriter out = null;

        try {
            client = new Socket("192.168.2.107", 4449);
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (UnknownHostException e) {
            problems.setText("Unknown host: 192.168.2.107");
        } catch (IOException e) {
            // System.out.println("No Input/Output.");
            problems.setText("No Input/Output.");
        }

        try {
            out.println("Hallo.");
            textIn.setText(in.readLine());
        } catch (IOException e) {
            problems.setText("Can't send/ get message.");
        }
    }   
};



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_android_client, menu);
    return true;
}
    }

So far so good. When i start my app on my android smartphone the app starts without any problem. Then i type something in my EditText field ‘textOut’ und pushed to ‘Send’ button. Then my screen gets black and the app closes itself. I also pasted the permission in the AndroidManifest.xml to enter the internet and to use access Wifi.

I hope that someone can help me, because i am working on this problem for 2 days and haven’t found any solution yet. I mainly used this side (http://android-er.blogspot.de/2011/01/simple-communication-using.html) and of course some other sides, but there i got most information. Furthermore there are no shown mistakes in my source code.

With kind regards,

Lukas5060

edit: Here is my LogCat:

12-28 20:21:55.929: I/dalvikvm(698): threadid=3: reacting to signal 3
12-28 20:21:56.039: I/dalvikvm(698): Wrote stack traces to ‘/data/anr/traces.txt’
12-28 20:21:56.259: I/dalvikvm(698): threadid=3: reacting to signal 3
12-28 20:21:56.299: I/dalvikvm(698): Wrote stack traces to ‘/data/anr/traces.txt’
12-28 20:21:56.741: D/gralloc_goldfish(698): Emulator without GPU emulation detected.
12-28 20:21:56.771: I/dalvikvm(698): threadid=3: reacting to signal 3
12-28 20:21:56.789: I/dalvikvm(698): Wrote stack traces to ‘/data/anr/traces.txt’
12-28 20:23:58.430: D/AndroidRuntime(698): Shutting down VM
12-28 20:23:58.430: W/dalvikvm(698): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-28 20:23:58.470: E/AndroidRuntime(698): FATAL EXCEPTION: main
12-28 20:23:58.470: E/AndroidRuntime(698): android.os.NetworkOnMainThreadException
12-28 20:23:58.470: E/AndroidRuntime(698): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
12-28 20:23:58.470: E/AndroidRuntime(698): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-28 20:23:58.470: E/AndroidRuntime(698): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-28 20:23:58.470: E/AndroidRuntime(698): at libcore.io.IoBridge.connect(IoBridge.java:112)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.net.Socket.startupSocket(Socket.java:566)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.net.Socket.tryAllAddresses(Socket.java:127)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.net.Socket.(Socket.java:177)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.net.Socket.(Socket.java:149)
12-28 20:23:58.470: E/AndroidRuntime(698): at net.ibasic.AndroidClient$1.onClick(AndroidClient.java:50)
12-28 20:23:58.470: E/AndroidRuntime(698): at android.view.View.performClick(View.java:3511)
12-28 20:23:58.470: E/AndroidRuntime(698): at android.view.View$PerformClick.run(View.java:14105)
12-28 20:23:58.470: E/AndroidRuntime(698): at android.os.Handler.handleCallback(Handler.java:605)
12-28 20:23:58.470: E/AndroidRuntime(698): at android.os.Handler.dispatchMessage(Handler.java:92)
12-28 20:23:58.470: E/AndroidRuntime(698): at android.os.Looper.loop(Looper.java:137)
12-28 20:23:58.470: E/AndroidRuntime(698): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 20:23:58.470: E/AndroidRuntime(698): at java.lang.reflect.Method.invoke(Method.java:511)
12-28 20:23:58.470: E/AndroidRuntime(698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-28 20:23:58.470: E/AndroidRuntime(698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-28 20:23:58.470: E/AndroidRuntime(698): at dalvik.system.NativeStart.main(Native Method)
12-28 20:23:59.070: I/dalvikvm(698): threadid=3: reacting to signal 3
12-28 20:23:59.100: I/dalvikvm(698): Wrote stack traces to ‘/data/anr/traces.txt’
12-28 20:24:02.420: I/Process(698): Sending signal. PID: 698 SIG: 9

  • 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-16T12:27:39+00:00Added an answer on June 16, 2026 at 12:27 pm

    Okay my guess is that the problem is that your doing this in the main ui thread which is far from recommended. Instead what you should do is checkout AsyncTask which is the preferred way of doing this kind of operations (reading from files or web and a bunch of other stuff).

    This is easier than it sounds what you need to do is something like this (note this is just to give you a feeling about how it looks):

    private class CreateSocketTask extends AsyncTask<String, Void, String> {
    
        @Override
        protected String doInBackground(String... urls) {
            //Do the socket stuff here...
        }
    
        @Override
        protected void onPostExecute(String result) {
            //This is called when doInBackground has finished
            //From here you'd call a method in the main ui class.
        }
    }
    

    Heres the android doc

    There are a bunch of tutorials on the net so you’ll be up and running in no time.

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

Sidebar

Related Questions

i am pretty new to android and java programming. I am programming a server
I'm pretty new to learning about java and android development; so please help me
I'm pretty new to Android, I do know some Java though. I want to
I'm pretty new to Android programming so any help would be great :) I'm
I am pretty new to android app development and java and I encountered the
I am completely new to android, and pretty much a Java newb. I have
I'm pretty new to Android programming so bear with me. I was wondering if
I'm pretty new to Android and Java, though I've really been excited about what
I'm pretty new to Android/java, and have come up against my first head-scratcher. I'm
I'm pretty new to android, Java and sqlite. For my first program I'm creating

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.