Android Socket Error Handling
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta
Python Catch Socket Error
Discuss the workings and policies of this site About Us Learn more java.net.socketexception: android about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack java socket Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping https://developer.android.com/reference/java/net/SocketException.html each other. Join them; it only takes a minute: Sign up Android Socket Connection fail up vote 0 down vote favorite 1 I am trying to establish a connection between Android Client and C# Server .. My server is working fine except the android client is not working even after adding internet connection permission Here is the http://stackoverflow.com/questions/13390026/android-socket-connection-fail code : private void connectSocket(String a){ try { InetAddress serverAddr = InetAddress.getByName("192.168.0.2"); Log.d("TCP", "C: Connecting...."); Socket socket = new Socket(serverAddr,4444); Log.d("TCP", "C: I dunno ..."); //String message = "1"; PrintWriter out = null; BufferedReader in = null; try { // Log.d("TCP", "C: Sending: '" + message + "'"); out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //out.println(message); while ((in.readLine()) != null) { txt.append(in.readLine()); } Log.d("TCP", "C: Sent."); Log.d("TCP", "C: Done."); } catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { socket.close(); } } catch (UnknownHostException e) { // TODO Auto-generated catch block Log.e("TCP", "C: UnknownHostException", e); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block Log.e("TCP", "C: IOException", e); e.printStackTrace(); } } And here is the Debugging Log: 11-15 02:41:58.040: W/dalvikvm(26839): threadid=1: thread exiting with uncaught exception (group=0x41c352a0) 11-15 02:41:58.075: E/AndroidRuntime(26839): FATAL EXCEPTION: main 11-15 02:41:58.075: E/AndroidRuntime(26839): android.os.NetworkOnMainThreadException 11-15 02:41:58.075: E/AndroidRuntime(26839): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) 11-15 02:41:58.075: E/AndroidRuntime(26839): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 11-15 02:41:58.075: E/AndroidRuntime(26839): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 11-15 02:41:58.075: E/AndroidRuntime(26839): at libcore.io.IoBridge.connect(IoBridge.java:112) 11-15 02:41:58.075: E/AndroidRuntime(26839): at java.net
Learning soars 7 habits of highly effective developers 10 hard-core coding tips for faster Python Beyond jQuery: An expert guide to choosing the right http://www.javaworld.com/javaworld/jw-09-1999/jw-09-timeout.html JavaScript framework More Insider Sign Out Search for Suggestions for you Insider email Core Java All Core Java Agile Development Java Concurrency Java Language Java Platform Java Security Programming Careers Testing and Debugging Enterprise Java All Enterprise Java Big Data Cloud Computing Data Analytics Development Tools Java APIs Java App Dev Java Web Development Open Source Scripting and JVM Languages Learn error handling Java All Learn Java Design Patterns Mobile Java All Mobile Java Java Android Developers Java iOS Developers News Features How-Tos Blogs Resources/White Papers Newsletters × Close Home Core Java News Simple handling of network timeouts More like this Master Merlin's new I/O classes Programming Java threads in the real world, Part 9 Programming Java threads in the real world, Part socket error handling 9 Learn just how easy it is to prevent stalled clients and servers Email a friend To Use commas to separate multiple email addresses From Privacy Policy Thank you Your message has been sent. Sorry There was an error emailing this page. Comments By David Reilly JavaWorld | Sep 1, 1999 1:00 AM PT RELATED TOPICS Core Java Comments Many programmers dread the thought of handling network timeouts. A common fear is that a simple, single-threaded network client without timeout support will balloon into a complex multithreaded nightmare, with separate threads needed to detect network timeouts, and some form of notification process at work between the blocked thread and the main application. While this is one option for developers, it is not the only one. Dealing with network timeouts need not be a difficult task, and in many cases you can completely avoid writing code for additional threads.When working with network connections, or any type of I/O device, there are