Socket连接的androidSocket、android

2023-09-06 15:35:57 作者:烟灬伤肺*不伤心ˇˇ

我试图打开Android的输入和输出流,但我不知道如何去这个问题。我已经打开在J2ME $ P $套接字连接pviously使用以下服务器:

I am trying to open input and output streams in android but I'm not sure how to go about this. I have opened a socket connection in J2ME previously by using the following in the server:

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();

现在我想把这变成机器人,但它不是让我打开的输入和输出流,也是我不得不改变这我不知道会工作以同样的方式的几件事情。

Now I want to put this into android but its not letting me open input and output streams and also I had to change a few things which I'm not sure would work in the same way.

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上的错误与连接器,getLocalAddress,的getAddress和openInput和OutputStreams。

The errors which I am getting in android are with the Connector, getLocalAddress, getAddress and openInput and OutputStreams.

我会AP preciate有这方面的帮助,我有很大的难度设置此功能的机器人。

I would appreciate any help on this, as I am having great difficulty setting this up in android.

感谢

编辑:

我已经改变了Android code到这一点:

I have changed the Android code to this:

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();

这编译罚款,但我得到一个错误的:

Which compiles fine but I get an error on:

 is = sc.openInputStream();
        os = sc.openOutputStream();

有另一种方式在Android开放输入和输出流?

Is there another way to open input and output streams in android?

感谢。

推荐答案

你可以试试这个链接上的code

you can try the code on this link

的http://www.helloandroid.com/tutorials/simple-connection-example-part-ii-tcp-communication

让我知道如果这对你有所帮助。

let me know if it helpful to you.

 
精彩推荐
图片推荐