in Education by
I have troubles with socket connection of PC (simple server written on Java) and android emulator. Connection is established, server sends data but when I try to read it on android it always reads a null-string. Here is some parts of my code: Server: serverSocket = new ServerSocket(8888); socket = serverSocket.accept(); PrintWriter output = new PrintWriter(socket.getOutputStream(), true); output.write("Output string"); socket.close(); Client: socket = new Socket("10.0.2.2", 8888); input = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s = input.readLine(); Log.i(TAG, s); socket.close(); I've omitted try-catches and logs for clearness. According to logs, connection is established, server sends data, but client receives only null-strings. I'd appreciate any help. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This works for me... (This code only for write and read data from socket for both server and client) Server: BufferedOutputStream bos = new BufferedOutputStream(connection. getOutputStream()); /** Instantiate an OutputStreamWriter object with the optional character * encoding. */ OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII"); String process = "Calling the Socket Server on "+ host + " port " + port; /** Write across the socket connection and flush the buffer */ osw.write(process); osw.flush(); Client: /** Instantiate a BufferedInputStream object for reading /** Instantiate a BufferedInputStream object for reading * incoming socket streams. */ BufferedInputStream bis = new BufferedInputStream(connection. getInputStream()); /**Instantiate an InputStreamReader with the optional * character encoding. */ InputStreamReader isr = new InputStreamReader(bis, "US-ASCII"); /**Read the socket's InputStream and append to a StringBuffer */ int c; while ( (c = isr.read()) != 13) instr.append( (char) c); /** Close the socket connection. */ connection.close(); System.out.println(instr); } catch (IOException f) { System.out.println("IOException: " + f); } catch (Exception g) { System.out.println("Exception: " + g); } Hope this will help you..

Related questions

0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have troubles with socket connection of PC (simple server written on Java) and android emulator. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Anyone got this problem before? In Windows 7 x86, I created an avd which the target is Android 2. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I tried to find out how to use firestore local emulator for python and for testing purpose. But I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection is established and communication is possible? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    I'm trying to create Server-client connection using sockets. Server is just an Echo-server. I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I've tried to test socket connection in Java, but failed. Here is my code (two simple applications, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I've tried to test socket connection in Java, but failed. Here is my code (two simple applications, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection has been closed or could not be opened? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection is going through the closing handshake? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection has not yet been established? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    ____________ is a tiny script that if uploaded to a web server can give hacker complete control of a remote ... for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    Which connection set up between our computer and ISP (Internet Service Provider) server using a modem? Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    The idea is to create server in master process, and handle requests in workers. I want to utilize all ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
...