|
|
| |
|
|
8³â Àü |
MainActivity.java
package com.example.sockest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
private String return_msg;
private EditText mEt;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
}
public void initData()
{
mEt = (EditText) findViewById(R.id.EditText01);
}
public void onClickBtn(View v)
{
Toast toast = Toast.makeText(getApplicationContext(), mEt.getText().toString(), 0);
toast.show();
TCPclient tcpThread = new TCPclient(mEt.getText().toString());
Thread thread = new Thread(tcpThread);
thread.start();
}
private class TCPclient implements Runnable
{
private static final String serverIP = "¼¹ö ¾ÆÀÌÇÇÁÖ¼Ò ÀÔ·Â(000.000.000.000)";
private static final int serverPort = 9999; //Æ÷Æ®¹øÈ£
private Socket inetSocket = null;
private String msg;
// private String return_msg;
public TCPclient(String _msg)
{
this.msg = _msg;
}
public void run()
{
// TODO Auto-generated method stub
try
{
Log.d("TCP", "C: Connecting...");
inetSocket = new Socket(serverIP ,serverPort );
//inetSocket.connect(socketAddr);
try
{
Log.d("TCP", "C: Sending: '" + msg + "'");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(inetSocket.getOutputStream())), true);
out.println(msg);
BufferedReader in = new BufferedReader(
new InputStreamReader(inetSocket.getInputStream()));
return_msg = in.readLine();
Log.d("TCP", "C: Server send to me this message -->"
+ return_msg);
}
catch (Exception e)
{
Log.e("TCP", "C: Error1", e);
}
finally
{
inetSocket.close();
}
}
catch (Exception e)
{
Log.e("TCP", "C: Error2", e);
}
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="onClickBtn"/>
</LinearLayout>
AndroidManifest.xml
ÀÎÅÍ³Ý »ç¿ëÀ» À§ÇØ ±ÇÇÑ Ãß°¡
<uses-permission android:name="android.permission.INTERNET" />
|
|
̵̧ : 280 |
̵̧
¸ñ·Ï
|
|
| |
|