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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:37:18+00:00 2026-05-26T22:37:18+00:00

this is my first Android app and I’m trying to use Sockets to send

  • 0

this is my first Android app and I’m trying to use Sockets to send text from my computer (server) to my phone (client). I’ve programmed in Java a lot before, but always with DrJava, not Eclipse, which is what I’m having to use for Android programming.

The following code is simply an example Java program using Sockets that I just added and removed a few lines and pasted it into the Android code. The code is found here: http://zerioh.tripod.com/ressources/sockets.html
If you don’t trust links, you can Google “java socket example.” It should be the first link.

I’m getting an uncaught exception error, but I’ve looked through the code many times and I can’t find a place without a catch line. The code works with Java, but not Android, so I think it’s some issue with Android-specific syntax.

Here is the Android code (client):

package com.example.helloandroid;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //TextView tv = new TextView(this);
    //tv.setText("Eclipse is a horrible program");
    Requester client = new Requester();
    client.run();
    //setContentView(tv);
}
}

class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
void run()
{
    try{
        //1. creating a socket to connect to the server
        requestSocket = new Socket("localhost", 2004);
        System.out.println("Connected to localhost in port 2004");
        //2. get Input and Output streams
        out = new ObjectOutputStream(requestSocket.getOutputStream());
        out.flush();
        in = new ObjectInputStream(requestSocket.getInputStream());
        //3: Communicating with the server
        try{
            message = (String)in.readObject();
            System.out.println("server>" + message);
            sendMessage("asdfjksajdflksjdklfjsdklfjlsdf");
            message = "bye";
            sendMessage(message);
        }
        catch(ClassNotFoundException classNot){
            System.err.println("data received in unknown format");
        }
    }
    catch(UnknownHostException unknownHost) {
        System.err.println("You are trying to connect to an unknown host!");
    }
    catch(IOException ioException) {
        ioException.printStackTrace();
    }
    finally{
        //4: Closing connection
        try{
            in.close();
            out.close();
            requestSocket.close();
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
}
void sendMessage(String msg)
{
    try{
        out.writeObject(msg);
        out.flush();
        System.out.println("client>" + msg);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    /*catch(IOException ioException){
        ioException.printStackTrace();
    }*/
}
}

The server code is the same as on the website.

Here is the catlog lines that I think are relevant. If I missed some, let me know and I’ll post those too:

11-20 22:37:35.314: W/System.err(545): java.net.SocketException: Permission denied (maybe missing INTERNET permission)
11-20 22:37:35.324: W/System.err(545):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
11-20 22:37:35.324: W/System.err(545):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:187)
11-20 22:37:35.324: W/System.err(545):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:266)
11-20 22:37:35.324: W/System.err(545):  at java.net.Socket.startupSocket(Socket.java:773)
11-20 22:37:35.324: W/System.err(545):  at java.net.Socket.tryAllAddresses(Socket.java:192)
11-20 22:37:35.324: W/System.err(545):  at java.net.Socket.<init>(Socket.java:256)
11-20 22:37:35.334: W/System.err(545):  at java.net.Socket.<init>(Socket.java:220)
11-20 22:37:35.334: W/System.err(545):  at com.example.helloandroid.Requester.run(HelloAndroid.java:33)
11-20 22:37:35.334: W/System.err(545):  at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:19)
11-20 22:37:35.334: W/System.err(545):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 22:37:35.334: W/System.err(545):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-20 22:37:35.334: W/System.err(545):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-20 22:37:35.344: W/System.err(545):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-20 22:37:35.344: W/System.err(545):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-20 22:37:35.344: W/System.err(545):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 22:37:35.344: W/System.err(545):  at android.os.Looper.loop(Looper.java:123)
11-20 22:37:35.344: W/System.err(545):  at android.app.ActivityThread.main(ActivityThread.java:4363)
11-20 22:37:35.344: W/System.err(545):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 22:37:35.344: W/System.err(545):  at java.lang.reflect.Method.invoke(Method.java:521)
11-20 22:37:35.344: W/System.err(545):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-20 22:37:35.344: W/System.err(545):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-20 22:37:35.354: W/System.err(545):  at dalvik.system.NativeStart.main(Native Method)
11-20 22:37:35.354: D/AndroidRuntime(545): Shutting down VM
11-20 22:37:35.354: W/dalvikvm(545): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-20 22:37:35.354: E/AndroidRuntime(545): Uncaught handler: thread main exiting due to uncaught exception
11-20 22:37:35.364: E/AndroidRuntime(545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.NullPointerException
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.os.Looper.loop(Looper.java:123)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.app.ActivityThread.main(ActivityThread.java:4363)
11-20 22:37:35.364: E/AndroidRuntime(545):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 22:37:35.364: E/AndroidRuntime(545):  at java.lang.reflect.Method.invoke(Method.java:521)
11-20 22:37:35.364: E/AndroidRuntime(545):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-20 22:37:35.364: E/AndroidRuntime(545):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-20 22:37:35.364: E/AndroidRuntime(545):  at dalvik.system.NativeStart.main(Native Method)
11-20 22:37:35.364: E/AndroidRuntime(545): Caused by: java.lang.NullPointerException
11-20 22:37:35.364: E/AndroidRuntime(545):  at com.example.helloandroid.Requester.run(HelloAndroid.java:60)
11-20 22:37:35.364: E/AndroidRuntime(545):  at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:19)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 22:37:35.364: E/AndroidRuntime(545):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-20 22:37:35.364: E/AndroidRuntime(545):  ... 11 more
11-20 22:37:35.384: I/dalvikvm(545): threadid=7: reacting to signal 3
11-20 22:37:35.404: I/dalvikvm(545): Wrote stack trace to '/data/anr/traces.txt'

When I run this code on an Android emulator, it says, “Sorry! The application…has stopped unexpectedly. Please try again.” Any help will be greatly appreciated! Thanks!

  • 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-26T22:37:18+00:00Added an answer on May 26, 2026 at 10:37 pm

    add this tag

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

    in your manifest.

    Its required that you add permissions to access internet before using it in your appplication

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

Sidebar

Related Questions

This is my first Android app and I've encountered an exception when trying to
This is my first Android app. I need to email what I have so
I'm about ready to market my first Android App (from a US Google Checkout/Merchant
I am trying to create my first Android app and need some help. Basically
To begin with, this is the first Android app I'm writing, and I have
I am trying to make my first Android app. I noticed that the SQLiteOpenHelper.onCreate()
I'm creating my first android app and I need to use a service. The
I have published my first android app and its quite different from iPhone. On
I've followed this tutorial: http://anandhansubbiah.com/blog/writing-your-first-android-application/ , but no matter what I do, and what
Code first: '''this is main structure of my program''' from twisted.web import http from

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.