English 中文(简体)
单列“待补”
原标题:Android Socket Exception "socket is closed"

当我尝试进行由对应服务器和星体客户组成的测试时,我总是会发现例外的“袖珍”是封闭的。 该守则只能向服务器发送ms,从服务器中接收ms,但如果你同时要这样做,它只是不工作。 我非常奇怪的是,它为什么会导致这种问题,如果我想要它能够首先寄送回服务器,我如何确定这一问题。

and then receive msg from echo server?

            // Server IP address 
            InetAddress serverIp;

            // try to connect Server
            try {

                // set up server IP address
                serverIp = InetAddress.getByName("192.168.17.1");

                // set up port
                int serverPort=12345;

                // initiate socket connection
                Socket clientSocket=new Socket(serverIp,serverPort);

                BufferedOutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
                out.write("Send From Android1111, stitch ".getBytes());
                out.flush();

                //wait to receive Server s msg 
                BufferedReader  br =new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

                total.toString();*/
            // Display received msg with Toast
                Toast.makeText(getApplicationContext(), br.readLine(), Toast.LENGTH_SHORT ).show();

            //close connection
                clientSocket.close();             

//              out.close();
//              out = null;
            } catch (IOException e) {
                // display exception with Toast
                Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
            }

遗憾的是,它依然没有工作。

            // set up Server IP address 
            serverIp = InetAddress.getByName("192.168.2.2");

            // set up Server port
            int serverPort=12345;

            // initiate socket connection
            Socket clientSocket=new Socket(serverIp,serverPort);

                // open input and output stream
            OutputStream out = clientSocket.getOutputStream();
            InputStream in = clientSocket.getInputStream();

            //send msg
            out.write("Send From Android1111, bitch ".getBytes());


                // receive msg from server
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            String rMsg = new String(buffer);
            Toast.makeText(getApplicationContext(), rMsg, Toast.LENGTH_LONG ).show();

            //close input and output stream
            in.close();
            out.close();

             //關閉連線
       clientSocket.close(); 
        } catch (IOException e) {
            // 出錯後顯示錯誤訊息Toast
            Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
        }

for helper s convenience, here s the python written code for server part:

# Practice Echo Server Program written in Python
import socket

# host =    means it binds to any available interface
host =   
port = 12345

# socket() function returns a socket object whose methods implement the various socket system calls.
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

# Bind the socket to address. 
s.bind((host,port))

# Listen for connections made to the socket. The backlog argument specifies 
# the maximum number of queued connections and should be at least 0; 
# the maximum value is system-dependent (usually 5), the minimum value is forced to 0.
s.listen(5)

# Accept a connection. The socket must be bound to an address and listening for 
# connections. The return value is a pair (conn, address) where conn is a new socket 
# object usable to send and receive data on the connection, and address is the address
# bound to the socket on the other end of the connection.
conn, addr = s.accept()
print  Connected by , addr

# Receive data from the socket. The return value is a string representing the data received.
# The maximum amount of data to be received at once is specified by bufsize. See the Unix
# manual page recv(2) for the meaning of the optional argument flags; it defaults to zero.
# Note For best match with hardware and network realities, the value of bufsize should be 
# a relatively small power of 2, for example, 4096.

while 1:
    data = conn.recv(1024)
    if not data: break 
    print  received data is :  , repr(data)
    conn.send(data)

conn.close()
最佳回答
问题回答

Your application needs INTERNET permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>




相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签