Android 与PC之间的同步问题
Android手机做Server,PC做Client,现在程序在模拟器上调试没有问题,然后在真机上Server没有反应,另外可能要用到adb forward tcp:localport tcp:deviceport,但是也还是不能成功.
请大侠们帮我想想办法:
PC(Server)c++:
WORD wVersion;
WSADATA wsdata;
int err;
wVersion = MAKEWORD(1,1);
err = WSAStartup(wVersion,&wsdata);
if (err != 0)
{
MessageBox(NULL,L"Load Socket error!",L"",NULL);
return;
}
if (LOBYTE (wsdata.wVersion )!= 1 || HIBYTE(wsdata.wVersion) != 1)
{
MessageBox(NULL,L"Load Socket version error!",L"",NULL);
WSACleanup();
return;
}
SOCKET sockSrv = socket(AF_INET,SOCK_STREAM,0);
if (sockSrv == INVALID_SOCKET )
{
MessageBox(NULL,L"Create socket object error!",NULL,NULL);
}
SOCKADDR_IN addrsrv;
addrsrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrsrv.sin_family = AF_INET;
addrsrv.sin_port = htons(8000);
bind(sockSrv,(SOCKADDR*)&addrsrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrclient;
int len = sizeof(SOCKADDR);
while (1)
{
SOCKET sockConn = accept(sockSrv,(SOCKADDR*)&addrclient,&len);
#if 1
MessageBox(NULL,L"Server4000 received a message!",NULL,NULL);
#endif
char szdata[100] = {0};
char newszdata[200] = {0};
recv(sockConn,szdata,100,0);
sprintf(newszdata,"the server 4000 received data is:%s",szdata);
#if 1
MessageBoxA(NULL,newszdata,NULL,NULL);
#endif
// send(sockConn,"how are you!",strlen("how are you"),0);
//after use the socket,must release the socket object
closesocket(sockConn);
}
smartphone:client(java)
String message = mEditText.getText().toString() + "\r\n";
Log.v("TAG", message);
Socket socket = null;
try
{
socket = new Socket("10.129.85.93",8000);
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(message);
out.close();
socket.close();
}
catch (Exception e)
{
Log.e(DEBUG_TAG, e.toString());
}
}
[解决办法]
没有看你的代码,但是给你一个建议,看懂android的adb通信方式,然后用socket的方式去做同步,数据通过adb来传输就好。
这个100%可以做出来的。
另外手机端的server一定要有root权限,我想你是用会将server编译到source code中的,所以这个不是问题。
[解决办法]
没问题的,手机端作为server,通过adb forward映射某个端口,server监听端口就可以了,我就是这么做的