我想使用套接字通信 TCP 服务器和客户端。我有一个服务器,它已经过测试并且运行正常,但是当我尝试用 Java 实现客户端时,它在创建套接字时崩溃,我不知道为什么。拜托,如果有人能帮助我,我将不胜感激。谢谢。我把我制作的简单应用程序的代码放在这里:
package udp.udp;
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;
public class UdpActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonSend = (Button)findViewById(R.id.send);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
try{
Socket s = new Socket("192.168.1.13", 5003);
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}};
}
以及我得到的失败日志:
05-22 19:37:47.035: D/AndroidRuntime(997): Shutting down VM
05-22 19:37:47.045: W/dalvikvm(997): threadid=1: thread exiting with uncaught exception
(group=0x409c01f8)
05-22 19:37:47.065: E/AndroidRuntime(997): FATAL EXCEPTION: main
05-22 19:37:47.065: E/AndroidRuntime(997): android.os.NetworkOnMainThreadException
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.net.InetAddress.getByName(InetAddress.java:295)
05-22 19:37:47.065: E/AndroidRuntime(997): at
udp.udp.UdpActivity$1.onClick(UdpActivity.java:55)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.view.View.performClick(View.java:3511)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.view.View$PerformClick.run(View.java:14105)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.os.Handler.handleCallback(Handler.java:605)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.os.Handler.dispatchMessage(Handler.java:92)
05-22 19:37:47.065: E/AndroidRuntime(997): at android.os.Looper.loop(Looper.java:137)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.app.ActivityThread.main(ActivityThread.java:4424)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.lang.reflect.Method.invokeNative(Native Method)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.lang.reflect.Method.invoke(Method.java:511)
05-22 19:37:47.065: E/AndroidRuntime(997): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 19:37:47.065: E/AndroidRuntime(997): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 19:37:47.065: E/AndroidRuntime(997): at dalvik.system.NativeStart.main(Native
Method)
05-22 19:37:47.635: I/dalvikvm(997): threadid=3: reacting to signal 3
05-22 19:37:47.655: I/dalvikvm(997): Wrote stack traces to '/data/anr/traces.txt'
05-22 19:42:47.176: I/Process(997): Sending signal. PID: 997 SIG: 9