Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Connecting Android "client" with c++ server. Help needed [SOLVED]

Options
  • 10-04-2011 6:40pm
    #1
    Registered Users Posts: 8,824 ✭✭✭


    EDIT I FOUND THE SOLUTION. I'll leave the thread here though incase I need to reference it again :P or anyone has a similar problem.

    Hey guys. I'm working on a rather ambitious project for college. It's way above what I was required to do but I thought it would be a great chance to learn some extra stuff. It's a deduction game where people send messages and vote for each other. The server of the game takes in strings sent by the android client and does it's magic before sending a message back.
    I have most of the mechanics themselves finished and am pretty comfortable with that part as I've been learning c++ for months now. I have zero experience of java/android coding but it's not too unfamiliar and the app wont be doing much processing (almost only a step up from a twitter app!). My major weakness though is socket programming. This is all new to me! From what I've read block sockets should be fine for this version of the app anyways.

    So what I'm trying to do is just create a very basic c++ server and a very basic android app that connects to it and then I can tidy it up into something better laid out. I have the server and I have a client c++ app that can connect to it (over 127.0.0.1) and I thought I had a similar android client app running in an AVD in Eclipse (both cobbled together from tutorials online) but it keeps failing the try/ catch below giving some IOException error and it won't connect. Can anyone help? Code below:

    c++ Server
    //Headers for command line stuff
    
    #include <sdkddkver.h>
    #include <conio.h>
    #include <stdio.h>
    
    //Headers for sockets
    
    #include <WinSock2.h>
    #include <Windows.h>
    #include <iostream>
    
    //link Ws2_32.lib
    
    using namespace std;
    
    void main()
    {
    	//Start up Winsock
    	long answer;
    	WSAData wsaData;
    	WORD DLLVERSION; //Version of winsock to use
    	DLLVERSION = MAKEWORD(2,1);
    
    	//Start the DLL with WSAStartup
    	answer = WSAStartup(DLLVERSION,&wsaData);
    
    	//DLL is now loaded and we can start using Winsock functions
    
    	//Create structure
    	SOCKADDR_IN addr;
    	//We need the lenght later (seemingly!)
    	int addrlen = sizeof(addr);
    
    	//Create our sockets
    	SOCKET sListen; //For listening for a connection
    	SOCKET sConnect; //Will come in to action when a connection is made with a client
    
    	//Configure connect to the an internet connection(AF_INET) and a streaming conection.
    	sConnect = socket(AF_INET, SOCK_STREAM, NULL);
    
    	//IP adress of our machine. inet_arrd() seems to convert an IP address in a string into the type addr expects
    	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    	//Set the family for the addr structure
    	addr.sin_family = AF_INET;
    	//Set the port of the structure. Think htons has to do with big / small endian conversion...
    	addr.sin_port = htons(1234);
    
    	//Configure sListen socket
    	sListen = socket(AF_INET, SOCK_STREAM, NULL);
    
    	//bind the listen scoket to the IP address and port..
    	//sListen "becomes" the addr structure
    	bind(sListen,(SOCKADDR*)&addr,sizeof(addr));
    	//Set sListen to listen. SOMAXCONN is the maximum connections allowed
    	listen(sListen, SOMAXCONN);
    
    	//With listen set up we create an endless loop until we get a connection.
    
    	for ( ; ; )
    	{
    		cout << "Waiting for connection" << endl;
    
    		//If a connection was found
    		if (sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
    		{
    			cout << "A Connection was found!" <<endl;
    
    			//send a message
    			answer = send(sConnect,"Your Message",13,NULL);
    		}
    	}
    }
    

    java/android code (minus package name as it's using my fullname)
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    import java.net.Socket;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    
    
    public class Client extends Activity {
        /** Called when the activity is first created. */
    	Socket jSocket = null;
    	private final String ipAddress = "127.0.0.1";
    	private final int port = 1234;
    	PrintWriter out = null;
    	BufferedReader inFromServer;
    	String inTxt;
    	
    	@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            try 
            {
            	jSocket = new Socket(ipAddress, port);
            	Log.v("Here", "good");
    
            }
            catch(Exception ee)
            {
            	Log.v("Here", "bad");
            }
            
            
        }
    }
    

    My manifest which should give internet access:
    ?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package=xxxxxxxxx (blocked out for privacy)
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="4" />
        <uses-permission android:name="android.permission.INTERNET"/>
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Client"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        </application>
    </manifest>
    


    Edit -
    Changing the settings in the client to google's IP and port 80 works. I have turned off the firewall on my PC to test too but still no luck :(


Comments

  • Registered Users Posts: 8,824 ✭✭✭ShooterSF


    The problem was that the android virtual device runs under a virtual network and to connect to your PC's loopback address you need to use the IP 10.0.2.2 This worked. More details here under the heading Emulator Networking for those interested.


Advertisement