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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:44:30+00:00 2026-06-18T00:44:30+00:00

Hayho, well my problem is a little tricky to me to explain. I try

  • 0

Hayho,

well my problem is a little tricky to me to explain. I try to connect my android tablet (4.0.3) to a RS-232 converter. That already works, but only oneway. If i try to send anything, it is sended. I cannot receive any answers, but this is neccessary. I get this

    Logcat Errors: Either Socket is not bound, or EADRESS already in use.

I tried multiple ways, but none worked as i need it.

My target: Sending AND receiving Bytes whenever they are sent.

My Architecture: MainActivity -> calls 2 Asynctasks
1. network, 2. ConcurrentServer
network has to send my commands via DataOutputStream. Allright here.
ConcurrentServer has to receive incoming Data with DataInputStream, give it to a method in ConcurrentServer class. This method should send an answerbyte.

My ConcurrentServer doesn’t receive, nor does the method send anything…

I hope you can help me with that.

Code:

package com.example.wettkampftimerbt;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

import org.apache.http.client.protocol.ClientContext;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class ConcurrentServer extends AsyncTask<Void, Void, Void>{

    private final String LFDNR1 = "lfdnr1";
    private final String LFDNR2 = "lfdnr2";
    private final String LFDNR3 = "lfdnr3";
    private final String LFDNR4 = "lfdnr4";
    private final String LFDNR5 = "lfdnr5";
    private final String LFDNR6 = "lfdnr6";
    private final String LFDNR7 = "lfdnr7";
    private final String LFDNR8 = "lfdnr8", PREFS_NAME = "prefs",
            COMMAND = "command", LANE = "lane", DINYMIN ="dinymin", DINYSEK="dinysek";

    int w1, w2, w3, w4, w5, w6, w7, w8, diny1, diny2, diny3, diny4, diny5,
            diny6, diny7, diny8, dinymin, dinysek;
    Context context;
    ServerSocket sock;
    Socket ss;



        protected Void doInBackground (Void... arg0) {
            System.out.println("ConcurrentServer_doInBackgorund");
            Log.e("SERVER", "CON");
        try {
            sock = new ServerSocket(2010);
            ss = sock.accept();
            sock.setReuseAddress(true);
            sock.bind(new InetSocketAddress(2010));

            ss.setReuseAddress(true);
            ss = new Socket("192.168.0.100", 2011);
            sock.setSoTimeout(1000);
            for (;;) {
                boolean stopData = true;    
                BufferedReader din = new BufferedReader(new
                        InputStreamReader(ss.getInputStream()));


                //DataInputStream din = new DataInputStream(
                    //  ss.getInputStream());
                while (stopData) {
                // Einlesen
                diny1 = (din).read();// 03
                diny2 = (din).read();// Empfänger(90...97)
                diny3 = (din).read();// Sender(80)
                diny4 = (din).read();// lfdnr
                diny5 = (din).read();// Kommando(Start, Stop...)
                if (diny5==79){
                dinymin = (din).read();//Minuten
                dinysek = (din).read();//Sekunden
                }
                diny6 = (din).read();// Prüfsumme
                diny7 = (din).read();// 05
                din.close();
                //ss.close();
                //sock.close();
                System.out
                        .println("diny: " + diny1 + " " + diny2 + " " + diny3
                                + " " + diny4 + " " + diny5 + " " + diny6 + " "
                                + diny7);
                Log.e("Reccom", "RECCOM");
                reccom();


                stopData=false;
                }
            }
        } catch (Exception e) {
            System.out.println("IO error" + e);}
        return null;
        }



    public int getLFDNR1(Context context) {
        System.out.println("ConcurrentServer_getlfdnr1");
        w1 = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getInt(LFDNR1, 00);
        //w1 = Integer.valueOf(LFDNR1);
        return w1;
    }

    public String getLFDNR2(Context context) {
        System.out.println("ConcurrentServer_getlfdnr2");
        w2 = Integer.valueOf(LFDNR2);
        return LFDNR2;
    }

    public String getLFDNR3(Context context) {
        System.out.println("ConcurrentServer_getlfdnr3");
        w3 = Integer.valueOf(LFDNR3);
        return LFDNR3;
    }

    public String getLFDNR4(Context context) {
        System.out.println("ConcurrentServer_getlfdnr4");
        w4 = Integer.valueOf(LFDNR4);
        return LFDNR4;
    }

    public String getLFDNR5(Context context) {
        System.out.println("ConcurrentServer_getlfdnr5");
        w5 = Integer.valueOf(LFDNR5);
        return LFDNR5;
    }

    public String getLFDNR6(Context context) {
        System.out.println("ConcurrentServer_getlfdnr6");
        w6 = Integer.valueOf(LFDNR6);
        return LFDNR6;
    }

    public String getLFDNR7(Context context) {
        System.out.println("ConcurrentServer_getlfdnr7");
        w7 = Integer.valueOf(LFDNR7);
        return LFDNR7;
    }

    public String getLFDNR8(Context context) {
        System.out.println("ConcurrentServer_getlfdnr8");
        w8 = Integer.valueOf(LFDNR8);
        return LFDNR8;
    }
    private void reccom() throws IOException {
        System.out.println("ConcurrentServer_reccom");
        if (diny1 == 3 && diny7 == 5) {
            answer();
        } else {
        }

        switch (diny2) {
        case (144): // 144=90=Bahn1
            c144(context);
            break;
        case (145): // 145=91=Bahn2
            c145(context);
            break;
        case (146): // 146=92=Bahn3
            c146(context);
            break;
        case (147): // 147=93=Bahn4
            c147(context);
            break;
        case (148): // 148=94=Bahn5
            c148(context);
            break;
        case (149): // 149=95=Bahn6
            c149(context);
            break;
        case (150): // 150=96=Bahn7
            c150(context);
            break;
        case (151): // 151=97=Bahn8
            c151(context);
            break;
        }

    }

    private void answer() throws IOException {
        System.out.println("ConcurrentServer_answer");
    Socket sock ;
    int w0=0,w5=32;
         SocketAddress sockaddr = new InetSocketAddress("192.168.0.100", 2011);  
         sock = new Socket();  
         int timeout = 1000;   // 1000 millis = 1 second  
         sock.connect(sockaddr, timeout); 
        boolean stopData = true;
        DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
        while (stopData) {
            dos.writeByte(w0);
            dos.writeByte(diny1);
            dos.writeByte(diny3);
            dos.writeByte(diny2);
            dos.writeByte(diny4);
            dos.writeByte(w5);
            dos.writeByte(diny6);
            dos.writeByte(diny7);
            dos.flush();
            //dos.close();
            stopData = false;}

    }

    private void c151(Context context) {
        System.out.println("ConcurrentServer_c151");
        getLFDNR8(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {

                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "151").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println("lfdnr8 passt nicht");
        }
    }

    private void c150(Context context) {
        System.out.println("ConcurrentServer_c150");
        getLFDNR7(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "150").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println("lfdnr7 passt nicht");
        }
    }

    private void c149(Context context) {
        System.out.println("ConcurrentServer_c149");
        getLFDNR6(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "149").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println("lfdnr6 passt nicht");
        }
    }

    private void c148(Context context) {
        System.out.println("ConcurrentServer_c148");
        getLFDNR5(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "148").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println("lfdnr5 passt nicht");
        }
    }

    private void c147(Context context) {
        System.out.println("ConcurrentServer_c147");
        getLFDNR4(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "147").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println("lfdnr4 passt nicht");
        }
    }

    private void c146(Context context) {
        System.out.println("ConcurrentServer_c146");
        getLFDNR3(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "146").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println("lfdnr3 passt nicht");
        }
    }

    private void c145(Context context) {
        System.out.println("ConcurrentServer_c145");
        getLFDNR2(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "145").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println("lfdnr2 passt nicht");
        }
    }

    private void c144(Context context) {
        System.out.println("ConcurrentServer_c144");
        final String PREFS_NAME = "prefs";
        final String COMMAND = "command";
        final String LANE = "lane", DINYMIN ="dinymin", DINYSEK="dinysek";
        getLFDNR1(context);
        if (w1 == diny4||(w1-1)==diny4)
            switch (diny5) {
            case (00):
            case (79): {
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(LANE, "144").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                        .edit().putString(COMMAND, "79").commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYMIN, Integer.valueOf(dinymin).toString()).commit();
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
                .edit().putString(DINYSEK, Integer.valueOf(dinysek).toString()).commit();
            }
                break;
            }
        else {
            System.out.println(w1+ "+"+ diny4);
            System.out.println("lfdnr1 passt nicht");
        }

    }

    public ConcurrentServer(Context context) {
        System.out.println("ConcurrentServer_Context");
        this.context = context;

    }



}

