亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

簡易聊天室(Socket實現粗略的Android聊天功能)

發布時間:2020-06-30 14:52:08 來源:網絡 閱讀:889 作者:鷺明 欄目:移動開發

客戶端代碼:


主類:MainActivity.java代碼如下

public class MainActivity extends Activity {


private TextView testview=null;

private Button button=null;

private EditText text=null;

protected Handler handler=null;

private OutputStream out=null;

private Socket s = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

this.testview=(TextView)super.findViewById(R.id.test);

this.button=(Button)super.findViewById(R.id.button);

this.text=(EditText)super.findViewById(R.id.edit);

this.handler=new Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (msg.what==0x123) {

testview.append("客戶端說:"+msg.obj.toString()+"\n");

}

}

};

//4.0之后訪問網絡不能在主程序中進行,要將代碼放在線程中,不然會報錯。

new Thread(new Runnable() {

@Override

public void run() {

try {

s=new Socket("10.0.2.2", 30000);

new Thread(new ClientThread(s, handler)).start();

out=s.getOutputStream();

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

this.button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

try {

out.write((text.getText().toString()+"\n").getBytes("utf-8"));

text.setText("");

}catch (IOException e) {

e.printStackTrace();

}

}

});

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


}


線程類:ClientThread.java代碼如下


public class ClientThread implements Runnable {

private Socket socket=null;

private Handler handler=null;

BufferedReader br=null;

public ClientThread(Socket s,Handler handler) throws IOException {

this.socket=s;

this.handler=handler;

br=new BufferedReader(new InputStreamReader(s.getInputStream()));

}

@Override

public void run() {

try {

String connet=null;

while ((connet=br.readLine())!=null) {

Message message=new Message();

message.what=0x123;

message.obj=connet;

handler.sendMessage(message);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}



服務端代碼:

線程服務類:ServerTherad.java代碼如下

public class ServerTherad implements Runnable {

private Socket s = null;

private BufferedReader buRead = null;

StringBuffer stb=new StringBuffer();

public ServerTherad(Socket s) throws IOException {

this.s = s;

this.buRead = new BufferedReader(new InputStreamReader(

this.s.getInputStream(), "utf-8"));

}


@Override

public void run() {

String connet=null;

try {

while ((connet=readFromClient())!=null) {

//System.out.println("信息\n"+stb.append(connet));

System.out.println("客戶端說:"+connet);

for (Socket ss:SimpleServer.socketList) {

OutputStream out=ss.getOutputStream();

out.write((connet+"\n").getBytes("utf-8"));

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

private String readFromClient(){

try {

return buRead.readLine();

} catch (Exception e) {

//刪除此Socket

SimpleServer.socketList.remove(s);

}

return null;

}


}



服務例子測試:SimpleServer.java代碼如下


public class SimpleServer {

public static ArrayList<Socket> socketList=new ArrayList<Socket>();

public static void main(String[] args) throws IOException {

Scanner sc = new Scanner(System.in);

ServerSocket ss=new ServerSocket(30000);

while (true) {

Socket s=ss.accept();

socketList.add(s);

new Thread(new ServerTherad(s)).start();

}

}


}


注意:測試要先啟動服務端運行,然后再啟動客戶端運行

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

镇远县| 社旗县| 连山| 镇江市| 安陆市| 龙川县| 衡阳县| 南陵县| 科技| 平顶山市| 巴林左旗| 南丹县| 林西县| 景泰县| 西宁市| 淮安市| 彭州市| 玉龙| 义乌市| 日喀则市| 玛纳斯县| 女性| 沙河市| 金阳县| 延安市| 禄劝| 周至县| 通渭县| 民丰县| 麻江县| 耿马| 荔波县| 韶关市| 西峡县| 灵璧县| 家居| 布拖县| 阿拉善左旗| 襄汾县| 晴隆县| 锡林浩特市|