Android中服务器 - 客户端故障(套接字没有绑定/插座已经被使用) [英] Server-Client malfunction in Android (socket is not bound/socket is already in use)

查看:179
本文介绍了Android中服务器 - 客户端故障(套接字没有绑定/插座已经被使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hayho,

以及我的问题是有点棘手给我解释一下。我尝试我的Andr​​oid平板电脑(4.0.3)连接到RS-232转换器。已经工作,但只是单向。如果我尝试发送任何东西,它是sended。我不能接受任何答案,但这个时,只需要。我得到这个

  logcat的错误:要么套接字未使用的约束,或EADRESS了。

我试过多种方法,但没有当过,我需要它。

我的目标:发送和接收的字节时,他们被送到。

我的架构: MainActivity - >调用2 Asynctasks
1.网络2. ConcurrentServer
网络上有通过DataOutputStream类送我的命令。这里还好吧。
ConcurrentServer已接收到的数据用DataInputStream所,给它在ConcurrentServer类中的方法。此方法应该发送answerbyte。

我的ConcurrentServer没有收到,也没有方法发送任何...

我希望你能帮助我这一点。

code:

 包com.example.wettkampftimerbt;进口java.io.BufferedReader中;
进口java.io.DataInputStream中;
进口java.io.DataOutputStream中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.net.InetSocketAddress;
进口java.net.ServerSocket的;
进口的java.net.Socket;
进口java.net.SocketAddress;进口org.apache.http.client.protocol.ClientContext;进口android.content.Context;
进口android.os.AsyncTask;
进口android.util.Log;公共类ConcurrentServer扩展的AsyncTask<太虚,太虚,太虚> {    私人最终字符串LFDNR1 =lfdnr1;
    私人最终字符串LFDNR2 =lfdnr2;
    私人最终字符串LFDNR3 =lfdnr3;
    私人最终字符串LFDNR4 =lfdnr4;
    私人最终字符串LFDNR5 =lfdnr5;
    私人最终字符串LFDNR6 =lfdnr6;
    私人最终字符串LFDNR7 =lfdnr7;
    私人最终字符串LFDNR8 =lfdnr8,preFS_NAME =preFS
            COMMAND =命令,LANE =巷,DINYMIN =dinymin,DINYSEK =dinysek;    INT W1,W2,W3,W4,W5,W6,W7,W8,diny1,diny2,diny3,diny4,diny5,
            diny6,diny7,diny8,dinymin,dinysek;
    上下文语境;
    ServerSocket的袜子;
    插座SS;        保护无效doInBackground(虚空......为arg0){
            的System.out.println(ConcurrentServer_doInBackgorund);
            Log.e(SERVER,CON);
        尝试{
            袜子=新的ServerSocket(2010);
            SS = sock.accept();
            sock.setReuseAddress(真);
            sock.bind(新的InetSocketAddress(2010));            ss.setReuseAddress(真);
            SS =新的Socket(192.168.0.100,2011年);
            sock.setSoTimeout(1000);
            为(;;){
                布尔stopData =真;
                喧嚣的BufferedReader =新的BufferedReader(新
                        InputStreamReader的(ss.getInputStream()));
                // DataInputStream以DIN =新DataInputStream所(
                    // ss.getInputStream());
                而(stopData){
                // Einlesen
                diny1 =(DIN).read(); // 03
                diny2 =(DIN).read(); //Empfänger(90 ... 97)
                diny3 =(DIN).read(); //发送者(80)
                diny4 =(DIN).read(); // lfdnr
                diny5 =(DIN).read(); //突击队(启动,停止...)
                如果(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;
                }
            }
        }赶上(例外五){
            的System.out.println(IO错误+ E);}
        返回null;
        }    公众诠释getLFDNR1(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr1);
        W1 = context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE).getInt(LFDNR1,00);
        // W1 = Integer.valueOf(LFDNR1);
        返回W1;
    }    公共字符串getLFDNR2(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr2);
        W2 = Integer.valueOf(LFDNR2);
        返回LFDNR2;
    }    公共字符串getLFDNR3(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr3);
        W3 = Integer.valueOf(LFDNR3);
        返回LFDNR3;
    }    公共字符串getLFDNR4(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr4);
        W4 = Integer.valueOf(LFDNR4);
        返回LFDNR4;
    }    公共字符串getLFDNR5(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr5);
        W5 = Integer.valueOf(LFDNR5);
        返回LFDNR5;
    }    公共字符串getLFDNR6(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr6);
        W6 = Integer.valueOf(LFDNR6);
        返回LFDNR6;
    }    公共字符串getLFDNR7(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr7);
        W7 = Integer.valueOf(LFDNR7);
        返回LFDNR7;
    }    公共字符串getLFDNR8(上下文的背景下){
        的System.out.println(ConcurrentServer_getlfdnr8);
        W8 = Integer.valueOf(LFDNR8);
        返回LFDNR8;
    }
    私人无效reccom()抛出IOException
        的System.out.println(ConcurrentServer_reccom);
        如果(diny1 == 3和;&放大器; diny7 == 5){
            回答();
        }其他{
        }        开关(diny2){
        案例(144):// 144 = 90 = Bahn1
            C144(上下文);
            打破;
        案例(145):// 145 = 91 = Bahn2
            C145(上下文);
            打破;
        案例(146):// 146 = 92 = Bahn3
            C146(上下文);
            打破;
        案例(147):// 147 = 93 = Bahn4
            C147(上下文);
            打破;
        案例(148):// 148 = 94 = Bahn5
            C148(上下文);
            打破;
        案例(149):// 149 = 95 = Bahn6
            C149(上下文);
            打破;
        案例(150):// 150 = 96 = Bahn7
            C150(上下文);
            打破;
        案例(151):// 151 = 97 = Bahn8
            C151(上下文);
            打破;
        }    }    私人无效的答案()抛出IOException
        的System.out.println(ConcurrentServer_answer);
    插座袜子;
    INT W0 = 0,W5 = 32;
         SocketAddress的SOCKADDR =新的InetSocketAddress(192.168.0.100,2011年);
         袜子=新的Socket();
         INT超时= 1000; // 1000米利斯=1秒
         sock.connect(套接字地址,超时);
        布尔stopData =真;
        DataOutputStream类DOS =新的DataOutputStream类(sock.getOutputStream());
        而(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;}    }    私人无效C151(上下文的背景下){
        的System.out.println(ConcurrentServer_c151);
        getLFDNR8(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(LANE,151)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(lfdnr8 passt nicht);
        }
    }    私人无效C150(上下文的背景下){
        的System.out.println(ConcurrentServer_c150);
        getLFDNR7(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(泳道,150)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(lfdnr7 passt nicht);
        }
    }    私人无效C149(上下文的背景下){
        的System.out.println(ConcurrentServer_c149);
        getLFDNR6(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(LANE,149)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(lfdnr6 passt nicht);
        }
    }    私人无效C148(上下文的背景下){
        的System.out.println(ConcurrentServer_c148);
        getLFDNR5(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(LANE,148)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(lfdnr5 passt nicht);
        }
    }    私人无效C147(上下文的背景下){
        的System.out.println(ConcurrentServer_c147);
        getLFDNR4(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(LANE,147)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(lfdnr4 passt nicht);
        }
    }    私人无效C146(上下文的背景下){
        的System.out.println(ConcurrentServer_c146);
        getLFDNR3(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(LANE,146)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(lfdnr3 passt nicht);
        }
    }    私人无效C145(上下文的背景下){
        的System.out.println(ConcurrentServer_c145);
        getLFDNR2(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(LANE,145)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(lfdnr2 passt nicht);
        }
    }    私人无效C144(上下文的背景下){
        的System.out.println(ConcurrentServer_c144);
        最后弦乐preFS_NAME =preFS
        最终字符串命令=命令;
        最后弦乐LANE =巷,DINYMIN =dinymin,DINYSEK =dinysek;
        getLFDNR1(上下文);
        如果(W1 == || diny4(W1-1)== diny4)
            开关(diny5){
            壳体(00):
            案例(79):{
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(LANE,144)提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                        。.edit()putString(指挥,79)提交();
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                .edit()putString(DINYMIN,Integer.valueOf(dinymin)的ToString())提交()。
                context.getShared preferences(preFS_NAME,Context.MODE_PRIVATE)
                。.edit()putString(DINYSEK,Integer.valueOf(dinysek)的ToString())提交();
            }
                打破;
            }
        其他{
            的System.out.println(W1 +++ diny4);
            的System.out.println(lfdnr1 passt nicht);
        }    }    公共ConcurrentServer(上下文的背景下){
        的System.out.println(ConcurrentServer_Context);
        this.context =背景;    }}

如果你想,你可能需要的网络 - code,也请件事告诉我。
BTW:这是我的code现在,实验和尝试了很多事情之后。所以,如果有什么增加一倍,这是什么原因。在我的原因是在Android新手它可能发生在我身上。

感谢。

编辑:

这环节,由堆垛类给出非常好。它`在详细和precise。对于谁是有困难的人,看看这个:

http://docs.oracle.com/javase/tutorial/网络/网址/ index.html的

由于一次级堆!


解决方案

  1. 请注意,根据您的Andr​​oid版本,AsyncTasks不是并行执行的,除非你明确指定为遗嘱执行人。


  2. 我想你必须积极地连接到转换器装置。如果是这样,你没有一台服务器插座;这真是一个客户端套接字。


  3. 我不知道你为什么创建沿途那么多的插座;套接字是一个双向的沟通渠道。


  4. 如果的你真的需要一个服务器套接字,你等着VOR传入连接,您接受()接受()会给你你的客户端特定的插座上。


Hote酒店这可以帮助;如果没有,你需要你要连接的设备的信息来更新您的问题,据我而言。

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. 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.

这篇关于Android中服务器 - 客户端故障(套接字没有绑定/插座已经被使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