当前位置:操作系统 > 安卓/Android >>

用socket简单实现C/S聊天通信

主想想法:在客户端上发送一条信息,在后台开辟一个线程充当服务端,收到消息就立即回馈给客户端。

第一步:创建一个继续Activity的SocketClientActity类,包为com.pku.net

编写布局文件socketclient.xml,代码如下:

 


[html] <?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" > 
 
    <ScrollView 
        android:id="@+id/scrollview3" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
 
        <TextView 
            android:id="@+id/chattxt2" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:background="#98F5FF" /> 
    </ScrollView> 
 
    <EditText 
        android:id="@+id/chattxt" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
 
    <Button 
        android:id="@+id/chatOk" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="发送" > 
    </Button> 
 
</LinearLayout> 
<?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" >

    <ScrollView
        android:id="@+id/scrollview3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/chattxt2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#98F5FF" />
    </ScrollView>

    <EditText
        android:id="@+id/chattxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/chatOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送" >
    </Button>

</LinearLayout>

 

 

接下来编写SocketClientActity.java文件:

 

[java] package com.pku.net; 
 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.net.UnknownHostException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.*; 
 
public class SocketClientActivity extends Activity { 
     
    SocketServerThread yaochatserver; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.socketclient); 
         
         
        try { 
            yaochatserver = new SocketServerThread(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block  
            e.printStackTrace(); 
        } 
        if (yaochatserver != null) { 
            yaochatserver.start(); 
        } 
 
        findviews(); 
        setonclick(); 
    } 
 
    private EditText chattxt; 
    private TextView chattxt2; 
    private Button chatok; 
 
    public void findviews() { 
        chattxt = (EditText) this.findViewById(R.id.chattxt); 
        chattxt2 = (TextView) this.findViewById(R.id.chattxt2); 
        chatok = (Button) this.findViewById(R.id.chatOk); 
    } 
 
    private void setonclick() { 
        chatok.setOnClickListener(new View.OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                try { 补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,