Android Open Source - ConnectedSpace Server Socket Thread






From Project

Back to project page ConnectedSpace.

License

The source code is released under:

Copyright (c) 2015, Shreyas Raman <skraman1999@gmail.com>.

If you think the Android project ConnectedSpace listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.shreyasr.connected.android.networking;
/*ww  w.j  av a2s. c  o  m*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import me.shreyasr.connected.android.AndroidLauncher;
import me.shreyasr.connected.network.NetworkHandler;

class ServerSocketThread implements Runnable {

    private final Activity lobbyActivity;
    private ServerSocket serverSocket;

    public ServerSocketThread(Activity lobbyActivity) {
        this.lobbyActivity = lobbyActivity;
        try {
            serverSocket = new ServerSocket(4243);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while(true) {
            try {
                final Socket socket = serverSocket.accept();
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        new AlertDialog.Builder(lobbyActivity)
                                .setTitle("Incoming Request")
                                .setMessage("Incoming Game Request")
                                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        try {
                                            socket.getOutputStream().write(1);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                                            @Override
                                            public void run() {
                                                Log.d("GAME", "Starting game from connection.");
                                                AndroidLauncher.network = new NetworkHandler(socket);
                                                Intent intent = new Intent(lobbyActivity, AndroidLauncher.class);
                                                lobbyActivity.startActivity(intent);
                                            }
                                        });
                                    }
                                })
                                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        try {
                                            socket.getOutputStream().write(0);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        try {
                                            socket.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                })
                                .setIcon(android.R.drawable.ic_dialog_alert)
                                .show();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}




Java Source Code List

me.shreyasr.connected.ConnectedSpace.java
me.shreyasr.connected.android.AndroidLauncher.java
me.shreyasr.connected.android.activity.ClientListAdapter.java
me.shreyasr.connected.android.activity.LobbyActivity.java
me.shreyasr.connected.android.networking.BroadcastListenerThread.java
me.shreyasr.connected.android.networking.ConnectionThread.java
me.shreyasr.connected.android.networking.HeartbeatThread.java
me.shreyasr.connected.android.networking.LobbyManager.java
me.shreyasr.connected.android.networking.ServerSocketThread.java
me.shreyasr.connected.android.util.AndroidAddressUtils.java
me.shreyasr.connected.android.util.Client.java
me.shreyasr.connected.desktop.DesktopLauncher.java
me.shreyasr.connected.entities.Laser.java
me.shreyasr.connected.entities.Star.java
me.shreyasr.connected.network.NetworkHandler.java
me.shreyasr.connected.util.MathHelper.java
me.shreyasr.connected.util.SpriteSheet.java