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

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 所在目录。根据本机情况而定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值