问题描述
服务端代码packagecom.example.ls;importjava.net.*;importjava.io.*;importandroid.net.LocalServerSocket;importandroid.net.LocalSocket;publicclassSimpleServer{publicstaticfinalStringSOCKET_ADDRESS="socket_address";publicstaticvoidmain(String[]args)throwsIOException{LocalServerSocketss=newLocalServerSocket(SOCKET_ADDRESS);while(true){LocalSockets=ss.accept();OutputStreamos=s.getOutputStream();os.write("您好!n".getBytes("utf-8"));os.close();s.close();}}}
客户端代码:packagecom.example.ls;importjava.io.BufferedReader;importjava.io.OutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.Socket;importandroid.app.Activity;importandroid.net.LocalServerSocket;importandroid.net.LocalSocket;importandroid.net.LocalSocketAddress;importandroid.os.Bundle;importandroid.util.Log;importandroid.widget.EditText;importandroid.widget.TextView;publicclassMainActivityextendsActivity{EditTextshow;publicstaticfinalStringSOCKET_ADDRESS="socket_address";@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i("11","11");show=(EditText)findViewById(R.id.show);try{Log.i("1","1");LocalSocketsocket=newLocalSocket();LocalSocketAddressl=newLocalSocketAddress(SOCKET_ADDRESS);socket.connect(l);Log.i("2","2");BufferedReaderbr=newBufferedReader(newInputStreamReader(socket.getInputStream()));Stringline=br.readLine();show.setText("来自服务器的数据:"+line);Log.i("3","3");br.close();socket.close();}catch(IOExceptione){e.printStackTrace();}}}
错误信息是:10-2305:56:39.390:W/System.err(1440):java.io.IOException:Connectionrefused10-2305:56:39.390:W/System.err(1440):atandroid.net.LocalSocketImpl.connectLocal(NativeMethod)10-2305:56:39.390:W/System.err(1440):atandroid.net.LocalSocketImpl.connect(LocalSocketImpl.java:238)10-2305:56:39.390:W/System.err(1440):atandroid.net.LocalSocket.connect(LocalSocket.java:98)10-2305:56:39.390:W/System.err(1440):atcom.example.ls.MainActivity.onCreate(MainActivity.java:50)10-2305:56:39.390:W/System.err(1440):atandroid.app.Activity.performCreate(Activity.java:5008)10-2305:56:39.400:W/System.err(1440):atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)10-2305:56:39.400:W/System.err(1440):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)10-2305:56:39.400:W/System.err(1440):atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)10-2305:56:39.400:W/System.err(1440):atandroid.app.ActivityThread.access$600(ActivityThread.java:130)10-2305:56:39.400:W/System.err(1440):atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)10-2305:56:39.410:W/System.err(1440):atandroid.os.Handler.dispatchMessage(Handler.java:99)10-2305:56:39.432:W/System.err(1440):atandroid.os.Looper.loop(Looper.java:137)10-2305:56:39.432:W/System.err(1440):atandroid.app.ActivityThread.main(ActivityThread.java:4745)10-2305:56:39.432:W/System.err(1440):atjava.lang.reflect.Method.invokeNative(NativeMethod)10-2305:56:39.432:W/System.err(1440):atjava.lang.reflect.Method.invoke(Method.java:511)10-2305:56:39.432:W/System.err(1440):atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)10-2305:56:39.432:W/System.err(1440):atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-2305:56:39.432:W/System.err(1440):atdalvik.system.NativeStart.main(NativeMethod)
解决方案
解决方案二:
求助啊~~~
解决方案三:
没人能解决吗~~
解决方案四:
不知道楼主解决没有?我也遇到相关的问题
解决方案五:
解决方案六:
SOCKET_ADDRESS="socket_address";不用具体的IP地址吗?
解决方案七:
SOCKET_ADDRESS="socket_address";这地址谁认识?
解决方案八:
底层创建的socket是有权限的。
解决方案九:
楼主后来怎么解决的,求答案啊!