Communication between Android simulator (JAVA) and C ++ socket

Source: Internet
Author: User

C ++ as the Client
View plaincopy to clipboardprint? Copy codeThe Code is as follows: // 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) // negotiate the version number
{
Printf ("Arrange Version Failure ");
Return-1;
}
SOCKET nSocket;
NSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // create a 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 simulator, JAVA as the Serve end
View plaincopy to clipboardprint?Copy codeThe Code is as follows: 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); // initialize the socket
Socket pAccSocket = m_pServerSocket.accept (); // accept blocking wait
New RunningThread (pAccSocket). start (); // create a new thread to send and receive data
} 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 [1, 1024];
String sSend = "hello Client! This is Server ";
Byte [] pSendByte = new byte [1, 1024];
PSendByte = sSend. getBytes ();
While (true)
{
Try
{
DataInputStream sRead = new DataInputStream (msocket. getInputStream (); // read
Int nRec = sRead. read (pRecbyte );
If (nRec> 0)
{
// System. out. println ("receive client message success! ");
DataOutputStream sWrite = new DataOutputStream (msocket. getOutputStream ());
SWrite. write (pSendByte); // send
Break;
}
} Catch (Exception e)
{
E. printStackTrace ();
}
}
}
}
}

The IP address used by the android simulator is "127.0.0.1". Using the PC port number, you must use the tool used by the android sdk to perform port ing before debugging the TCP connection of the simulator .. Use the abd tool under Tools in the Sdk and run E in the cmd window: \ install \ android \ Android \ android-sdk-windwows \ tools \ adb forward tcp: 12581 tcp: 31012 "the preceding directory is the directory where android sdk Tools is located. Depends on local conditions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.