Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:套接字已关闭_Android_Socketexception - Fatal编程技术网

Android:套接字已关闭

Android:套接字已关闭,android,socketexception,Android,Socketexception,PCClient: public class PCServer { /** * @param args */ static Socket socket = null; private static String ip = "192.168.42.129"; private static int port = 18181; public static void main(String[] args) { // TODO Auto-generated method stub

PCClient:

public class PCServer
{

/**
 * @param args
 */
static Socket socket = null;
private static String ip = "192.168.42.129";
private static int port = 18181;

public static void main(String[] args)
{
    // TODO Auto-generated method stub
    try
    {
        socket = new Socket(ip, port);
    } catch (UnknownHostException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try
    {
        SendMsg(ip, port, "BZT");
        GetMessage getMessage = new GetMessage();
        getMessage.start();
    } catch (UnknownHostException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void SendMsg(String ip, int port, String msg) throws UnknownHostException, IOException
{
    try
    {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(msg);
        writer.flush();
        writer.close();
        //socket.close();
    } catch (UnknownHostException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

static class GetMessage extends Thread
{
    @Override
    public void run()
    {
        // TODO Auto-generated method stub
        super.run();
        try
        {
            while (true)
            {
                System.out.println("--------------------------------------------------------");
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String str = in.readLine();
                System.out.println(str);
                //client.close();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
  }
}
AndroidServer:Service

public class SocketService extends Service
{
public static final int SERVERPORT = 18181;
ServerSocket serverSocket;
@Override
public IBinder onBind(Intent arg0)
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    // TODO Auto-generated method stub
    try
    {
        serverSocket = new ServerSocket(SERVERPORT);
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    GetSocketMessage getSocketMessage = new GetSocketMessage();
    getSocketMessage.start();
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy()
{
    // TODO Auto-generated method stub
    super.onDestroy();
    try
    {
        this.serverSocket.close();
    } 
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

class GetSocketMessage extends Thread
{
    @Override
    public void run()
    {
        // TODO Auto-generated method stub
        super.run();
        try
        {
            Log.i("s", "S: Connecting...");
            while (true)
            {
                Socket client = serverSocket.accept();
                Log.i("s", "S: Receiving...");
                try
                {
                    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    String str = in.readLine();
                    Log.i("s", "S: Received: '" + str + "'");

                    if ("BZT".equals(str))
                    {
                        Intent intent =  new Intent("com.StarHope.ZWGKXT.connected");
                        sendBroadcast(intent);
                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                        writer.write("OK");
                        writer.flush();
                        writer.close();
                    }
                    if ( Lbjy_usbActivity.content !=null )
                    {
                        Log.i("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "oooooooooooooooooooooooooooooo");
                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                        writer.write("");
                        writer.flush();
                        writer.close();
                    }
                } 
                catch (Exception e)
                {
                    Log.i("s", "S: Error");
                    e.printStackTrace();
                } 
                finally
                {
                    Log.i("s", "S: Done");
                }
            }
        } 
        catch (Exception e)
        {
            Log.i("s", "S: Error");
            e.printStackTrace();
        }
    }
  }
}
android手机服务器可以接收PCClient“BZT”发送的消息, 但当手机向PC发送“OK”时,总有一个

Exception:java.net.SocketException: Socket is closed.

如何修复此问题?

关闭使用
getOutputStream()
getInputStream()
获得的任何流将关闭套接字

你在“BZT”之前收到什么东西了吗?如果是,则在发送空答案后,套接字将关闭


您应该只使用一个输出流,并且
flush()
不关闭每条消息。

关闭使用
getOutputStream()
getInputStream()获得的任何流将关闭套接字

你在“BZT”之前收到什么东西了吗?如果是,则在发送空答案后,套接字将关闭

您应该只使用一个输出流,并且不关闭每条消息