Created
December 6, 2012 11:09
-
-
Save ngsw-taro/4223747 to your computer and use it in GitHub Desktop.
AndroidAdventCalendar2012
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Android(サーバ側) | |
package com.taroid.advent; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
public class MainActivity extends Activity { | |
private final MessageListener mMessageListener = new MessageListener(9876) { | |
@Override | |
protected void onReceivedData(final String data) { | |
Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show(); | |
} | |
}; | |
@Override | |
public void onCreate(final Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mMessageListener.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Android(サーバ側) | |
package com.taroid.advent; | |
import android.os.Handler; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.concurrent.Executors; | |
public abstract class MessageListener { | |
private final int mPort; | |
private final Handler mHandler; | |
private final Runnable mAcceptTask = new Runnable() { | |
public void run() { | |
final ServerSocket serverSocket; | |
try { | |
serverSocket = new ServerSocket(mPort); | |
} catch (final IOException e) { | |
throw new RuntimeException(e); | |
} | |
final Socket socket; | |
try { | |
socket = serverSocket.accept(); | |
} catch (final IOException e) { | |
throw new RuntimeException(e); | |
} finally { | |
try { | |
serverSocket.close(); | |
} catch (final IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
try { | |
final InputStream inputStream = socket.getInputStream(); | |
final String data = readAllLine(inputStream); | |
mHandler.post(new Runnable() { | |
public void run() { | |
onReceivedData(data); | |
} | |
}); | |
} catch (final IOException e) { | |
throw new RuntimeException(e); | |
} finally { | |
try { | |
socket.close(); | |
} catch (final IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
}; | |
public MessageListener(final int port) { | |
mPort = port; | |
mHandler = new Handler(); | |
} | |
public void start() { | |
Executors.newSingleThreadExecutor().execute(mAcceptTask); | |
} | |
protected abstract void onReceivedData(String data); | |
private static String readAllLine(final InputStream inputStream) throws IOException { | |
final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | |
final StringBuilder builder = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
builder.append(line); | |
} | |
return builder.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PC(クライアント側) | |
package com.taroid.sample; | |
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.net.Socket; | |
public class Sample { | |
public static void main(String[] args) throws IOException { | |
try (final Socket socket = new Socket("localhost", 9876)) { | |
final OutputStream outputStream = socket.getOutputStream(); | |
final BufferedWriter writer = new BufferedWriter( | |
new OutputStreamWriter(outputStream)); | |
writer.append("Hello!"); | |
writer.flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment