我编写了简单的客户端服务器套接字编程代码,如下所示
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
dataOutputStream.writeUTF("Hello!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if( socket!= null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataInputStream!= null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataOutputStream!= null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}}
android客户端如下
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;
/** Called when the activity is first created. */
@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) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("113.193.42.220", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}};
}
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>
我在执行client.pls时遇到FatalException,建议我如何处理这个问题。 我已经尝试了我的电脑的IP地址和127.0.0.1也所以我有任何问题的IP地址或任何其他导致FatalException请帮助我。
logcat如下
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)
答案 0 :(得分:6)
您正在主应用程序线程上访问网络。 Android 3+中不允许这样做。您需要启动单独的线程访问网络。
您可以使用AsyncTask
将代码(即正在访问的网络)与另一个Thread
分开。