android开发中,android虚拟机作为服务器与pc客户端实现socket网络通信中,android虚拟机的ip是多少?

android开发中,android虚拟机作为服务器与pc客户端实现socket网络通信中,android虚拟机的ip是多少?,第1张

Android使用的仅仅使用了PC 里边的一个端口

IP自然就是127001了,或者是localhost吧

关于5037口和5555口,找到了一个看起来合理的解释:

adb(Android Debug Bridge)包括三个部分:

1)adb client, 运行在PC上(为DDMS,即IDE工作)

2)adb daemon(守护进程), 运行于Emulator(为与Emulator中的VM交互工作);

3)adb server(服务进程), 运行在PC(任务管理器上有),管理着adb client和adb daemon的通信server与client通信的端口是是5037,

adb server会与emulator交互的,使用的端口有两个,一个是5554专门用于与Emulator实例的连接,那么数据可以从Emulator转发给IDE控制台了,另一个则是5555,专门与adb daemon连接为后面调试使用。

一般来说,通信协议都会设计成语言无关的,所以,服务器端使用什么语言和客户端是什么平台没关系。

所以,服务器使用什么语言,只和你的研发团队习惯使用什么语言和什么语言可以满足业务需求有关。。

一、HTTP请求(APACHE的HttpClient实现)

服务器端,就是普通的servlet、Strutus2就可以

移动端

protected static String get(String url, List<NameValuePair> params) {

String resultMsg;

// 设置http请求配置

HttpParams parms = new BasicHttpParams();

parmssetParameter("charset", HTTPUTF_8);

// 配置连接超时

HttpConnectionParamssetConnectionTimeout(parms, 10 1000);

// 设置请求超时

HttpConnectionParamssetSoTimeout(parms, 15 1000);

// 实例化HttpClient

HttpClient httpclient = new DefaultHttpClient(parms);

// 实例化HttpGet

HttpGet httpget = new HttpGet(url);

// 设置请求头

httpgetaddHeader("Content-Type", "application/json");

httpgetaddHeader("charset", HTTPUTF_8);

try {

if (paramssize() > 0)

url = url + "" + URLEncodedUtilsformat(params, HTTPUTF_8);

HttpResponse resp = httpclientexecute(httpget);

int statusCode = respgetStatusLine()getStatusCode();

if (statusCode == HttpStatusSC_OK) {

StringBuffer result = getResponse(resp);

resultMsg = resulttoString();

} else {

resultMsg = "连接异常";

}

} catch (Exception e) {

resultMsg = "连接异常";

} finally {

// 关闭get

httpgetabort();

// 关闭连接 ,释放资源

httpclientgetConnectionManager()shutdown();

}

return resultMsg;

}

protected static String post(String uri, Object params) {

String resultMsg;

// 设置http请求配置

HttpParams hp = new BasicHttpParams();

hpsetParameter("charset", HTTPUTF_8);

// 配置连接超时

HttpConnectionParamssetConnectionTimeout(hp, 10 1000);

HttpConnectionParamssetSoTimeout(hp, 15 1000);

// 实例化HttpClient

HttpClient httpclient = new DefaultHttpClient(hp);

// 实例化HttpPost请求

HttpPost httppost = new HttpPost(uri);

// 设置头信息

httppostaddHeader("Content-Type", "application/json");

httppostaddHeader("charset", HTTPUTF_8);

try {

// 将参数进行json化

ObjectMapper mapper = new ObjectMapper();

String jsonStr = mapperwriteValueAsString(params);

Logi(TAG, "URI=" + uri + ",BEAN=" + jsonStr);

// 定义消息实体

StringEntity se = new StringEntity(jsonStr, HTTPUTF_8);

httppostsetEntity(se);

// 通信

HttpResponse resp = httpclientexecute(httppost);

int statusCode = respgetStatusLine()getStatusCode();

Logi(TAG, "StatusCode=" + statusCode);

if (statusCode == HttpStatusSC_OK) {

StringBuffer result = getResponse(resp);

resultMsg = resulttoString();

} else {

resultMsg = "连接异常";

}

} catch (Exception e) {

eprintStackTrace();

resultMsg = "连接异常";

} finally {

// 关闭get

httppostabort();

// 关闭连接 ,释放资源

httpclientgetConnectionManager()shutdown();

}

Logi(TAG, resultMsg);

return resultMsg;

}

二、SOCKET连接

服务器端:

import javaioBufferedReader;

import javaioIOException;

import javaioInputStreamReader;

import javaioPrintWriter;

import javanetServerSocket;

import javanetSocket;

public class service_java_test {

public static void main(String[] args) throws IOException {

ServerSocket server = new ServerSocket(10000); //绑定的端口号

Socket socket = serveraccept(); //连接不成功以至于下一行的"连接成功"

//在调试区显示不出来

Systemoutprintln("连接成功");

BufferedReader in = new BufferedReader(new InputStreamReader(socketgetInputStream()));

PrintWriter out = new PrintWriter(socketgetOutputStream());

while (true) {

String msg = inreadLine();

Systemoutprintln(msg);

outprintln("Server received " + msg); //向接收方发送已接受到了的语句

outflush();

if (msgequals("bye")) { //若接收到"bye"则break

break;

}

}

socketclose();

}

}

安卓客户端:

package comexamplet4_android;

import javanetSocket;

import androidappActivity;

import androidosBundle;

import androidwidgetTextView;

public class MainActivity extends Activity {

private TextView myTextView;

protected void onCreate(Bundle savedInstanceState) {

superonCreate(savedInstanceState);

setContentView(Rlayoutactivity_main);

myTextView = (TextView) findViewById(RidtextView1);

Thread t = new Thread(new Runnable(){

public void run(){

try {

Socket sk = new Socket("1921682531", 10000);//绑定套接字,这一行一直执行不成功

//以至于下一行在安卓页面的TextView上

//不显示“已连接”的字样

//"1921682531"是我利用DOS命令查找

//的本机IP

myTextViewsetText("已连接");

} catch (Exception e) {

eprintStackTrace();

}

}

});

tstart();

}

}

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » android开发中,android虚拟机作为服务器与pc客户端实现socket网络通信中,android虚拟机的ip是多少?

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情