Android客户端与服务器通信

  • Android socket编程
    服务端:
    1、指定端口对serversocket进行事例化
    2、调用ServerSocket的accepet()方法防止在链接期间造成阻塞
    3、获取底层Socket流对并对其进行读写操作
    4、将数据封装成流
    5、对Socket进行读写
    6、关闭所有打开的输入输出流
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.ServerSocket;
import java.net.Socket;
public class Server implements Runnable{
	pubulic void run(){
		try{
		//创建ServerSocket 在端口66788 监听客户请求
		ServerSocket serverSocket = new ServerSocket(66788);
		while(ture){
			//接收客户端的请求
			Socket client = serverSocket.accept();
			System.out.println("already accept");
			try{
				BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
				String str = in.readLine();
				System.out.println("read:" + str);
				//向服务器发送消息
				PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),ture);
				out.println("serever message");
				//关闭流
				out.close();
				in.close();
			}catch (Exception e){
				System.out.println(e.getMessage());
				e.printStackTrace();
			}finally{
				//关闭
				client.close();
				System.out.println("close");
			}
		}
		}catch (Exception e){
			System.out.println(e.getMessage());
		}
	}
	//main函数,开启服务器
	public static void main(String a[]){
		Thread desktopServerThread = new Thread(new Server());
		desktopServerThread.start();
	}
}

  • 客户端
    1、通过IP地址和端口实例化请求连接服务器
    2、获取Socket上的流以进行读写
    3、把流包进BufferedReader / PrintWriter 的事例;
    4、对Socket进行读写
    5、关闭打开流
public class Activity01 extends Activity{
	private final String DEBUG_TAG = "Activity01"private TextView mTextView = null;
	private EditText mEditText = null;
	private Button mButton = null;
	/**Called when the activity is first created.**/
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContantView(R.layout.main);
		mButton = (Button)findViewById(R.id.Button01);
		mTextView = (TextView)findViewById(R.id.TextView01);
		mEditText = (EditText)findViewById(R.id.EditText01);
		//登陆
		mButton.setOnClickListener(new onClickListener()){
			public void onClick(View v){
				Socket socket null;
				String message = mEditText.getText().toString() + "\r\n";
				try{
					//创建Socket
					socket = new Socket("192.168.1.6",66788);
					//向服务器发送消息
					PrintWriter out = new PrintWriter(newBufferedWriter(newOutputStreamWriter(socket.getOutputStream())),ture);
					out,println(message);
					//接收来自服务器的消息
					BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					String msg = br.readLine();
					if(msg != null){
						mTextView.setText(msg);
					else{
						mTextView.setText("数据错误")}
					//关闭流
					out.close();
					br.close();
					//关闭Socket
					socket.close();
				}catch (Exception e){
					//
					Log.e(DEBUG_TAG,e.toString());
					}
				}
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漏洞百出

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值