下面实现安卓作为客户端实现网络聊聊天室的实例:
建立安卓客户端:
<p> </p><p>package com.example.mysocketclient;</p><p>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;</p><p>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;</p><p>public class MainActivity extends ActionBarActivity {</p><p> private EditText edit, edit_content; private TextView content; private Button test, send;</p><p> @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);</p><p> this.test.setOnClickListener(new Test()); this.send.setOnClickListener(new Send());</p><p> }</p><p> private class Test implements OnClickListener {</p><p> @Override public void onClick(View arg0) {</p><p> connect(); }</p><p> }</p><p> private class Send implements OnClickListener {</p><p> @Override public void onClick(View arg0) {</p><p> send(); }</p><p> }</p><p> // ----------------------------------------</p><p> Socket socket = null; BufferedWriter writer = null; BufferedReader reader = null;</p><p> public void connect() {</p><p> // 线程控制当前数据的读写 AsyncTask<Void, String, Void> read = new AsyncTask<Void, String, Void>() {</p><p> @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);</p><p> } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }</p><p> @Override protected void onProgressUpdate(String... values) { if(values[0].equals("@success")){ Toast.makeText(MainActivity.this, "连接到服务器!", Toast.LENGTH_SHORT).show(); } </p><p> content.append("别人说:"+values[0]); super.onProgressUpdate(values); }</p><p> };</p><p> read.execute();</p><p> }</p><p> 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(); } }</p><p>} </p><p> </p>
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TableRow> <EditText android:id="@+id/edit" android:layout_width="200dp" android:layout_height="wrap_content" android:text="10.0.2.2" /> <Button android:id="@+id/test" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="连接测试" /> </TableRow> </TableLayout> <TextView android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> <EditText android:id="@+id/edit_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请在这里输入聊天内容" /> <Button android:id="@+id/fasong" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送消息" /> </LinearLayout>
首先运行上节写的服务器程序,然后运行安卓客户端,点击连接测试如下:
运行两个安卓虚拟机如下:
可以实现在线实时聊天了。
代码如下:
http://download.csdn.net/detail/yayun0516/8624193
http://download.csdn.net/detail/yayun0516/8624199
时间: 2024-10-16 14:45:22