使用TCP编程,事先服务器不断地接收客户端的请求

标题:使用TCP编程,事先服务器不断地接收客户端的请求

方式一:客户端

  • 先使用BufferedReader,InputStreamReader读取System.in,存到String,
  • 再使用DateOutputStream写入到流中【os.writeUTF(str);】

服务器端的完整代码:

//测试System.in,客户端不断获取输入,再写到流中;服务器端接收

/**
 * 1.创建ServerSocket(int port)对象
 * 2.在Socket上监听客户端的连接请求
 * 3.阻塞、等待连接的建立
 * 4.接收并处理请求信息
 * 5.将处理结果返回给客户端
 * 6.关闭流和Socket对象
 * @author dell
 *
 */
public class TestTCPServerSystemIn02 {
	public static void main(String[] args) throws IOException {
		System.out.println("-----Server-----");
		//1.创建ServerSocket(int port)对象
		ServerSocket server=new ServerSocket(6666);
		//2.在Socket上监听客户端的连接请求
		Socket client=server.accept();
		//3.阻塞、等待连接的建立  
		System.out.println("建立好了一次连接");
		//4.接收并处理请求信息
		DataInputStream is=new DataInputStream(client.getInputStream());
		String str="";
		while(!(str=is.readUTF()).equals("exit")){
			System.out.println(str);
		}
		 //5.将处理结果返回给客户端
		 //6.关闭流和Socket对象
		System.out.println("接收完毕!!!");
		is.close();
		server.close();
	}
}

客户端的完整代码:

//测试System.in,客户端不断获取输入,再写到流中;服务器端接收
/**
 * 1.创建Socket(String host,int port)对象
 * 2.向服务器发送连接请求
 * 3.向服务器发送服务请求
 * 4.接收服务结果
 * 5.关闭流和Socket对象
 * @author dell
 *
 */
public class TestTCPClientSystemIn02 {
	public static void main(String[] args) throws IOException {
		System.out.println("-----Client-----");
		//1.创建Socket(String host,int port)对象
		//2.向服务器发送连接请求
		Socket client=new Socket("localhost",6666);
		//3.向服务器发送服务请求
		/*
		 * 先使用BufferedReader,InputStreamReader读取System.in,存到String, 
		*  再使用DateOutputStream写入到流中【os.writeUTF(str);】
		*/
		BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
		DataOutputStream os=new DataOutputStream(client.getOutputStream());
		String str="";
		while((str=is.readLine())!=null) {//使用BufferedReader除去\r\n了
			os.writeUTF(str);
			os.flush();
			if(str.equals("exit")) {
				break;
			}
		}
		
		//4.接收服务结果
		//5.关闭流和Socket对象
		System.out.println("发送完毕!!!");
		os.close();
		client.close();
	}
}

方式二、
客户端

  • 先使用BufferedInputStream读取System.in,存到buf字节数组中
  • 【byte[] buf=new byte[1024*60]】
  • 再使用DateOutputStream写入到流中【os.writeByte(str);】

服务器端的完整代码:

//测试System.in,客户端不断获取输入,再写到流中;服务器端接收
/**
 * 方式一:
 * 先使用BufferedReader,InputStreamReader读取System.in,存到String, 
*  再使用DateOutputStream写入到流中【os.writeUTF(str);】
*  方式二:
*  先使用BufferedInputStream读取System.in,存到buf字节数组中
*  【byte[] buf=new byte[1024*60]】
*  再使用DateOutputStream写入到流中【os.writeByte(str);】
*/
/**
 * 1.创建ServerSocket(int port)对象
 * 2.在Socket上监听客户端的连接请求
 * 3.阻塞、等待连接的建立
 * 4.接收并处理请求信息
 * 5.将处理结果返回给客户端
 * 6.关闭流和Socket对象
 * @author dell
 *
 */
public class TestTCPServerSystemIn022 {
	public static void main(String[] args) throws IOException {
		System.out.println("-----Server-----");
		//1.创建ServerSocket(int port)对象
		ServerSocket server=new ServerSocket(6666);
		//2.在Socket上监听客户端的连接请求
		Socket client=server.accept();
		//3.阻塞、等待连接的建立  
		System.out.println("建立好了一次连接");
		//4.接收并处理请求信息
		DataInputStream is=new DataInputStream(client.getInputStream());
		String str="";
		while(!(str=is.readUTF()).equals("exit")){
			System.out.println(str);
		}
		 //5.将处理结果返回给客户端
		 //6.关闭流和Socket对象
		System.out.println("接收完毕!!!");
		is.close();
		server.close();
	}
}

客户端的完整代码:

//测试System.in,客户端不断获取输入,再写到流中;服务器端接收
/*
 * 方式一:
 * 先使用BufferedReader,InputStreamReader读取System.in,存到String, 
*  再使用DateOutputStream写入到流中【os.writeUTF(str);】
*  方式二:
*  先使用BufferedInputStream读取System.in,存到buf字节数组中
*  【byte[] buf=new byte[1024*60]】
*  再使用DateOutputStream写入到流中【os.writeByte(str);】
*/
/**
 * 1.创建Socket(String host,int port)对象
 * 2.向服务器发送连接请求
 * 3.向服务器发送服务请求
 * 4.接收服务结果
 * 5.关闭流和Socket对象
 * @author dell
 *
 */
public class TestTCPClientSystemIn022 {
	public static void main(String[] args) throws IOException {
		System.out.println("-----Client-----");
		//1.创建Socket(String host,int port)对象
		//2.向服务器发送连接请求
		Socket client=new Socket("localhost",6666);
		//3.向服务器发送服务请求
		/**
		 * 方式一:
		 * 先使用BufferedReader,InputStreamReader读取System.in,存到String, 
		*  再使用DateOutputStream写入到流中【os.writeUTF(str);】
		*  方式二:
		*  先使用BufferedInputStream读取System.in,存到buf字节数组中
		*  【byte[] buf=new byte[1024*60]】
		*  再使用DateOutputStream写入到流中【os.writeByte(str);】
		*/
		BufferedInputStream is=new BufferedInputStream(System.in);
		DataOutputStream os=new DataOutputStream(client.getOutputStream());
		byte[] buf=new byte[1024*60];
		int len=0;
		while((len=is.read(buf))!=-1) {//使用BufferedInputStream没有除去\r\n
			String str=new String(buf,0,len-2);
			os.writeUTF(str);
			os.flush();
			if(str.equals("exit")) {
				break;
			}
		}
		
		//4.接收服务结果
		//5.关闭流和Socket对象
		System.out.println("发送完毕!!!");
		os.close();
		client.close();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值