码迷,mamicode.com
首页 > 移动开发 > 详细

android 实现socket通信

时间:2015-05-21 17:30:32      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

客户端代码:

activity:

package com.example.sockettest;


import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

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

public class main extends Activity implements OnClickListener {
    /**
     * Called when the activity is first created.
     */
    private EditText edtmsgcontent;
    private Button btnSend;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        InitView();
    }

    private void InitView() {
        setContentView(R.layout.main);

        edtmsgcontent = (EditText) findViewById(R.id.msgcontent);
        btnSend = (Button) findViewById(R.id.btnsend);

        btnSend.setOnClickListener(this);
    }

    public void onClick(View bt) {

        try {
            String msg = edtmsgcontent.getText().toString();
            if (!TextUtils.isEmpty(msg))
                SendMsg(msg);
            else {
                Toast.makeText(this, "cccc", Toast.LENGTH_LONG);
                edtmsgcontent.requestFocus();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void SendMsg(final String msg) throws UnknownHostException, IOException {
        new Thread() {
            @Override
            public void run() {
                try {
                    String ip = "192.168.1.155";
                    int port = 1818;
                    Socket socket = null;
                    socket = new Socket(ip, port);
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    writer.write(msg);
                    writer.flush();
                    writer.close();
                    socket.close();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="hello"
    />
<EditText android:text="EditText" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/msgcontent"></EditText>
<Button android:layout_height="wrap_content" android:text="Send" android:layout_width="fill_parent" android:id="@+id/btnsend"></Button>
</LinearLayout>
AndroidMenifest.xml文件中添加网络访问权限:

<uses-permission android:name="android.permission.INTERNET"/>


服务器:

不是web工程,直接run main函数就可以了。

package test;

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


public class Server {
	
	public static void main(String[] args){
		BufferedReader in;
		PrintWriter out;
		try{
			ServerSocket ss = new ServerSocket(43211);
			System.out.println("1");
			Socket socket = ss.accept();
			System.out.println("2");
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(),true);
			while(true){
				String line = in.readLine();
				System.out.println(line);
				if(line.equals("88")){
					break;
				}
				out.println("hello: " + line);
				out.flush();
			}
			out.close();
			in.close();
			socket.close();
		}catch(IOException e){
		
		}
	}
			
}
附上源代码链接地址

android 实现socket通信

标签:

原文地址:http://blog.csdn.net/u010127250/article/details/45893133

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!