Ready to use C / C + + to write a dll project, which works as a socket client and server communicate android. , android server dll is to give the user calls a running app on the phone. Then the dll code, how to write it? Neighborhoods<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
I also recently had an idea you want to say this program, but LZ, I'm curious if the server is APP as a cell phone, then the client how to find the IP of the server is not fixed yet.
Reply:
No (fixed) ip, the data communication between the two lines
Reply:
To operate in this network segment
Reply:
Can say more points
Reply:
If the LAN does fit, what if it is GPRS, IP can not be fixed.
You can set a fixed IP address in the LAN.
As with dll socket client to connect to Android Service that actually rare.
Reply:
1 If you go mobile service, 2G, 3G, GPRS horse of God, it is through the base station assigned ip, can not control. If you pour should be able to use the wifi, the route set up, just do not know whether between wifi and wired LAN, you can try.
2 phone itself generally as a client application, server you try to turn on the phone, I do not understand a bit. Since you say you use the data cable, data lines that are connected to the device, and also what socket ah, this is not superfluous. The landlord was curious what the real needs in the end.
Reply:
server put cell phones, other devices would like the phone to initiate communication request, and then send a command, the mobile terminal receives the command and returns the command response. Also, I do not know what you said, "Since you say you use the data cable, data cable that is connected to the device, and also what socket ah, it does not bother the" implicit meaning? I would like to ask, between two devices without using wifi and 2g, 3g network like the only connection between the two lines through the data, then both should be how to communicate it?
Reply:
This uncommon ah, pea pods are so right, the landlord said too subtle, ha ha
Reply:
Oh, know. If you have such demand server does have a role in it!
Reply:
Problem solved. Includes two header files and a cpp file, the compiler will generate a dll file, then write a call this dll works on ok.
PS: Some of the code is
online referencesInitSock.h
/ / Initsock.h file
# Include
# Include
# Include
# Include
# Pragma comment (lib, "WS2_32") / / Link to WS2_32.lib
class CInitSock
{
public:
CInitSock (BYTE minorVer = 2, BYTE majorVer = 2)
{
/ / Initialize WS2_32.dll
WSADATA wsaData;
WORD sockVersion = MAKEWORD (minorVer, majorVer);
if (:: WSAStartup (sockVersion, & wsaData)! = 0)
{
exit (0);
}
}
~ CInitSock ()
{
:: WSACleanup ();
}
};
SocketDll.h
# Pragma once
# Define DLL_EXPORT __ declspec (dllexport)
extern "C" DLL_EXPORT void startSocket ();
SocketDll.cpp
# Include "InitSock.h"
# Include
# Include
# Include
using namespace std;
CInitSock initSock; / / initialize Winsock library
# Include "SocketDll.h"
void startSocket ()
{
/ / Port mapping
system ("adb forward tcp: 8081 tcp: 10086");
/ / Create sets of characters
SOCKET s = :: socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf ("Failed socket () / n");
return;
}
/ / Fill in the remote address information
sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons (8081);
/ / Set ip
servAddr.sin_addr.S_un.S_addr = inet_addr ("127.0.0.1");
if (:: connect (s, (sockaddr *) & servAddr, sizeof (servAddr)) == -1)
{
printf ("Failed connect () / n");
return;
} Else {
cout << "successfully connected!" << endl;
}
char buff [256];
char szText [256];
while (TRUE)
{
/ / Send data to the server
cin >> szText;
szText [255] = '/ 0';
:: Send (s, szText, strlen (szText), 0);
/ / Receive data from the server side
int nRecv = :: recv (s, buff, 256, 0);
if (nRecv> 0)
{
buff [nRecv] = '/ 0';
printf ("the received data:% s / n", buff);
}
}
/ / Close the sets of characters
:: Closesocket (s);
}
No comments:
Post a Comment