最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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
@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:orientation="vertical" >
android:layout_
android:orientation="horizontal" >
android:layout_
android:layout_
android:text="10.0.2.2" />
android:id="@+id/test"
android:layout_
android:layout_
android:text="连接测试" />
android:layout_
android:layout_
android:text="" />
android:layout_
android:layout_
android:hint="请在这里输入聊天内容" />
android:id="@+id/fasong"
android:layout_
android:layout_
android:text="发送消息" />
首先运行上节写的服务器程序,然后运行安卓客户端,点击连接测试如下:
运行两个安卓虚拟机如下:
可以实现在线实时聊天了。
相关文章
- sora软件价格介绍 02-22
- sora官网入口地址一览 02-22
- Sora生成的视频使用教程 02-22
- 《梦幻西游》元宵灯谜线索答案大全2024 02-22
- 《原神》有朋自远方来第一天通关攻略 02-22
- 《苏醒之路》四个结局达成攻略 02-22