当前位置:   article > 正文

java安卓模拟器和电脑通信_Android 模拟器(JAVA)与C++ socket 通讯 分享

怎样用java连接安卓模拟器

C++ 作为Client端

view plaincopy to clipboardprint?

// Client.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include

#pragma comment(lib,"ws2_32.lib")

#define  MAX_BUF_SIZE 1024

#define  PORT_NUMBER 12581

int _tmain(int argc, _TCHAR* argv[])

{

WSADATA wSaData;

WORD dwSockVersion = MAKEWORD(2,2);

if (0 != WSAStartup(dwSockVersion,&wSaData)) //协商版本号

{

printf("Arrange Version Failure");

return -1;

}

SOCKET nSocket;

nSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创建TCP socket

if (INVALID_SOCKET == nSocket)

{

printf("invalid socket");

WSACleanup();

return -1;

}

sockaddr_in sa;

sa.sin_family = AF_INET;

sa.sin_addr.s_addr = inet_addr("127.0.0.1");

sa.sin_port = htons(PORT_NUMBER);

if ( 0 != connect( nSocket,( const SOCKADDR * )&sa, sizeof(sa) ) )

return -1;

char buf[MAX_BUF_SIZE] = {0};

char tmp[MAX_BUF_SIZE] = {0};

strcpy(tmp,"this is Client!");

int nSend = send(nSocket, tmp, (int)strlen(tmp), 0);

int nRecv = 0;

nRecv = recv(nSocket, buf, MAX_BUF_SIZE, 0);

if (nRecv > 0)

{

printf("%s\n",buf);

}

closesocket(nSocket);

WSACleanup();

return 0;

}

Android 模拟器,JAVA作为Serve端

view plaincopy to clipboardprint?

package com.Android.SocketTest;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.net.InetAddress;

import java.net.ServerSocket;

import java.net.Socket;

import android.app.Activity;

import android.os.Bundle;

public class SocketTest extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

StartAcceptSocket();

}

private void StartAcceptSocket()

{

try

{

short nPort = 31012;

ServerSocket m_pServerSocket = new ServerSocket(nPort); //初始化socket

Socket pAccSocket = m_pServerSocket.accept(); //accept 阻塞等待

new RunningThread(pAccSocket).start();  //新建一个线程进行数据收发

}catch(Exception e)

{

e.printStackTrace();

}

}

public class RunningThread extends Thread

{

private Socket msocket = null;

RunningThread(Socket s)

{

this.msocket = s;

}

public void run()

{

byte [] pRecbyte = new byte[1024];

String sSend = "hello Client! this is Server";

byte [] pSendByte = new byte[1024];

pSendByte = sSend.getBytes();

while(true)

{

try

{

DataInputStream sRead = new DataInputStream(msocket.getInputStream());  //读取

int nRec = sRead.read(pRecbyte);

if(nRec > 0 )

{

//System.out.println("receive client message success!");

DataOutputStream    sWrite = new DataOutputStream(msocket.getOutputStream());

sWrite.write(pSendByte);  //发送

break;

}

}catch(Exception e)

{

e.printStackTrace();

}

}

}

}

}

android 模拟器使用的IP 地址为"127.0.0.1";使用PC机的端口号,在模拟器TCP连接调试之前必须使用android sdk 使用的工具进行一次端口映射。如图所示。即使用 Sdk 中的Tools下abd 工具,在cmd 窗口中运行E:\\install\\android\\Android\\android-sdk-windwows\\tools\\adb forward tcp:12581 tcp:31012" 前面目录为android sdk  Tools 所在目录。根据本机情况而定。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/320679
推荐阅读
相关标签
  

闽ICP备14008679号