Android Emulator (JAVA) share _android with C + + socket communication

Source: Internet
Author: User
Tags port number
C + + as client side
View Plaincopy to Clipboardprint?
Copy Code code 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))//negotiated version number
{
printf ("Arrange Version failure");
return-1;
}
SOCKET Nsocket;
Nsocket = socket (af_inet, sock_stream, ipproto_tcp); Creating 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 emulator, Java as serve end
View Plaincopy to Clipboardprint?
Copy Code code 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 the activity is a. */
@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 socket
Socket paccsocket = m_pserversocket.accept (); Accept blocking Waiting
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[1024];
String ssend = "Hello client! ' Is Server ';
byte [] psendbyte = new byte[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 Android emulator uses the IP address "127.0.0.1"; using the PC's port number, you must use the tools used by the Android SDK to perform a port mapping before the emulator TCP connection is debugged. As shown in the figure. Even with the tools in the Sdk Abd tool, run e:\\install\\android\\android\\android-sdk-windwows\\tools\\adb forward in the cmd window tcp:12581 tcp:31012 "The previous directory is the directory where the Android SDK Tools are located. Depending on the circumstances of the machine.

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.