--------------------客户端--------------------
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
private Socket s;
private EditText ed_url;
private EditText send_msg;
private ScrollView scrollView;
private LinearLayout ll_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_url = (EditText) findViewById(R.id.tvip);
send_msg = (EditText) findViewById(R.id.tvsendmsg);
scrollView = (ScrollView) findViewById(R.id.scrollView1);
ll_content = (LinearLayout) findViewById(R.id.lin);
}
/**
* 开启子线程
*
* @param v
*/
public void connection(View v) {
AsyncTask<Void, String, Void> as = new AsyncTask<Void, String, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
String url=ed_url.getText().toString();
s = new Socket(url, 12321);
System.out.println(s);
// 读取服务发送过来的消息
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
publishProgress(line); // asyncTask提供方法
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
// 把消息显示到界面
String rmsg = values[0];
/**
* 显示到控件
*/
showMsg(rmsg);
super.onProgressUpdate(values);
}
};
as.execute();
}
// 点击发送
public void send(View v) {
try {
String msg = send_msg.getText().toString();
/**
* 发送给服务器,并显示在自己的界面
*/
View v1=View.inflate(this, R.layout.item_mine, null);
TextView tv=(TextView) v1.findViewById(R.id.tvmymsg);
tv.setText(msg);
/**
*
*/
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
ll_content.addView(v1);//把实例化视图,
PrintWriter pw = new PrintWriter(s.getOutputStream()); // 发送消息
pw.println(msg);
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
// 把读取的消息显示到界面上
public void showMsg(String msg) {
View v = View.inflate(this, R.layout.item_others, null);
TextView tvmsg = (TextView) v.findViewById(R.id.othermsg);
tvmsg.setText(msg);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
// 将这个整体的布局,添加到
ll_content.addView(v);
System.out.println("接受到的消息为:" + msg);
}
}
--------------------xml--------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<EditText
android:id="@+id/tvip"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="connection"
android:text="连接服务器" />
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_below="@+id/linearLayout1" >
<LinearLayout
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<EditText
android:id="@+id/tvsendmsg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="send"
android:text="发送" />
</LinearLayout>
</RelativeLayout>
--------------------消息mine--------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tvmymsg"
android:background="@drawable/me_chat_pop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="hellddddddddddddddo" />
</LinearLayout>
--------------------消息other--------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/othermsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/she_chat_pop"
android:text="你好................" />
</LinearLayout>
--------------------服务端--------------------
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static List<Socket> clients = new ArrayList<Socket>();
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12321);
while(true){
Socket socket = serverSocket.accept();
clients.add(socket);
new ServerThread(socket).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--------------------服务器线程--------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread{
private Socket socket;
BufferedReader reader;
public ServerThread(Socket socket) {
super();
this.socket = socket;
try {
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
String readline = null;
try {
while((readline = reader.readLine()) != null){
for (Socket s : Server.clients) {
PrintWriter writer = new PrintWriter(s.getOutputStream());
if(s != socket){
writer.println(readline);
writer.flush();
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}