android Socket實(shí)現(xiàn)簡(jiǎn)單聊天小程序
android Socket實(shí)現(xiàn)簡(jiǎn)單聊天小程序,供大家參考,具體內(nèi)容如下
服務(wù)器端:
package org.hwq.echo;
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 TalkServer {
public static void main(String[] args) throws IOException{
ServerSocket server = null;
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
try{
server = new ServerSocket(4700);
client = server.accept();
out = new PrintWriter(client.getOutputStream());
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = in.readLine();
while(!"bye".equals(line)){
System.out.println("client:"+line);
out.println("echo:"+line);
out.flush();
line = in.readLine();
}
}catch (Exception e) {
e.printStackTrace();
if(client !=null)
client.close();
if(server != null)
server.close();
}
}
}
手機(jī)端:
package org.hwq.cho;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class EchoActivity extends Activity implements OnClickListener {
EditText show,msg;
Button send;
Handler handler;
Socket client;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new MyHandler();
show = (EditText) findViewById(R.id.show);
msg = (EditText) findViewById(R.id.msg);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
String message = msg.getText().toString();
// System.out.println("msg:"+message);
new EchoThread(EchoActivity.this,message).start();
}
public class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 1:
Toast.makeText(EchoActivity.this, "建立連接失敗", 0).show();
break;
case 2:
String message = (String) msg.obj;
System.out.println("Handler:"+message);
show.append("\n"+message);
break;
}
}
}
private class EchoThread extends Thread{
private Context context;
private String msg;
EchoThread(Context context,String msg){
this.context = context;
this.msg = msg;
}
public void run(){
if(client == null){
try {
client = new Socket("192.168.1.102",4700);
} catch (IOException e) {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}
System.out.println("建立連接");
try{
BufferedReader in;
BufferedReader input;
PrintWriter out;
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
String line = msg;
if(!"bye".equals(line)){
System.out.println("line:"+line);
out.println(line);
out.flush();
String echo = in.readLine();
System.out.println("server:"+echo);
Message message = new Message();
message.obj = echo;
message.what = 2;
handler.sendMessage(message);
}
}catch (Exception e) {
}
}
}
}
注意幾點(diǎn):
1、添加網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
如果沒(méi)添加,無(wú)法使用socket連接網(wǎng)絡(luò)。
2、在新啟線程中不要使用android系統(tǒng)UI界面
在EchoThrad的run()方法里面,有下面代碼:
if(client == null){
try {
client = new Socket("192.168.1.102",4700);
} catch (IOException e) {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}
這里的handler.sendMessage(message);是發(fā)送一個(gè)消息給handler,然后handler根據(jù)消息彈出一個(gè)Toast顯示連接失敗。如果這里直接使用
Toast.makeText(EchoActivity.this, "建立連接失敗", 0).show();
會(huì)報(bào)如下錯(cuò):
Can't create handler inside thread that has not called Looper.prepare()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Socket實(shí)現(xiàn)多個(gè)客戶(hù)端聊天布局
- android使用Socket通信實(shí)現(xiàn)多人聊天應(yīng)用
- Android Socket通信實(shí)現(xiàn)簡(jiǎn)單聊天室
- Android使用Websocket實(shí)現(xiàn)聊天室
- 基于Socket.IO實(shí)現(xiàn)Android聊天功能代碼示例
- android socket聊天室功能實(shí)現(xiàn)
- android Socket實(shí)現(xiàn)簡(jiǎn)單聊天功能以及文件傳輸
- Android 基于Socket的聊天室實(shí)例
- Android基于socket實(shí)現(xiàn)的簡(jiǎn)單C/S聊天通信功能
- Android Socket實(shí)現(xiàn)多個(gè)客戶(hù)端即時(shí)通信聊天
相關(guān)文章
Android組件Glide實(shí)現(xiàn)圖片平滑滾動(dòng)效果
這篇文章主要介紹了Android組件Glide實(shí)現(xiàn)圖片平滑滾動(dòng)效果的相關(guān)資料,具有一定的參考價(jià)值,需要的朋友可以參考下2016-07-07
Android隱藏和沉浸式虛擬按鍵NavigationBar的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Android隱藏和沉浸式虛擬按鍵NavigationBar的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android Flutter實(shí)現(xiàn)圖片滑動(dòng)切換效果
Flutter 為了簡(jiǎn)化開(kāi)發(fā),提供了不少轉(zhuǎn)換動(dòng)畫(huà)組件,這類(lèi)組件通常命名為 xxTransition。本篇要介紹的就是 SlideTransition,并用它實(shí)現(xiàn)圖片滑動(dòng)切換效果,感興趣的可以了解一下2022-04-04
android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(2)
這篇文章主要為大家詳細(xì)介紹了android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android Compose 屬性動(dòng)畫(huà)使用探索詳解
這篇文章主要為大家介紹了Android Compose 屬性動(dòng)畫(huà)使用探索詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android 個(gè)人理財(cái)工具三:添加賬單頁(yè)面 上
本文主要介紹Android 個(gè)人理財(cái)工具添加賬單頁(yè)面的功能實(shí)現(xiàn),這里提供實(shí)例代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-08-08
Android自定義ViewPager實(shí)現(xiàn)個(gè)性化的圖片切換效果
這篇文章主要介紹了Android自定義ViewPager實(shí)現(xiàn)個(gè)性化的圖片切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android本地存儲(chǔ)SharedPreferences詳解
這篇文章主要介紹了Android本地存儲(chǔ)SharedPreferences詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android四大組件之BroadcastReceiver詳解
今天小編就為大家分享一篇關(guān)于Android四大組件之BroadcastReceiver詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01

