I wrote simple code for client server sockets as follows
MyServer.java
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class MyServer { public static void main(String[] args){ ServerSocket serverSocket = null; Socket socket = null; DataInputStream dataInputStream = null; DataOutputStream dataOutputStream = null; try { serverSocket = new ServerSocket(8888); System.out.println("Listening :8888"); } catch (IOException e) {
The android client is as follows
MyClientActivity.java
package com.exercise.AndroidClient; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.os.Bundle; public class MyClientActivity extends Activity { EditText textOut; TextView textIn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textOut = (EditText)findViewById(R.id.textout); Button buttonSend = (Button)findViewById(R.id.send); textIn = (TextView)findViewById(R.id.textin); buttonSend.setOnClickListener(buttonSendOnClickListener); } Button.OnClickListener buttonSendOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0) {
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/textout" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Send" /> <TextView android:id="@+id/textin" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.exercise.AndroidClient" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.INTERNET"/> <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MyClientActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
I get FatalException on client.pls, offering me how to handle this. I tried my IP address and 127.0.0.1, so I have any problems with the ip address or something else, because FatalException pls helps me.
logcat is as follows
02-21 21:05:48.793: D/gralloc_goldfish(553): Emulator without GPU emulation detected. 02-21 21:08:39.794: D/AndroidRuntime(553): Shutting down VM 02-21 21:08:39.794: W/dalvikvm(553): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 02-21 21:08:39.824: E/AndroidRuntime(553): FATAL EXCEPTION: main 02-21 21:08:39.824: E/AndroidRuntime(553): android.os.NetworkOnMainThreadException 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 02-21 21:08:39.824: E/AndroidRuntime(553): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 02-21 21:08:39.824: E/AndroidRuntime(553): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 02-21 21:08:39.824: E/AndroidRuntime(553): at libcore.io.IoBridge.connect(IoBridge.java:112) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.startupSocket(Socket.java:566) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.tryAllAddresses(Socket.java:127) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.<init>(Socket.java:177) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.net.Socket.<init>(Socket.java:149) 02-21 21:08:39.824: E/AndroidRuntime(553): at com.exercise.AndroidClient.MyClientActivity$1.onClick(MyClientActivity.java:44) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.view.View.performClick(View.java:3511) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.view.View$PerformClick.run(View.java:14105) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.Handler.handleCallback(Handler.java:605) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:92) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:137) 02-21 21:08:39.824: E/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4424) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 21:08:39.824: E/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:511) 02-21 21:08:39.824: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 02-21 21:08:39.824: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 02-21 21:08:39.824: E/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method)
source share