Logo Questions Linux Laravel Mysql Ubuntu Git Menu

android tcp/ip socket won't send data to computer

i tried to make an application that send data from android to computer (java to vb.net). The data sending is working in eclipse java project, but not in android project . But i have a problem. no incoming data to visual basic in my computer. VB Code :

 Imports System.Net.Sockets
 Imports System.Threading
 Imports System.IO

Public Class Form1

Dim Listener As New TcpListener(65535)
Dim Client As New TcpClient
Dim Message As String = ""

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
End Sub

Private Sub Listening()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Client = New TcpClient("", 65535)

    Dim Writer As New StreamWriter(Client.GetStream())
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If Listener.Pending = True Then
        Message = ""
        Client = Listener.AcceptTcpClient()

        Dim Reader As New StreamReader(Client.GetStream())
        While Reader.Peek > -1
            Message = Message + Convert.ToChar(Reader.Read()).ToString
        End While

        MsgBox(Message, MsgBoxStyle.OkOnly)
    End If
 End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As     System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End Sub

End Class

java code :

package com.example.androidclient2;

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

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView serverMessage;
Thread m_objThreadClient;
Socket clientSocket;
public void onCreate(Bundle savedInstanceState) {
public void Start(View view)
m_objThreadClient=new Thread(new Runnable() {
      public void run()
             clientSocket= new Socket("",65535);
             ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
             oos.writeObject("Hellow there");
             Message serverMessage= Message.obtain();
             ObjectInputStream ois =new     ObjectInputStream(clientSocket.getInputStream());
             String strMessage = (String)ois.readObject();
           catch (Exception e) 
                // TODO Auto-generated catch block



Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
public void messageDisplay(String servermessage)


And Thanks For The help .

like image 653
fares Avatar asked Mar 21 '23 13:03


1 Answers

Have just created and tested this class on android today:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class TcpClient {

private static final String TAG = TcpClient.class.getSimpleName();

private Socket socket;
private PrintWriter out;
private boolean connected;

public TcpClient()
    socket = null;
    out = null;
    connected = false;

public void connect(Context context, String host, int port)
    new ConnectTask(context).execute(host, String.valueOf(port));

private class ConnectTask extends AsyncTask<String, Void, Void> {

    private Context context;

    public ConnectTask(Context context) {
        this.context = context;

    protected void onPreExecute() {
        showToast(context, "Connecting..");

    protected void onPostExecute(Void result) {
        if (connected) {
            showToast(context, "Connection successfull");

    private String host;
    private int port;

    protected Void doInBackground(String... params) {
        try {
            String host = params[0];
            int port = Integer.parseInt(params[1]);
            socket = new Socket(host, port);
            out = new PrintWriter(socket.getOutputStream(), true);
        } catch (UnknownHostException e) {
            showToast(context, "Don't know about host: " + host + ":" + port);
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            showToast(context, "Couldn't get I/O for the connection to: " + host + ":" + port);
            Log.e(TAG, e.getMessage());
        connected = true;
        return null;


public void disconnect(Context context)
    if ( connected )
        try {
            connected = false;
        } catch (IOException e) {
            showToast(context, "Couldn't get I/O for the connection");
            Log.e(TAG, e.getMessage());

 * Send command to a Pure Data audio engine. 
public void send(String command)
    if ( connected ) out.println(command +";");   

private void showToast(final Context context, final String message) {
    new Handler(context.getMainLooper()).post(new Runnable() {

        public void run() {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();

It might help you even though I see no obvious error in your code.

Is the device on the same WiFi network as your computer? It won't work via USB for instance.

like image 56
cYrixmorten Avatar answered Apr 01 '23 21:04