Simon Erasmus
Simon Erasmus

Reputation: 134

TCP from android to PC not working using java

I am trying to send a simple message over TCP from my android phone (using java application) to my computer. I have an socket that is listening on my computer but as soon as I run this app, it crashes. I am really new to Android developing so please bear with me...

Here is my Java code:

package com.scorekeep.clienttcp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

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


public class MainActivity extends Activity {

    EditText textOut;


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

        Socket socket = null;
        try {
            socket = new Socket("10.0.0.10",5000);
            DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
            DOS.writeUTF("HELLO_WORLD");
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

Views: 129

Answers (3)

user6657161
user6657161

Reputation: 361

Two changes in code. public static void sendmsg and public public static PrintWriter out at begining of AsyncTask class

Upvotes: 1

user6657161
user6657161

Reputation: 361

As requested an extended example :

import android.app.*;
import android.os.*;
import android.util.*;
import java.io.*;
import java.net.*;

public class MainActivity extends Activity 
{
    @Override
     protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new connection().execute();
    }
}

 class connection extends AsyncTask<String,String,String> {
public static PrintWriter out;
BufferedReader in;
public static boolean running = true;
    @Override
    protected String doInBackground(String... message) {

        try
        {
            InetAddress serverAddr = InetAddress.getByName("localhost");
            Socket socket = new Socket(serverAddr, 8008);
            // send the message to the server
            out = new PrintWriter(new BufferedWriter(
                              new OutputStreamWriter(socket.getOutputStream())), true);

            in = new BufferedReader(new InputStreamReader(
                                    socket.getInputStream()));

        while(running) {
            String msgfromserver = in.readLine();
        }
        }
        catch (Exception e)
        {}

        return null;
    }

    public static void sendmsg(String msg){
        if(out!=null){
            out.println(msg);
            out.flush();
        }
    }
}

Usage: Call connection.sendmsg("some text"); from OnClick method of button

And set connection.running = false; onbackpress. (Or before finishing activity)

Upvotes: 1

user6657161
user6657161

Reputation: 361

You cannot execute background tasks like socket connection in ui thread. You should use AsyncTask.

Example

import android.app.*;
import android.os.*;
import android.util.*;
import java.io.*;
import java.net.*;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new connection().execute();
    }
}

class connection extends AsyncTask<String,String,String> {

    @Override
    protected String doInBackground(String... message) {

        try
        {
            InetAddress serverAddr = InetAddress.getByName("ip here");
            Socket socket = new Socket(serverAddr, 5000); //port here
    // send the message to the server
        PrintWriter out = new PrintWriter(new BufferedWriter(
                              new OutputStreamWriter(socket.getOutputStream())), true);
        out.println("hi");
        out.flush(); //optional
        socket.close();
        }
        catch (Exception e)
        {}

        return null;
    }
}

Note Untested. May contain errors.

Upvotes: 1

Related Questions