Android 模拟器(JAVA)与C++ socket 通讯 分享
程序员文章站
2023-11-16 18:08:04
c++ 作为client端view plaincopy to clipboardprint?复制代码 代码如下:// client.cpp : defines the en...
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 所在目录。根据本机情况而定。
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 所在目录。根据本机情况而定。
上一篇: 世界十大有毒鱼类 河豚仅第八第一的被称为“吸血鬼鱼”
下一篇: CATIA零件模型怎么切割曲面?