android实现PC到手机的SOCKET通信

以手机为服务器端,PC为客户端

客户端:

 

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		// 创建Socket
		Socket socket = null;
		DataOutputStream out = null;
		BufferedReader br = null;
		inputText.setEditable(false);
		try {
			//创建一个Socket对象,指定服务器端的IP地址和端口号
			socket = new Socket("192.168.137.167", 5000);
			// 客户端给服务器发送消息
			out = new DataOutputStream(socket.getOutputStream());
			String inputCommand = inputText.getText();
			System.out.println(inputCommand);
			if (inputCommand != null) {
				out.writeUTF(inputCommand);
			}
			out.flush();
			// 接收来自服务器的消息

			br = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			String msg = br.readLine(); 
			System.out.println("客户端"+msg);
			showText.append(msg+'\n');
		/*	while ((msg = br.readLine()) != null) {
				
				System.out.println("客户端"+msg);
			} */

		//	socket.close();
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} finally {
			inputText.setEditable(true);
			try {
				if (out != null) {
					out.close();
				}
				if (br != null) {
					br.close();
				}
				if (socket != null) {
					socket.close();
				}
			} catch (IOException e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}
		}

	}


服务器端

package com.chiplight.android.SocketTest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SocketActivity extends Activity {
    /** Called when the activity is first created. */
    private   TextView text_view=null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button StartSeverceButton = (Button)findViewById(R.id.StartSeverce);
        StartSeverceButton.setOnClickListener(StartSeverce_Listener);
        text_view=(TextView) findViewById(R.id.text_command);	
    }
    private Button.OnClickListener StartSeverce_Listener=new Button.OnClickListener(){
    	public void onClick(View v){
    		//在此处添加按钮响应的功能
    		new ServerThread().start();
    	}
    };
    class ServerThread extends Thread{
    	public void run() {
    			//声明一个ServerSocket对象
			ServerSocket serverSocket = null;

			try {
				//创建一个ServerSocket对象,并让这个Socket在4567端口监听
				serverSocket = new ServerSocket(5000);
				Socket client = null;
				OutputStream out = null;
				while (true) {
					try {
						// 监听客户端,调用ServerSocket的accept()方法,接受客户端所发出的请求
						//如果客户端没有发送请求,会阻塞,一直等待accept函数的返回				
						System.out.println("开启服务器!" );
						client = serverSocket.accept();
						System.out.print("服务器成功开启!!");
						//从Socket中得到InputStream对象
						int temp = 0;
						byte[] RecBuff = new byte[1024];
						InputStream inputStream = client.getInputStream();
						System.out.println("server get inputstream" );
						// 从Inputstream当中读取客户端的命令
						temp = inputStream.read(RecBuff);
						for(int i= 0;i<temp;i++)
							System.out.println(RecBuff[i]);
						System.out.println("读取完毕");
						//向客户端发送数据
						byte[] revinfo= {51,52};
						out=(OutputStream) client.getOutputStream();
						out.write(revinfo);
						System.out.println("before send");
						out.flush();
						System.out.println("server send message");
							
					} catch (IOException e) {
						// TODO Auto-generated catch blockg
						e.printStackTrace();
					} finally {
						if (client != null) {
							client.close();
					}

						if (out != null) {
							out.close();
						}
					}
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
    }
    


 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值