嗨,我是Android新手,我将以形式发送一些文字
使用套接字从一个Android应用程序吐司到另一个,这里我安装两个
应用程序在不同的Android设备,我通过编辑文本配置IP
从消息接收器应用程序,当我通过change_URL按钮吐司
消息必须显示在另一个应用程序,当我尝试我得到日志
(连接失败:ECONNREFUSED(连接拒绝))和.....我需要帮助
如果有人已经尝试过这个。
- >类Server_test_send
`public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sender_main_);
text = (TextView) findViewById(R.id.msg_view);
updateConversationHandler = new Handler();
// this.serverThread = new Thread(new ServerThread());
//this.serverThread.start();
testBtn = (Button) findViewById(R.id.TestBtn);
urlbtn = (Button) findViewById(R.id.changeurl);
changeurl = new Dialog(this);
changeurl.requestWindowFeature(Window.FEATURE_NO_TITLE);
changeurl.setContentView(R.layout.changeurl);
okbt = (Button) changeurl.findViewById(R.id.okbt);
utlet = (AutoCompleteTextView) changeurl.findViewById(R.id.urlet);
urlbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeurl.show();
}
});
okbt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
path = utlet.getText().toString();
SharedPreferences sp = getSharedPreferences(
"doorcameraurlsas",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp
.edit();
editor.putString("doorcameraurlsas", path);
editor.commit();
new SendText().execute(path);
changeurl.dismiss();
}
});
testBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
public class SendText extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
try {
String url = strings[0];
client = new Socket(url, 8080);
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write("Message from other app");
printwriter.flush();
printwriter.close();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
Another Class
public class MainActivity_Receiver extends Activity implements
View.OnClickListener{
private String off;
private Switch enbmode;
private Socket socket;
private static final int SERVERPORT = 8080;
private static final String SERVER_IP = "192.168.1.60";
ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
}
public void onClick(View view) {
try {
enbmode = (Switch) findViewById(R.id.switch1);
String str = enbmode.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
serverSocket = new ServerSocket(8080); // Server socket
} catch (IOException e) {
Log.i(" PortID","Could not find the port:4444");
}
Log.i("ServerStarted"," with port id :4444");
while (true) {
try {
clientSocket = serverSocket.accept();
inputStreamReader = new
InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
message = bufferedReader.readLine();
Toast.makeText(getApplicationContext(),"The Massage from
client "+message,Toast.LENGTH_LONG).show();
Log.d("Message ", " From server "+message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
Log.i("Problem","Problem in message reading");
}
}
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
`
答案 0 :(得分:0)
如果连接被拒绝,则表示已到达目标主机,但拒绝接受连接。
在标准情况下,这意味着没有套接字正在侦听指定的端口。