android 聊天室

服务器实现:

public class MzzQQServer {


//端口随便设置,但必须是比1024大
private static final int SERVERPORT = 6666 ;

private static List<Socket> clientList = new ArrayList<Socket>();

//线程池
private ExecutorService executorService = null ;

private ServerSocket server = null ;
public static void main(String[] args) {
new MzzQQServer();
}

public MzzQQServer() {
try {
server = new ServerSocket(SERVERPORT);
//得到一个线程池
executorService = Executors.newCachedThreadPool();
Socket client = null ;
//得到一个socket,此方法是阻塞方法
client = server.accept();
clientList.add(client);
executorService.execute(new ThreadServer(client));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

static class ThreadServer implements Runnable {


private BufferedReader br ;
private PrintWriter pw ;
private Socket socket ;
private String strMsg ;
public ThreadServer(Socket socket) throws IOException {
this.socket = socket ;
br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while((strMsg = br.readLine()) != null) {
sendMessage();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//发送消息给所有客户端
private void sendMessage() throws IOException {
for (Socket socket : clientList) {
pw = new PrintWriter(socket.getOutputStream() , true);
pw.println(strMsg);
}
}

}
}





客户端实现:

Activity ---->

public class MzzQQChatActivity extends Activity{ private EditText chatsView = null ; private EditText content = null ; private Button send = null ; private static final String SERVERIP = "192.168.1.101"; private static final int SERVERPORT = 6666 ; private BufferedReader br = null ; private PrintWriter pw = null ; private Socket socket = null ; private String strMsg = ""; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.chat); chatsView = (EditText)findViewById(R.id.chats_notes); chatsView.setMovementMethod(ScrollingMovementMethod.getInstance()); chatsView.setSelection(chatsView.getText().length(), chatsView.getText().length()); content = (EditText)findViewById(R.id.chat_content); send = (Button)findViewById(R.id.chat_send); try { socket = new Socket(SERVERIP, SERVERPORT); br = new BufferedReader(new InputStreamReader(socket.getInputStream())); pw = new PrintWriter(socket.getOutputStream() , true ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } send.setOnClickListener(new SendListener()); Thread thread = new Thread(runnable); thread.start(); } class SendListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub try { send(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void send() throws Exception { String str = content.getText().toString() + "\n" ; pw.print(str); pw.flush(); content.setText(""); } private Runnable runnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { while((strMsg = br.readLine()) != null) { strMsg += "\n"; handler.sendMessage(handler.obtainMessage()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); chatsView.append(strMsg); } }; }
客户端xml的实现

chat.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/chats_notes" /> <EditText android:layout_width="fill_parent" android:layout_height="250px" android:id="@+id/chats_notes" android:singleLine="false" android:scrollbars="vertical" android:focusable="false" android:enabled="false" android:maxLines="15" android:clickable="true" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:layout_width="180px" android:layout_height="wrap_content" android:id="@+id/chat_content" android:singleLine="true" /> <Button android:layout_width="50px" android:layout_height="wrap_content" android:id="@+id/chat_send" android:text="@string/send" /> </LinearLayout> </LinearLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值