Android与PC通过Socket进行通信。
PC: Server
Android:Client
服务端程序:
package wu.bu.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
public class Server {
// 监听器对象
private ServerSocket server;
/**
* 实例化,指定监端口
*
* @param port
*/
public Server(int port) {
try {
// 指定监听端口和最大访问数量
server = new ServerSocket(port, 10);
} catch (IOException e) {
e.printStackTrace();
}
}
public void Listener() {
while (true) {
try {
System.out.println("begin");
// 开始监听,等待数据的到来
final Socket socket = server.accept();
// 新的线程
new Thread(new Runnable() {
public void run() {
// 缓冲器
BufferedReader buffer;
try {
buffer = new BufferedReader(new InputStreamReader(
socket.getInputStream(), "UTF-8"));
while (!socket.isClosed()) {
String str;
str = buffer.readLine();
if (str == null)
break;
System.out.println(str);
}
System.out.println("--------------------");
socket.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @vennle
*
* @param args
*/
public static void main(String[] args) {
// 设置5554为服务器监听端口
new Server(5554).Listener();
}
}
client端:
package com.bu.client;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientSocket {
private Socket socket;
private final static String IP = "10.0.129.143";
private final static int PORT = 5554;
public ClientSocket() {
super();
if (socket == null) {
try {
socket = new Socket(IP, PORT);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void send(String msg) {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.println(msg);
out.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void close() {
try {
socket.close();
socket = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
Activity:
package com.bu.client;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SocketClient extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText text = (EditText) this.findViewById(R.id.et_msg);
final Button button = (Button) this.findViewById(R.id.btn_send);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClientSocket socket = new ClientSocket();
String str = text.getText().toString();
socket.send(str);
socket.close();
}
});
}
}