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

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值