gpt4 book ai didi

java - 安卓socket连接失败?

转载 作者:行者123 更新时间:2023-12-01 04:53:36 25 4
gpt4 key购买 nike

我正在编写一个在两部 Android 手机之间发送消息的示例聊天程序,因此我编写了以下代码,并让手机连接到自身(10.0.2.2 或 localhost)以测试代码是否有效。但看起来在 receivemsg() 的线程中,套接字从未连接过。那么我是否使用了错误的 IP 地址来引用自己?或者我的代码有问题吗?感谢您的帮助!

package com.example.chatroomprogram;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ClientActivity extends Activity {

private Handler handler = new Handler();
public ListView msgView;
public ArrayAdapter<String> msgList;
// public ArrayAdapter<String> msgList=new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1);;
public String ipaddress;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
Intent intent = getIntent();
ipaddress=intent.getStringExtra("ipaddress");
Log.i("123","ip is "+ipaddress);
msgView = (ListView) findViewById(R.id.listView);

msgList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
msgView.setAdapter(msgList);
// msgView.smoothScrollToPosition(msgList.getCount() - 1);

Button btnSend = (Button) findViewById(R.id.btn_Send);

receiveMsg();

btnSend.setOnClickListener(new View.OnClickListener() {

@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

final EditText txtEdit = (EditText) findViewById(R.id.txt_inputText);
//msgList.add(txtEdit.getText().toString());
sendMessageToServer(txtEdit.getText().toString());
msgView.smoothScrollToPosition(msgList.getCount() - 1);

}
});

// receiveMsg();
//----------------------------
//server msg receieve
//-----------------------



//End Receive msg from server//
}
public void sendMessageToServer(String str) {

final String str1=str;
new Thread(new Runnable() {

@Override
public void run() {
//String host = "opuntia.cs.utep.edu";
//String host="10.0.";
String host2 = "10.0.2.2";
PrintWriter out = null;

Socket socket = null;
try {
socket = new Socket("10.0.2.2", 8008);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out = new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("123","not null");
// out.println("hello");
out.println(str1);
Log.i("123", "hello");
out.flush();


}
}).start();
}



public void receiveMsg()
{
new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub

//final String host="opuntia.cs.utep.edu";
final String host="10.0.2.2";
//final String host="127.0.0.1";
Socket socket = null ;
BufferedReader in = null;
try {
//socket = new Socket(host,8008);
ServerSocket ss = new ServerSocket(8008);
socket = ss.accept();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

while(true)
{
String msg = null;
try {
msg = in.readLine();
Log.i("123","MSGGG: "+ msg);

//msgList.add(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(msg == null)
{
break;
}
else
{
displayMsg(msg);
}
}

}
}).start();


}

public void displayMsg(String msg)
{
final String mssg=msg;
handler.post(new Runnable() {

@SuppressLint("NewApi")
@Override
public void run() {
// TODO Auto-generated method stub
msgList.add(mssg);
msgView.setAdapter(msgList);
msgView.smoothScrollToPosition(msgList.getCount() - 1);
Log.i("123","hi");
}
});

}

}

更新:问题解决,结论:如果使用AVD,就使用10.0.2.2即可;如果使用真机调试,可以使用localhost

最佳答案

我的错...我使用了 10.0.2.2 并且它有效...

关于java - 安卓socket连接失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14529210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com