简易聊天室(Socket实现粗略的Android聊天功能)-创新互联

客户端代码:

潼南ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信: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之后访问网络不能在主程序中进行,要将代码放在线程中,不然会报错。

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 socketList=new ArrayList();

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();

}

}

}

注意:测试要先启动服务端运行,然后再启动客户端运行

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:简易聊天室(Socket实现粗略的Android聊天功能)-创新互联
标题来源:http://scyanting.com/article/csodce.html