java IO 实现的socket
public class MyServerSocket { public static void main(String[] args) throws Exception{ ServerSocket ss = new ServerSocket(8888); Socket s = ss.accept(); System.out.println("server=========== accepted connect from " + s.getInetAddress().getHostAddress()); //read from standard input BufferedReader stdReader = new BufferedReader(new InputStreamReader(System.in)); //write to socket PrintWriter socketWriter = new PrintWriter(s.getOutputStream()); //read from socket BufferedReader socketRreader = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = socketRreader.readLine(); while(!line.equals("quit")){ System.out.println("client: " + line); socketWriter.println(stdReader.readLine()); socketWriter.flush(); line = socketRreader.readLine(); Thread.sleep(500); } stdReader.close(); socketWriter.close(); socketRreader.close(); s.close(); ss.close(); } } public class MySocket { public static void main(String[] args) throws Exception{ Socket s = new Socket("localhost", 8888); System.out.println("client=========== connected with server " + s.getInetAddress().getHostAddress()); //read from standard input BufferedReader stdReader = new BufferedReader(new InputStreamReader(System.in)); //write to socket PrintWriter socketWriter = new PrintWriter(s.getOutputStream()); //read from socket BufferedReader socketRreader = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = stdReader.readLine(); while(!line.equals("quit")){ socketWriter.println(line); socketWriter.flush(); System.out.println("server: " + socketRreader.readLine()); line = stdReader.readLine(); Thread.sleep(500); } stdReader.close(); socketWriter.close(); socketRreader.close(); s.close(); } }
java NIO 实现的socket
public class MyServerSocketChannel { public static void main(String[] args) throws Exception{ Selector selector = Selector.open(); ServerSocketChannel ssc = ServerSocketChannel.open();//(new InetSocketAddress(8888)); ssc.bind(new InetSocketAddress(8888)); ssc.configureBlocking(false); ssc.register(selector, SelectionKey.OP_ACCEPT); while(true){ selector.select(); Iterator<SelectionKey> it = selector.selectedKeys().iterator(); while(it.hasNext()){ SelectionKey key = it.next(); if((key.readyOps() & SelectionKey.OP_ACCEPT ) == SelectionKey.OP_ACCEPT){ ServerSocketChannel mychannel = (ServerSocketChannel)key.channel(); SocketChannel sc = mychannel.accept(); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ + SelectionKey.OP_WRITE); System.out.println( "accept from "+sc ); }else{ SocketChannel sc = (SocketChannel)key.channel(); if((key.readyOps() & SelectionKey.OP_READ )== SelectionKey.OP_READ){ System.out.println("received from client: " + SocketUtil.readFromSocketChannel(sc)); } if((key.readyOps() & SelectionKey.OP_WRITE)== SelectionKey.OP_WRITE){ SocketUtil.writeToSocketChannel(sc, "I'm Server "); System.out.println("write to client: "); } } it.remove(); } Thread.sleep(2000); } } } public class MySocketChannel { public static void main(String[] args) throws Exception{ if(args.length==0){ System.out.println("please input your ID! "); return; } SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); sc.connect(new InetSocketAddress("localhost",8888)); while(sc.finishConnect()==false){ System.out.println("connecting... "); Thread.sleep(500); } System.out.println("connected with server!"); while(true){ System.out.println("received from server: " + SocketUtil.readFromSocketChannel(sc)); SocketUtil.writeToSocketChannel(sc, "I'm " + args[0]); System.out.println("wrote to server: "); Thread.sleep(2000); } } } public class MySocketChannel2 { public static void main(String[] args) throws Exception{ ByteBuffer buffer = ByteBuffer.allocate(1024); SocketChannel sc = SocketChannel.open(new InetSocketAddress("localhost", 8888)); ReadableByteChannel chan = Channels.newChannel(System.in); while(true){ chan.read(buffer); buffer.flip(); sc.write(buffer); buffer.clear(); } } } public class SocketUtil { public static String readFromSocketChannel(SocketChannel channel) throws Exception{ ByteBuffer buffer = ByteBuffer.allocate(1024); StringBuffer stringBuffer = new StringBuffer(); while(true){ buffer.clear(); if(channel.read(buffer)<=0) break; stringBuffer.append(new String(buffer.array(), 0, buffer.position())); } return stringBuffer.toString(); } public static void writeToSocketChannel(SocketChannel channel, String str) throws Exception{ byte[] bytes = str.getBytes(); ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); channel.write(buffer); } }
相关推荐
Java IO、NIO以及NIO.2是Java中用于处理输入/输出操作的三种主要机制。本书《Java IO, NIO and NIO.2》旨在深入浅出地介绍这些机制,同时书中内容均为英文。接下来将详细介绍这些知识点。 **Java IO** Java IO是...
Java IO (Input/Output) 和 NIO (Non-blocking Input/Output) 是Java平台中用于处理输入和输出操作的重要部分。这两种技术在实现客户端与服务器之间的通信时起着至关重要的作用。下面将详细介绍Java IO和NIO的特点、...
### Java NIO 实现Socket通信详解 #### 一、NIO与传统IO的区别及优势 在探讨如何使用Java NIO实现Socket通信之前,我们需要先理解NIO(Non-blocking I/O,非阻塞I/O)与传统阻塞I/O之间的区别。 **传统阻塞I/O...
在"JavaIODemo"这个练习中,我们可能会看到以下内容: 1. 流的创建与使用:演示如何创建输入流和输出流对象,例如从文件读取数据并写入到另一个文件。 2. 缓冲技术:使用BufferedReader和BufferedWriter实现高效的...
在深入探讨Java IO与Java NIO之间的区别之前,我们先简单回顾一下这两种I/O模型的基本概念。 #### 1. Java IO(Blocking IO) Java IO,也称为传统的阻塞式IO或同步阻塞式IO,是Java早期版本中的主要I/O处理方式。...
SimpleSocketServer Java IO | NIO样本使用Java IO和NIO设置自己的Socket Server(待办事项) 深入了解Tomcat和Jetty的实现方式(希望如此)
Ioserver java Nio socket 框架 是个不错的NIO 通讯框架,本来想学习mina框架,看了看mina的源码太头痛,本人觉得看懂了Ioserver 再看mina的框架,想多的学习 java NIO 的也可以下载 看看,很值得学习啊!!!
Java I/O, NIO, and NIO.2 is a power-packed book that accelerates your mastery of Java's various I/O APIs. In this book, you'll learn about classic I/O APIs (File, RandomAccessFile, the stream classes ...
not introduced with the other NIO types in Java 1.4 because they depend on the variable arguments capability that was introduced in Java 5.) NIO is missing several features, which were subsequently ...
Java NIO(New IO)是Java 1.4版本引入的一个新特性,它为Java应用程序提供了非阻塞I/O操作的能力,与传统的IO模型(基于流的I/O和基于缓冲区的I/O)相比,NIO具有更高的效率和灵活性。在Java NIO中,数据是以通道...
标题中的“Java socketA_java nio_java socket a”可能是指使用Java NIO实现的Socket通信,这里的"A"可能是表示"Advanced"或"Alternative",意味着比传统的阻塞I/O模型更为高级或替代方案。 在Java Socket API中,...
Java平台的IO机制是软件开发...总的来说,Java的IO机制提供了丰富的选择,开发者可以根据应用场景和需求选择合适的IO模型,以实现高效、可扩展的系统。在实际工作中,深入理解这些概念和机制对于优化系统性能至关重要。
本篇将基于给定的标题“采用NIO实现一个Socket服务器”来详细阐述如何使用NIO进行Socket服务端的开发。 首先,理解NIO的核心概念: 1. **通道(Channel)**:通道是连接到I/O设备的途径,可以读取或写入数据。常见的...
以上就是使用Java Socket编程实现文件上传涉及到的主要知识点,包括Socket通信机制、文件I/O、数据库操作、异常处理、多线程、安全性和性能优化等方面。理解并掌握这些内容,对于开发高效、可靠的文件上传系统至关...
### Java NIO 详细教程知识点解析 #### 一、Java NIO 概述 Java NIO(New IO)是Java平台提供的一种新的IO操作模式,它首次出现在...Java NIO是一种强大的技术,它能够显著提高应用程序在网络IO方面的性能和扩展性。
描述中提到的"用nio想的一个不阻塞NIOSocket例子"可能是一个Java NIO的Socket通信示例,利用NIO的Channel和Selector来实现客户端和服务器之间的非阻塞通信。通常,NIO中的SocketChannel用于网络通信,Selector用于...
在Java编程领域,IO(Input/Output)和NIO(Non-blocking Input/Output)是两种不同的I/O模型,它们在处理数据输入和输出时有着显著的差异。本教程旨在帮助NIO初学者理解这两种模型的核心概念及其实际应用,通过具体...
Java IO和NIO是Java平台中用于输入/输出操作的核心组件。这两个库为开发者提供了处理数据传输的强大工具,无论是在磁盘、网络还是内存中。本合集包含了两本关于这些主题的专业书籍——"Java IO"和"Java NIO",它们...