一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Android基于Socket客户端简单聊天室实例

时间:2015-04-23 编辑:简简单单 来源:一聚教程网

本文我们分享一个在Android下基于Socket客户端简单聊天室实例源码

建立安卓客户端:

package com.example.mysocketclient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

private EditText edit, edit_content;
 private TextView content;
 private Button test, send;

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  this.edit = (EditText) super.findViewById(R.id.edit);
  this.edit_content = (EditText) super.findViewById(R.id.edit_content);
  this.content = (TextView) super.findViewById(R.id.show);
  this.test = (Button) super.findViewById(R.id.test);
  this.send = (Button) super.findViewById(R.id.fasong);

  this.test.setOnClickListener(new Test());
  this.send.setOnClickListener(new Send());

}

private class Test implements OnClickListener {

  @Override
  public void onClick(View arg0) {

   connect();
  }

}

private class Send implements OnClickListener {

  @Override
  public void onClick(View arg0) {

   send();
  }

}

// ----------------------------------------

Socket socket = null;
 BufferedWriter writer = null;
 BufferedReader reader = null;

public void connect() {

 
  // 线程控制当前数据的读写
  AsyncTask read = new AsyncTask() {

   @Override
   protected Void doInBackground(Void... arg0) {
    try {
     socket = new Socket(MainActivity.this.edit.getText()
       .toString(), 12345);
     writer = new BufferedWriter(new OutputStreamWriter(
       socket.getOutputStream()));
     reader = new BufferedReader(new InputStreamReader(
       socket.getInputStream()));
     publishProgress("@success");
    } catch (UnknownHostException e1) {
     Toast.makeText(MainActivity.this, "无法连接服务器!",
       Toast.LENGTH_SHORT).show();
     e1.printStackTrace();
    } catch (IOException e1) {
     Toast.makeText(MainActivity.this, "无法连接服务器!",
       Toast.LENGTH_SHORT).show();
     e1.printStackTrace();
    }
    try {
     String lineString = null;
     while ((lineString = reader.readLine()) != null) {
      publishProgress(lineString);

     }
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    return null;
   }

   @Override
   protected void onProgressUpdate(String... values) {
    if(values[0].equals("@success")){
     Toast.makeText(MainActivity.this, "连接到服务器!", Toast.LENGTH_SHORT).show();
    }
   

    content.append("别人说:"+values[0]);
    super.onProgressUpdate(values);
   }

  };

  read.execute();

}

public void send() {
  try {
   content.append("我说:"+edit_content.getText().toString()+"\n");
   writer.write(edit_content.getText().toString()+"\n");
   writer.flush();
   edit_content.setText("");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}


 




布局文件:


    android:layout_
    android:layout_
    android:orientation="vertical" >

            android:layout_
        android:layout_
        android:orientation="horizontal" >

       

                            android:id="@+id/edit"
                android:layout_
                android:layout_
                android:text="10.0.2.2" />

                            android:id="@+id/test"
                android:layout_
                android:layout_
                android:text="连接测试" />
       

   

            android:id="@+id/show"
        android:layout_
        android:layout_
        android:text="" />

            android:id="@+id/edit_content"
        android:layout_
        android:layout_
        android:hint="请在这里输入聊天内容" />

            android:id="@+id/fasong"
        android:layout_
        android:layout_
        android:text="发送消息" />



首先运行上节写的服务器程序,然后运行安卓客户端,点击连接测试如下:

技术分享


运行两个安卓虚拟机如下:

技术分享


可以实现在线实时聊天了。

热门栏目