当前位置:编程学习 > JAVA >>

localsocket连接提示connect refused,求大侠如何解决

服务端代码
package com.example.ls;

import java.net.*;
import java.io.*;

import android.net.LocalServerSocket;
import android.net.LocalSocket;

public class SimpleServer
{
public static final String SOCKET_ADDRESS = "socket_address";

public static void main(String[] args) 
throws IOException
{
LocalServerSocket ss = new LocalServerSocket(SOCKET_ADDRESS);
while (true)
{
LocalSocket s = ss.accept();
OutputStream os = s.getOutputStream();
os.write("您好!\n"
.getBytes("utf-8"));
os.close();
s.close();
}
}
}

客户端代码:
package com.example.ls;


import java.io.BufferedReader;
import java.io.OutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.Socket;


import android.app.Activity;

import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;

import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;




public class MainActivity extends Activity
{

EditText show;
public static final String SOCKET_ADDRESS = "socket_address";
@Override

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Log.i("11", "11");
show = (EditText) findViewById(R.id.show);

try

{
Log.i("1", "1");
LocalSocket socket = new LocalSocket();
LocalSocketAddress l=new LocalSocketAddress(SOCKET_ADDRESS);
socket.connect(l);
Log.i("2", "2");
BufferedReader br = new BufferedReader(

new InputStreamReader(socket.getInputStream()));


String line = br.readLine();

show.setText("来自服务器的数据:" + line);
Log.i("3", "3");
br.close();

socket.close();
}

catch (IOException e)
{

e.printStackTrace();

}
}
}


错误信息是:
10-23 05:56:39.390: W/System.err(1440): java.io.IOException: Connection refused
10-23 05:56:39.390: W/System.err(1440):  at android.net.LocalSocketImpl.connectLocal(Native Method)
10-23 05:56:39.390: W/System.err(1440):  at android.net.LocalSocketImpl.connect(LocalSocketImpl.java:238)
10-23 05:56:39.390: W/System.err(1440):  at android.net.LocalSocket.connect(LocalSocket.java:98)
10-23 05:56:39.390: W/System.err(1440):  at com.example.ls.MainActivity.onCreate(MainActivity.java:50)
10-23 05:56:39.390: W/System.err(1440):  at android.app.Activity.performCreate(Activity.java:5008)
10-23 05:56:39.400: W/System.err(1440):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-23 05:56:39.400: W/System.err(1440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-23 05:56:39.400: W/System.err(1440):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-23 05:56:39.400: W/System.err(1440):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-23 05:56:39.400: W/System.err(1440):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-23 05:56:39.410: W/System.err(1440):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 05:56:39.432: W/System.err(1440):  at android.os.Looper.loop(Looper.java:137)
10-23 05:56:39.432: W/System.err(1440):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-23 05:56:39.432: W/System.err(1440):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 05:56:39.432: W/System.err(1440):  at java.lang.reflect.Method.invoke(Method.java:511)
10-23 05:56:39.432: W/System.err(1440):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-23 05:56:39.432: W/System.err(1440):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 05:56:39.432: W/System.err(1440):  at dalvik.system.NativeStart.main(Native Method)
--------------------编程问答-------------------- 求助啊~~~ --------------------编程问答-------------------- 没人能解决吗~~ --------------------编程问答-------------------- 不知道楼主解决没有?我也遇到相关的问题 --------------------编程问答-------------------- 你没通过ip? --------------------编程问答-------------------- SOCKET_ADDRESS = "socket_address";
不用具体的IP地址吗? --------------------编程问答-------------------- SOCKET_ADDRESS = "socket_address"; 
这地址谁认识? --------------------编程问答-------------------- 底层创建的socket 是有权限的。
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,