gpt4 book ai didi

android - 如何在没有服务器的情况下通过套接字连接两个 Android 设备

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:20 25 4
gpt4 key购买 nike

我正在尝试开发一个 android 应用程序,它可以在没有服务器的情况下与其他设备在对等连接上交换数据。所以请建议我该怎么做。提前谢谢你。

最佳答案

这是一个完整的SocketProgramming无服务器聊天代码。

在我的应用程序中,首先您是客户端,然后搜索服务器。当您找不到任何服务器时,您将成为服务器并等待客户端。

public class MainActivity extends ActionBarActivity {

private Handler handler = new Handler();
private TextView text;
private EditText input;
private Button send;
private Socket socket;
private DataOutputStream outputStream;
private BufferedReader inputStream;
private String DeviceName = "Device";

private boolean searchNetwork() {
log("Connecting");
String range = "192.168.56.";
for (int i = 1; i <= 255; i++) {
String ip = range + i;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ip, 9000), 50);
outputStream = new DataOutputStream(socket.getOutputStream());
inputStream = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
DeviceName += "1";
Log.i("Server", DeviceName);
log("Connected");
return true;
} catch (Exception e) {
}
}
return false;

}

private void runNewChatServer() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(9000);
log("Waiting for client...");
socket = serverSocket.accept();
DeviceName += "2";
log("a new client Connected");
} catch (IOException e) {
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
input = (EditText) findViewById(R.id.input);
send = (Button) findViewById(R.id.send);
Thread thread = new Thread(new Runnable() {

@Override
public void run() {
try {
if (!searchNetwork()) {
runNewChatServer();

}

outputStream = new DataOutputStream(
socket.getOutputStream());
inputStream = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while (true) {

String Message = inputStream.readLine();
if (Message != null) {
log(Message);
}
}
} catch (IOException e) {
log("Error: IO Exception");
e.printStackTrace();
}
}
});
send.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
if (outputStream == null) {
return;
}
try {
String Message = input.getText().toString() + "\n";
outputStream.write(Message.getBytes());
log2(input.getText().toString());
} catch (IOException e) {
e.printStackTrace();
}
input.setText("");
}
});
thread.start();

}

private void log(final String message) {
handler.post(new Runnable() {
String DeviceName2="";
@Override
public void run() {
if (DeviceName.equals("Device1")) {
DeviceName2 = "Device2";
}else if(DeviceName.equals("Device2")) {
DeviceName2 = "Device1";
}else{
DeviceName2 = "UnknowDevice";
}

text.setText(text.getText() + "\n" + DeviceName2 + " :"
+ message);

}
});
}
private void log2(final String message) {
handler.post(new Runnable() {

@Override
public void run() {


text.setText(text.getText() + "\n" + "you" + " :"
+ message);

}
});
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.exit(0);
return true;
}
return super.onKeyDown(keyCode, event);
}

}

关于android - 如何在没有服务器的情况下通过套接字连接两个 Android 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29163381/

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