客戶端代碼:
潼南ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!主類: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之后訪問網(wǎng)絡(luò)不能在主程序中進(jìn)行,要將代碼放在線程中,不然會報錯。
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();
}
}
}
服務(wù)端代碼:
線程服務(wù)類: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;
}
}
服務(wù)例子測試: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();
}
}
}
注意:測試要先啟動服務(wù)端運(yùn)行,然后再啟動客戶端運(yùn)行
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章題目:簡易聊天室(Socket實現(xiàn)粗略的Android聊天功能)-創(chuàng)新互聯(lián)
當(dāng)前URL:http://www.rwnh.cn/article24/csodce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站維護(hù)、網(wǎng)站制作、動態(tài)網(wǎng)站、電子商務(wù)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容