1、创建服务器端可以是Web或Java项目,创建类TcpServer:

/**
* @描述 服务器端
* @项目名称 SocketServer
* @包名 com.java.socket.server
* @类名 TCPServer
* @author chenlin
* @date 2012年6月26日 下午4:36:27
* @version 1.0
*/@SuppressWarnings("all")
public class TCPServer {
public static void main(String[] args) {
int port = 10002;
try {
ServerSocket server = new ServerSocket(port);
while (true) {
// 获得客户端连接
// 阻塞式方法
System.out.println("准备阻塞...");
final Socket client = server.accept();
System.out.println("阻塞完成...");

new Thread(new Runnable() {
@Override
public void run() {
try {
// 输入流,为了获取客户端发送的数据
InputStream is = client.getInputStream();

//得到输出流
OutputStream out = client.getOutputStream();

byte[] buffer = new byte[1024];
int len = -1;
System.out.println("准备read...");
while ((len = is.read(buffer)) != -1) {
String text = new String(buffer, 0, len);
System.out.println(text);

//回复信息
out.write("收到信息,over".getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();

}
} catch (Exception e) {
e.printStackTrace();
}
}

2、创建手机端,先写个Socket管理类

/**
* @描述 使用socket实现长连接
* @项目名称 App_Chat
* @包名 com.android.chat.utils
* @类名 TcpUtil
* @author chenlin
* @date 2012年6月26日 下午4:06:43
* @version 1.0
*/
public class TcpManager {
protected static final int STATE_FROM_SERVER_OK = 0;
private static String dsName = "192.168.31.239";
private static int dstPort = 10002;
private static Socket socket;

private static TcpManager instance;

private TcpManager() {
};

public static TcpManager getInstance() {
if (instance == null) {
synchronized (TcpManager.class) {
if (instance == null) {
instance = new TcpManager();
}
}
}
return instance;
}

/**
* 连接
*
* @return
*/
public boolean connect(final Handler handler) {

if (socket == null || socket.isClosed()) {
new Thread(new Runnable() {
@Override
public void run() {

try {
socket = new Socket(dsName, dstPort);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException("连接错误: " + e.getMessage());
}

try {
// 输入流,为了获取客户端发送的数据
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
final String result = new String(buffer, 0, len);

Message msg = Message.obtain();
msg.obj = result;
msg.what = STATE_FROM_SERVER_OK;
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
}

}
}).start();
}

return true;
}

/**
* 发送信息
*
* @param content
*/
public void sendMessage(String content) {
OutputStream os = null;
try {
if (socket != null) {
os = socket.getOutputStream();
os.write(content.getBytes());
os.flush();
}
} catch (IOException e) {
throw new RuntimeException("发送失败:" + e.getMessage());
}
//此处不能关闭
// finally {
// if (os != null) {
// try {
// os.close();
// } catch (IOException e) {
// throw new RuntimeException("未正常关闭输出流:" + e.getMessage());
// }
// }
// }
}

/**
* 关闭连接
*/
public void disConnect() {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException("关闭异常:" + e.getMessage());
}
socket = null;
}
}
}

3、创建页面:

/**
* @描述 Socket通信简易客户端
* @项目名称 SocketClient
* @包名 org.java.socket.client
* @类名 ClientActivity
* @author chenlin
* @date 2012年6月26日 下午4:43:06
* @version 1.0
*/

public class ClientActivity extends Activity {
private EditText mEtContent;
private TcpManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = TcpManager.getInstance();
manager.connect(mHandler);
}

private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case TcpManager.STATE_FROM_SERVER_OK:
String result = (String) msg.obj;
ToastUtil.show(ClientActivity.this, result);
break;

default:
break;
}
};
};

/**
* 发送信息
* @param view
*/
public void clickMessage(View view){
mEtContent = (EditText)findViewById(R.id.et);
if (mEtContent.getText()!= null) {
manager.sendMessage(mEtContent.getText().toString());
}
}

@Override
protected void onDestroy() {
manager.disConnect();
super.onDestroy();
}
}

4、测试:
先运行服务器
然后启动客户端,按“发送消息”按钮,看服务器端是否打印了数据