If you think, you should need the network-code, too, please tell it to me.
BTW: this is my code NOW, after experimenting and trying a lot of things. So if anything is doubled, this is the reason. In Cause of I am a newbie on Android it may happens to me.

Thanks.

EDIT:

This link, given by Class Stacker is very good. It`s in detailed and precise. For anyone who’s having trouble, check this:

http://docs.oracle.com/javase/tutorial/networking/urls/index.html

Thanks one more time Class Stacker!

  • 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-18T00:44:31+00:00Added an answer on June 18, 2026 at 12:44 am
    1. Please note that, depending on your Android version, AsyncTasks are not executed in parallel, unless you explicitly specify the executor for that.

    2. I guess you’ll have to actively connect to the converter device. If so, you don’t have a server socket; it’s really a client socket.

    3. I have no idea why you create so many sockets along the way; a socket is a bi-directional communication channel.

    4. If you really need a server socket, you wait vor incoming connections, which you accept(), and accept() will give you your client-specific socket.

    Hote this helps; if not, you need to update your questions with information about the device you’re connecting to, as far as I’m concerned.

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

Sidebar

Related Questions

heyho, i stuck in the following problem: i try to prevent that a user
Heyho! I've a little app running as LSUIElement with a NSStatusItem . Sometimes the
Heyho, I'm trying to develop a cross-platform app, that should manage different SQL data.
Im an Android noob, coming from objC, I have a very stupid question (project
Heyho, I'm building a custom green back button which is being created in the
Heyho, I need to grab some datas from actions which been done from date
Heyho, I'm trying to set a Procedure like: Create or Replace Procedure MyProcedure1( x
Heyho, I've gotta write a Procedure which Inserts a resultset from a select-statement into
I am translating some code from attoparsec to Parsec, because the parser needs to
So I have some Haskell code in which I'd like to work with Data.Set

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.