服务器之家

服务器之家 > 正文

Android 模拟器(JAVA)与C++ socket 通讯 分享

时间:2021-01-20 15:50     来源/作者:Android开发网

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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部