2013年8月8日星期四

android client using socket connection to communicate with themachine

android do clients connect locally, where the socket connection error, how else do you know?
code: android side:
package com.android;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class NetworkClientActivity extends Activity {
private TextView myTextView;

@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
myTextView = (TextView) findViewById (R.id.msgTextView01);
myTextView.setText ("dd");
try {

Socket socket = new Socket ("192.168.1.115", 10000);

InputStream in = socket.getInputStream ();
byte [] buffer = new byte [in.available ()];
in.read (buffer);
String msg = new String (buffer);
myTextView.setText (msg);
} catch (UnknownHostException e) {

e.printStackTrace ();
} catch (IOException e) {

e.printStackTrace ();
}
}
}
server side:
package com.server;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Myserver {
public static void main (String [] args) {
try {
ServerSocket server = new ServerSocket (10000);
while (true) {
Socket client = server.accept ();
OutputStream out = client.getOutputStream ();
String msg = "Hello android";
out.write (msg.getBytes ());
client.close ();

}
} catch (IOException e) {

e.printStackTrace ();
}
}

}

------ Solution ------------------------------------ --------
remember to open the network connection in the xml permissions Ha pro ~
------ For reference only --------- ------------------------------
who knows ah, this android program into java project if no problem
------ For reference only -------------------------------------- -
you put into 10.0.2.2 192.168.1.115 try
------ For reference only ---------------------- -----------------
Why we wanted to change this, it? I changed, or not ah

------ For reference only ---------------------------------- -----
not ah
------ For reference only ---------------------------- -----------
reported what good too?
------ For reference only -------------------------------------- -
reported this error
java.net.PlainSocketImpl.connect (no source)
------ For reference only ---------------------- -----------------
hey, understand, this simply can not do, android emulator does not provide support. There are real machine on the line.
------ For reference only -------------------------------------- -
simulator how would not support it, upstairs right, privilege
------ For reference only ------------------- --------------------
really written permission, does not work ah
------ For reference only ----- ----------------------------------
using telnet to the next port binding
1. Open cmd
2. input telnet localhost 5554 (5554 is the device number from your simulator)
3. enter telnet mode redir add tcp: 10000:10000

PS: and IP into the second floor (the simulator connected to the PC segment IP)
------ For reference only --------------- ------------------------
The question I would like to know, to help the landlord under the roof. . .
------ For reference only -------------------------------------- -
add permissions yet?
------ For reference only ----------- ----------------------------
I have encountered this problem

One solution is to write the thread of connection:

new Thread()
{
@Override
public void run()
{
try {
       
Socket socket=new Socket("192.168.1.115",10000);

InputStream in=socket.getInputStream();
byte[]buffer=new byte[in.available()];
in.read(buffer);
String msg=new String(buffer);
myTextView.setText(msg);
} catch (UnknownHostException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}.start();


I do not know there is no other solution

没有评论:

发表评论