原文:http://www.rosoo.net/a/201112/15430.html
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 所在目录。根据本机情况而定。
如果有多个设备(模拟器或者真机)连接在PC上,可以指定设备:
adb -s
例如:adb -s HTC91245667 forward tcp:12581 tcp:31012
程序C++ 部分编程环境vs 2005 android 采用eclipse 下 android 2.1版本。测试通过。以下是完整源码:
C++ 作为Client端
- // 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端
- 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();
- }
- }
- }
- }
- }