gpt4 book ai didi

android - 套接字连接android

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:09:15 26 4
gpt4 key购买 nike

我试图在android中打开输入和输出流,但我不知道如何进行。我之前在服务器中使用以下命令在j2me中打开了一个套接字连接:

scn = (ServerSocketConnection) Connector.open("socket://:5000");     
String myAddress = scn.getLocalAddress();
si.setText("My address: " + myAddress);
// Wait for a connection.
sc = (SocketConnection) scn.acceptAndOpen();
//Get address of socket connection
String yourAddress = sc.getAddress();
si.setText("Connected to: " + yourAddress);
is = sc.openInputStream();
os = sc.openOutputStream();

现在我想把这个放到android中,但它不允许我打开输入和输出流,而且我不得不改变一些我不确定是否能以同样的方式工作的事情。
scn = (ServerSocket) Connector.open("socket://:5000");    
String myAddress = scn.getLocalAddress();
Connection.setText("My address: " + myAddress);
// Wait for a connection.
Socket sc = scn.accept();
//Get address of socket connection
String yourAddress = sc.getAddress();
Connection.setText("Connected to: " + yourAddress);
is = sc.openInputStream();
os = sc.openOutputStream();

我在android中遇到的错误有connector、getlocaladdress、getaddress以及openinput和outputstreams。
我会感谢任何帮助,因为我有很大的困难,在安卓设置这一点。
谢谢
编辑:
我已将Android代码更改为:
ServerSocket scn = new ServerSocket(5554);    
InetAddress myAddress = scn.getInetAddress();
Connection.setText("My address: " + myAddress);
// Wait for a connection.
Socket sc = scn.accept();
//Get address of socket connection
InetAddress yourAddress = sc.getInetAddress();
Connection.setText("Connected to: " + yourAddress);
is = sc.openInputStream();
os = sc.openOutputStream();

它编译得很好,但我得到一个错误:
 is = sc.openInputStream();
os = sc.openOutputStream();

在android中有没有其他打开输入和输出流的方法?
谢谢。

最佳答案

你可以试试这个链接上的代码
http://www.helloandroid.com/tutorials/simple-connection-example-part-ii-tcp-communication
如果对你有帮助,请告诉我。

关于android - 套接字连接android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5057417/

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