3
\$\begingroup\$

Starting to write a game, it'll have a C server, and at the very least an Android client. However I have run into an issue where I can't seem to get them to connect. I have thrown debug logging into each to see where they're stopping. The C code stops at accept() and the Android code stops at Socket() nothing gets printed to logs after them for either. I've tried setting ai_family to ipv6 and ipv4, tried forcing ipv6_v6only both on and off in setsockopt. nmap shows the server as "closed". I presume if it was working correctly it should be "open". Ports that aren't allowed through the firewall show up as "filtered", so that's not an issue. The android VM can reach my website on the same server, so it's not DNS/connectivity issues. I can only think it's a problem in my code itself.

main.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int main(){
    int status,serv_socket,accept_socket;
    struct addrinfo hints,*servinfo;
    struct sockaddr_storage rem_addr;
    socklen_t addr_size;
    char user_name[32];

    memset(&hints,0,sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    status = getaddrinfo("arenlor.com","62674",&hints,&servinfo);
    if(status != 0) {
        perror("getaddrinfo");
        exit(1);
    }

    serv_socket = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol);
    if(serv_socket == -1) {
        perror("socket");
        exit(1);
    }

    status = bind(serv_socket,servinfo->ai_addr,servinfo->ai_addrlen);
    if(status == -1) {
        perror("bind");
        exit(1);
    }
    freeaddrinfo(servinfo);

    status = listen(serv_socket,10);
    if(status == -1) {
        perror("listen");
        exit(1);
    }

    addr_size = sizeof(rem_addr);
    accept_socket = accept(serv_socket,(struct sockaddr *)&rem_addr,&addr_size);
    if(accept_socket == -1) {
        perror("accept");
        exit(1);
    }
    status = (int)recv(accept_socket,user_name,sizeof(user_name),0);
    if(status == -1) {
        perror("recv");
        exit(1);
    }

    fprintf(stdout,"%s\n",user_name);
    shutdown(serv_socket,2);
    shutdown(accept_socket,2);
    return 0;
}

MainActivity.java

package com.arenlor.game.arenrpg;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void attemptLogin(View view) {
        new blahBlah().execute("Arenlor");
    }

    private static class blahBlah extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... loginData) {
            try {
                Socket gameConnect = new Socket("arenlor.com",62674);
                if(!gameConnect.isConnected()) {
                    Log.d("isConnected","Couldn't connect");
                    return null;
                }
                Writer out = new BufferedWriter(new OutputStreamWriter(gameConnect.getOutputStream()));
                out.write(loginData[0]);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:id="@+id/button" android:text="@string/loginButton"
            android:onClick="attemptLogin"/>
</android.support.constraint.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.arenlor.game.arenrpg">
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
\$\endgroup\$
  • \$\begingroup\$ after: if(status == -1) { perror("recv"); exit(1); probably should have: if( status == 0 ) { perror( "client closed" ); exit( 1 ); \$\endgroup\$ – user3629249 Jul 22 '18 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.