java 服务端
package socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketSingleServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=null;
Socket client=null;
try {
serverSocket=new ServerSocket(8888);
client=serverSocket.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(client.getOutputStream());
DataInputStream dis=new DataInputStream(client.getInputStream());
ServerPush sp=new ServerPush(br, dos);
sp.start();
while (!client.isClosed()) {
String str="";
// if ((str=br.readLine())!=null) {
// dos.writeUTF(str);
// dos.flush();
// }
if ((str=dis.readUTF())!=null) {
dos.writeUTF("fuwuduan:"+str);
dos.flush();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
client.close();
}
}
}
class ServerPush extends Thread{
BufferedReader br;
DataOutputStream dos;
public ServerPush(BufferedReader br,DataOutputStream dos){
this.br=br;
this.dos=dos;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true){
String str="";
try {
if ((str=br.readLine())!=null) {
dos.writeUTF(str);
dos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
安卓端
package com.example.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class SocketActivity extends Activity {
private EditText result;
private Handler handler;
Socket socket = null;
private EditText msg;
DataOutputStream dos = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_socket);
result = (EditText) findViewById(R.id.editText1);
msg = (EditText) findViewById(R.id.editText2);
new Thread() {
public void run() {
// 定義一個數據輸入流,進行讀取里邊的數據
DataInputStream dis = null;
try {
// 通過new一個socket對象,連接到服務器端的端口
socket = new Socket("14.109.110.211", 8888);
dis = new DataInputStream(socket.getInputStream());
String str = "";
while ((str = dis.readUTF()) != null) {
//创建一个Message对象,
Message msg = new Message();
//创建一个
Bundle bundle = new Bundle();
bundle.putString("con", str);
msg.setData(bundle);
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
String con = data.getString("con");
result.setText(result.getText().toString() + "\n" + con);
}
};
}
public void send(View view) {
String s = msg.getText().toString();
try {
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(s);
result.setText(result.getText().toString() + "\n" + s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="128dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:onClick="send"
android:text="send" />
</RelativeLayout>