开发者

Socket connection android

开发者 https://www.devze.com 2023-02-12 08:28 出处:网络
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:

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

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

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

Thanks

Edit:

I have changed the Android code to this:

Serve开发者_运维知识库rSocket 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();

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

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号