socket

 

 

 

InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
inputStream.read() //返回的是字节编码,流中第一个字符的字节编码
int i = inputStream.read(bytes);//i是返回的读到bs的字节个数
String s = new String(bytes,0,i);//new String(bytes)这样字符串里会有很多空格,空字节数组也转换成了字符串

1KB=1024B=1024byte=8192bit

1,socketClient,socketServer 一旦一方停止连接中断,任何一方把流关闭即中断
2,socket 一边输入即另一边输出,输入耗时太长(获取输入流后打断点)输出一直阻塞
3,客户端输入流没进行输入,服务端输出流会一直阻塞,在while中阻塞,outputStream.close()操作后不再阻塞
4,服务端while监听客户端有outputStream.write操作,服务端即输出,可通过约定字符串比如end再输出(只是打印,流还在未关闭状态)

长度+内容的方式传送
由于inputStream.read()只能读取一个字节最大256,所以当outputStream.write(280)i大于256时,需要两次移位写入
outputStream.write(i >> 8);
outputStream.write(i);
(inputStream.read() << 8)+inputStream.read()读取时两次移位读取

while(-1 != ( n = inputStream.read(bytes))){//bytes不足以放下
System.out.println(n);
sb.append(new String(bytes,0,n));
}


https://www.cnblogs.com/yiwangzhibujian/p/7107785.html

	public static void main(String args[]) throws Exception {
		// 要连接的服务端IP地址和端口
		String host = "127.0.0.1";
		int port = 55533;
		// 与服务端建立连接
		Socket socket = new Socket(host, port);
		// 建立连接后获得输出流
		OutputStream outputStream = socket.getOutputStream();
		String message = "hello123456789";
		int i = 280;
		Scanner scan = new Scanner(System.in);
		while (true) {

			if (scan.hasNext()) {
				message = scan.next();
			}

			outputStream.write((message).getBytes("UTF-8"));
			outputStream.flush();
			if ("end1".equals(message)) {
				outputStream.close();// 关闭之前,服务端会一直阻塞(有流入即流出,没有则阻塞)
			}

			// outputStream.write(i >> 8);//当需要写入一个大于256的数字时,需要分两次移位写入,因为一次最大写入一个字节
			// outputStream.write(i);

		}

		// outputStream.close();
		// socket.close();
	}

	public static void main(String[] args) throws Exception {
		// 监听指定的端口
		int port = 55533;
		ServerSocket server = new ServerSocket(port);

		// server将一直等待连接的到来
		System.out.println("server将一直等待连接的到来");
		Socket socket = server.accept();
		// 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
		InputStream inputStream = socket.getInputStream();
		byte[] bytes = new byte[277];
		int len;
		int n;
		StringBuffer sb = new StringBuffer();
		while (true) {

//			int first = inputStream.read();
//			System.out.println(first);
//			// 如果读取的值为-1 说明到了流的末尾,Socket已经被关闭了,此时将不能再去读取
//			if (first == -1) {
//				break;
//			}
//			int second = inputStream.read();
//			System.out.println(second);
//			int length = (first << 8) + second;
//			System.out.println(length);

			 int i = inputStream.read(bytes);//bytes足以放下
			 String s = "";
			 if(-1 != i){
			 s = new String(bytes,0,i);
			 System.out.println("******"+s);
			 }

			System.out.println("监听inputStream");
		}
//		 inputStream.close();
//		 socket.close();
//		 server.close();
	}

  

	public void server() throws Exception {
		final ServerSocket socket = new ServerSocket(55533); // 1
		try {
			for (;;) {
				System.out.println("wait Accept");
				final Socket clientSocket = socket.accept(); // 2
				System.out.println("Accepted connection from " + clientSocket);
				new Thread(new Runnable() { // 3
							@Override
							public void run() {
								OutputStream out;
								try {
									out = clientSocket.getOutputStream();
									System.out.println("write start");// socket
																		// 一边输入即另一边输出,输入耗时太长(打断点)输出一直阻塞
									out.write("Hi!\r\n".getBytes(Charset
											.forName("UTF-8"))); // 4
																	// out.flush();
									System.out.println("write over");
									clientSocket.close(); // 5
								} catch (IOException e) {
									e.printStackTrace();
									try {
										clientSocket.close();
									} catch (IOException ex) { // ignore on
																// close

									}
								}
							}
						}).start(); // 6
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
        	// 要连接的服务端IP地址和端口
	    String host = "127.0.0.1";
	    // 与服务端建立连接
	    Socket socket = new Socket(host, 55533);
	    InputStream is = socket.getInputStream();
	    byte[] bs = new byte[80];
	    int len = is.read(bs);
	    System.out.println(new String(bs,0,len));

 

posted @ 2019-12-08 18:01  苍天一穹  阅读(106)  评论(0编辑  收藏  举报