ServerSocket & Socket

8302 ワード

サーバ:
アプリケーションで起動:
Server1Main.java
package com;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1Main implements Runnable{

	@Override
	public void run() {
		
		
		try {
			int count=0;
			while(true){
				System.out.println("---------------------Start Server---------------------");
				ServerSocket server = new ServerSocket(9999);
				System.out.println("Accept");
				Socket client = server.accept();
				System.out.println("New Accept");
				count++;
				BufferedReader bf = new BufferedReader(new InputStreamReader(client.getInputStream()));
				System.out.println("Info from client: "+bf.readLine());
				
				bf.close();
				client.close();
				server.close();
				System.out.println("End Server:"+count);
				System.out.println("---------------------End Server---------------------

"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void main(String[] args){ Server1Main t = new Server1Main(); t.run(); } }

携帯クライアント
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pandy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <!--   -->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SocketDemoActivity"
            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>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />



    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:text="IP:" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView2"
        android:layout_marginRight="77dp"
        android:text="Port:" />




    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_toLeftOf="@+id/textView3"
        android:layout_toRightOf="@+id/textView1"
        android:text="192.168.0.197"
         />



    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView3"
        android:layout_marginLeft="32dp"
        android:text="9999"
         />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/textView1"
        android:text=" " />



    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="30dp"
        android:text="This a message from client."
         >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="14dp"
        android:text=" :" />

</RelativeLayout>
package com.pandy;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SocketDemoActivity extends Activity {
    /** Called when the activity is first created. */
	private Button button1;
	private EditText editText1,editText2,editText3;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1 = (Button)findViewById(R.id.button1);
        editText1 = (EditText)findViewById(R.id.editText1);
        editText2 = (EditText)findViewById(R.id.editText2);
        editText3 = (EditText)findViewById(R.id.editText3);
        button1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String ip = editText1.getText().toString();
				int port = Integer.parseInt(editText2.getText().toString());
				String message = editText3.getText().toString();
				Socket socket = null;
				try {
					socket = new Socket(ip,port);
					PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
					out.write(message);
					
					out.close();
					socket.close();
					Toast.makeText(SocketDemoActivity.this, "Successful.", Toast.LENGTH_SHORT);
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				
			}
		});
        
        
    }
}