i3geek.com
闫庚哲的个人博客

Android 简单示例TCP和UDP传输

TCP服务器端代码

try {
		Boolean endFlag = false;
		ServerSocket ss = new ServerSocket(12345);
		while (!endFlag) 
		{
			// 等待客户端连接                  
			Socket s = ss.accept();                  
			BufferedReader input = new BufferedReader(newInputStreamReader(s.getInputStream()));
			//注意第二个参数据为true将会自动flush,否则需要需要手动操作output.flush()                  
			PrintWriter output = newPrintWriter(s.getOutputStream(),true);                  
			String message = input.readLine();                  
			Log.d("Tcp Demo", "message from Client:"+message);                  
			output.println("message received!");                  
			//output.flush();                  
			if("shutDown".equals(message))
			{
				endFlag=true;                  
			}                  
			s.close();              
		}              
		ss.close();             
	} catch (UnknownHostException e) {             
		e.printStackTrace();          
	} 
	catch (IOException e) {
		e.printStackTrace();          
	}

TCP客户端代码:

try {
		Socket s = new Socket("localhost", 12345);
		// outgoing stream redirect to socket
		OutputStream out = s.getOutputStream();
		// 注意第二个参数据为true将会自动flush,否则需要需要手动操作out.flush()
		PrintWriter output = new PrintWriter(out, true);
		output.println("Hello IdeasAndroid!");
		BufferedReader input = new BufferedReader(newInputStreamReader(s.getInputStream()));
		// read line(s)
		String message = input.readLine();
		Log.d("Tcp Demo", "message From Server:" + message);
		s.close();
	} 
	catch (UnknownHostException e) 
	{              
		e.printStackTrace();          
	} 
	catch (IOException e) 
	{              
		e.printStackTrace();          
	}

UDP服务器端代码:

// UDP服务器监听的端口         
	Integer port = 12345;
	// 接收的字节大小,客户端发送的数据不能超过这个大小       
	byte[] message = new byte[1024];
	try {              
		// 建立Socket连接              
		DatagramSocket datagramSocket = new DatagramSocket(port);              
		DatagramPacket datagramPacket = new DatagramPacket(message,message.length);
		try {                  
			while (true) {
				// 准备接收数据                      
				datagramSocket.receive(datagramPacket);
				Log.d("UDP Demo", datagramPacket.getAddress().getHostAddress().toString() + ":" + new String(datagramPacket.getData()));
				}              
			} catch (IOException e) {
				e.printStackTrace();              
			}          
		} catch (SocketException e) { 
			e.printStackTrace();         
		}

UDP客户端代码:

public static void send(String message) {
		message = (message == null ? "Hello IdeasAndroid!" : message);
		int server_port = 12345;
		DatagramSocket s = null;
		try {             
			s = new DatagramSocket();          
		} catch (SocketException e) {
			e.printStackTrace();          
		}          
		InetAddress local = null;
		try {
			// 换成服务器端IP             
			local = InetAddress.getByName("localhost");          
		} catch (UnknownHostException e) {
			e.printStackTrace();          
		}          
		int msg_length = message.length();
		byte[] messagemessageByte = message.getBytes();
		DatagramPacket p = new DatagramPacket(messageByte, msg_length, local,server_port);
		try {              
			s.send(p);          
		} catch (IOException e) {
			e.printStackTrace();         
		}      
	}
赞(0)
未经允许不得转载:爱上极客 » Android 简单示例TCP和UDP传输
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址