android socket编程

android中sockect通讯实质属于java socket通讯,在java.net目录下,主要可分为基于tcp协议socket和基于udp协议的DatagramSocket两个方向

网络通讯还离不开输入,输出流的处理 在 java.io/java.nio目录下,其中对于输入输出流的选择,主要看情况选择合适的流进行封装处理。

这里我选择的是DataOutputStream ,DataInputStream主要是因为它们可以直接读取和写入基本数据类型方法,省去转换类型的麻烦。


1,基于tcp协议的socket简单案例:

class Client{
		
	public static final int port=8882;
	private Socket s=null;
	private String str=null;
	private DataOutputStream dos=null;
	private DataInputStream dis=null;
	private boolean bconn=false;
	private Thread th;
	
	public Client(String ip){
		th=new Thread(new Received());
		connect(ip);
		th.start();
	}

	private void send_msg(){
		try {
			dos=new DataOutputStream(s.getOutputStream());
			dos.writeUTF(str);
			dos.flush();
			
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}
	
	public void connect(String ip){
		try {
			s=new Socket(ip, port);		
			bconn=true;
			dis=new DataInputStream(s.getInputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void release_dis(){
		try {
			bconn=false;
			if(dos!=null){
			dos.close();
			dos=null;
			}
			if(dis!=null){
				dis.close();
				dis=null;
			}
			if(s!=null){
			s.close();
			s=null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	private class Received implements Runnable{

		@Override
		public void run() {
			
			try {
				while(bconn){	
					String msg=dis.readUTF();
					Log.e("read msg", msg);
				}
			} catch(SocketException e){
				Log.e("quit msg", "has quit");
			}catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	}


2,基于udp协议的DatagramSocket案例:

	import java.io.ByteArrayInputStream;
	import java.io.ByteArrayOutputStream;
	import java.io.DataInputStream;
	import java.io.DataOutputStream;
	import java.io.IOException;
	import java.net.DatagramPacket;
	import java.net.DatagramSocket;
	import java.net.InetSocketAddress;
	import java.net.Socket;
	import java.net.SocketException;
	import java.net.UnknownHostException;


	public class NetClient {

		String ip;
		private int udp_port;
		private DatagramSocket ds=null;
		
		public void connect(String ip,int port){
			
			this.ip=ip;
			this.udp_port=port;
			try {
				ds=new DatagramSocket(udp_port);
			} catch (SocketException e1) {
				e1.printStackTrace();
			}
			
			new Thread(new UDPThread()).start();
		}

		private class UDPThread implements Runnable{

			byte[] buf=new byte[1024];
			@Override
			public void run() {
				
				while(ds!=null){
					DatagramPacket dp=new DatagramPacket(buf, buf.length);
					try {
						ds.receive(dp);
						parse(dp);
						System.out.println("receive a package from server");
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			private void parse(DatagramPacket dp) {
				ByteArrayInputStream bais = new ByteArrayInputStream(buf, 0, dp.getLength());
				DataInputStream dis = new DataInputStream(bais);
				String msg=null;
				try {
					msg=dis.readUTF();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
	}
		/**
		 * send data package to server
		 * @param ds
		 * @param ip
		 * @param udpPort
		 */
		public void sendMsg(String str) {
			if(ds==null)
				return;
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			DataOutputStream dos=new DataOutputStream(baos);
			
			try {
				dos.write(str.getBytes("UTF-8"));
				dos.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			byte[] buf=baos.toByteArray();
			try {
				DatagramPacket dp=new DatagramPacket(buf, buf.length, new InetSocketAddress(ip, udp_port));
				ds.send(dp);
			} catch (SocketException e) {
				Log.e("udpmsg", e.getMessage());
			} catch (IOException e) {
				Log.e("udpmsg", e.getMessage());
			}
		}



添加网络访问权限

<!-- 访问权限1、访问联网权限 2、访问连接WIFI的权限 3、改变WIFI的权限  4、改变网络的权限 -->

		<uses-permission android:name = "android.permission.INTERNET"/>
		<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
		<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
		<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
		<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>



你可能感兴趣的:(android,socket,tcp,UDP)