您的位置:首页 > 移动开发 > Android开发

Android实现PC端作为服务器,手机作为客户端Socket通信

2014-08-25 11:42 337 查看
PC服务端代码如下:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by crab on 14-8-22.
*/
public class PCServer {
private ServerSocket mServer;
private int port = 10086;
private boolean mRunning = false;

public PCServer() throws IOException {
mServer = new ServerSocket(port);
}

public void startServer() {
mRunning = true;
while (mRunning) {
try {
Socket client = mServer.accept();
System.out.println("已经有一个客户端连接上了");
CommniucationThread thread = new CommniucationThread(client);
thread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void setRunning(boolean running) {
mRunning = running;
}

public boolean getRunning() {
return mRunning;
}

public void stopServer() {
mRunning = false;
if (mServer != null) {
try {
mServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
try {
PCServer server = new PCServer();
server.startServer();
} catch (IOException e) {
e.printStackTrace();
}
}

private class CommniucationThread extends Thread {

private Socket mSocket;

public CommniucationThread(Socket socket) {
mSocket = socket;
}

public void run() {
try {
InputStream is = mSocket.getInputStream();
DataInputStream dis = new DataInputStream(is);
String result = null;
while ((result = dis.readUTF()) != null) {
System.out.println("我是客户端,我传来的数据:" + result);
//服务端回答客户端
String messageToClient=new String("You are right.".getBytes(),"utf-8");
sendMessageToClient(messageToClient);
}
System.out.println("这次会话完成,关闭连接了。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("客户端异常关闭");
}finally{
closeCommniucation();
}
}

private void sendMessageToClient (String message) throws IOException {
OutputStream os = mSocket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(message);
dos.flush();
}

public void closeCommniucation() {
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
mSocket=null;
}
}
}
}

Android手机端代码如下:


private class ClientThread extends Thread{
private AtomicBoolean mRunning=new AtomicBoolean(false);
private Socket mClient;
public ClientThread(){

}
private boolean connectServer(){
boolean connect=false;
try {
//请替换成自己的host
mClient=new Socket(InetAddress.getByName("10.1.104.57"),10086);
Log.i("test","客户但连接服务器成功");
connect=true;
} catch (IOException e) {
e.printStackTrace();
}
return connect;
}
public void run(){
if(connectServer()){
mRunning.set(true);
try {
InputStream is=mClient.getInputStream();
DataInputStream dis=new DataInputStream(is);
String result=null;
while((result=dis.readUTF())!=null){
Log.i("test","我是服务器发送来的消息:"+result);
}
Log.i("test","这次与服务器的会话结束");
} catch (IOException e) {
e.printStackTrace();
Log.i("test","与服务的会话异常结束");
}finally {
closeClient();
}
}
}
public void sendMessage(String message){
//如果线程没有在运行,则不发送消息出去
if(!mRunning.get()){
return;
}
try {
OutputStream os = mClient.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(new String(message.getBytes(),"utf-8"));
dos.flush();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeClient(){
mRunning.set(false);
if(mClient!=null){
try {
mClient.close();
} catch (IOException e) {
e.printStackTrace();
}
mClient=null;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: