安卓下的TCP通信SOCKET编程

TCPClient

package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.*;
/**
 * 安卓下的Socket基本实现原理
 * 客户端:创建一个Socket对象,指定服务器端的ip地址和端口号,申请连接。
 * 通过输入流InPutStream读取服务端的数据,
 * 通过输出流OutPutStream向服务端写数据
 * @author sofie
 */
public class TCPClient {

	public static void main(String[] args) throws Exception {
		
		Socket s = new Socket("192.168.0.XXXX", 8066);//申请链接- 办公室安卓测试屏
		OutputStream os = s.getOutputStream();
		DataOutputStream dos = new DataOutputStream(os);
		dos.writeUTF("hello server!123456");
		
		DataInputStream dis = new DataInputStream(s.getInputStream());//输入管道
        System.out.println("服务端输出:"+dis.readUTF());
         
		dos.flush(); 
		dos.close();
		s.close();
	  }
}

TCPServer

package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
/**
 * 安卓下的Socket基本实现原理
 * 服务端:首先声明一个ServerSocket对象并指定端口号,
 * 然后调用ServerSocket的accept( )方法接收客户端的数据。
 * accept()方法在没有客户端请求连接之前处于阻塞状态,
 * 一旦接收到连接请求,则通过输入流读取接收的数据。代码实例如下
 * @author sofie
 */
public class TCPServer {
	public static void main(String[] args){
		ServerSocket ss;
		Socket s = null;
		DataInputStream dis = null;
		DataOutputStream dos = null;
		try {
			 ss = new ServerSocket(8066);
	         //不止接受一个客户端
	         s = ss.accept();//接收一个连接
	         dis = new DataInputStream(s.getInputStream());//输入管道
			 dos = new DataOutputStream(s.getOutputStream());
			 //获取节目单数据
			 String data = AndroidDataUtil.ProgramListContext();
			 boolean flag = true;
	         while (flag) {
	           //输出流写入读取的数据	 
	           System.out.println("服务端接收到客户端输出信息:"+dis.readUTF());
        	   dos.writeUTF(data); 
        	   System.out.println("服务端返回给客户端的数据:");
	         }
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
	         try {
				dis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
		       try {
				   s.close();
			   } catch (IOException e) {
				   e.printStackTrace();
			   }finally{
				   try {
					   dos.close();
				   } catch (IOException e) {
					   e.printStackTrace();
				   }
			   }
			}
		}
	}
}

TCPServer-接受客户端发送数据,返回给客户端数据

package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
/**
 * 安卓下的Socket基本实现原理
 * 服务端:首先声明一个ServerSocket对象并指定端口号,
 * 然后调用ServerSocket的accept( )方法接收客户端的数据。
 * accept()方法在没有客户端请求连接之前处于阻塞状态,
 * 一旦接收到连接请求,则通过输入流读取接收的数据。代码实例如下
 * @author sofie
 *
 */
public class TCPServer {
	public static void main(String[] args) {
		ServerSocket ss;
		Socket s = null;
		DataInputStream dis = null;
		DataOutputStream dos = null;
		try {
			 ss = new ServerSocket(8067);
	         //不止接受一个客户端
	         s = ss.accept();//接受一个连接
	         dis = new DataInputStream(s.getInputStream());//输入管道
			 dos = new DataOutputStream(s.getOutputStream());
	         while (true) {
	         System.out.println("服务端输出:"+dis.readUTF());
			 //输出流写入读取的数据
        	 dos.writeUTF("{'test':'123456567'}"); 
        	 System.out.println("返回给客户端的数据:");
	         }
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
	         try {
				dis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
		       try {
				   s.close();
			   } catch (IOException e) {
				   e.printStackTrace();
			   }finally{
				   
				   try {
					   dos.close();
				   } catch (IOException e) {
					   e.printStackTrace();
				   }
			   }
			}
		}
	}
}

TCPClient

package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.*;
/**
 * 安卓下的Socket基本实现原理
 * 客户端:创建一个Socket对象,指定服务器端的ip地址和端口号,申请连接。
 * 通过输入流InPutStream读取服务端的数据,
 * 通过输出流OutPutStream向服务端写数据
 * @author sofie
 */
public class TCPClient {

	public static void main(String[] args) throws Exception {

		Socket s = new Socket("192.168.0.xxxxx", 8066);
		OutputStream os = s.getOutputStream();
		DataOutputStream dos = new DataOutputStream(os);
		dos.writeUTF("hello server!???????123456");
		
		DataInputStream dis = new DataInputStream(s.getInputStream());//输入管道
        System.out.println("服务端输出:"+dis.readUTF());
         
		dos.flush(); 
		dos.close();
		s.close();
	  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值