怎么在java中使用SocketChannel實(shí)現(xiàn)一個(gè)客戶端?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的阿克陶網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1.SpringMVC,Spring Web MVC是一種基于Java的實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式的請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架。2.Shiro,Apache Shiro是Java的一個(gè)安全框架。3.Mybatis,MyBatis 是支持普通 SQL查詢,存儲(chǔ)過(guò)程和高級(jí)映射的優(yōu)秀持久層框架。4.Dubbo,Dubbo是一個(gè)分布式服務(wù)框架。5.Maven,Maven是個(gè)項(xiàng)目管理和構(gòu)建自動(dòng)化工具。6.RabbitMQ,RabbitMQ是用Erlang實(shí)現(xiàn)的一個(gè)高并發(fā)高可靠AMQP消息隊(duì)列服務(wù)器。7.Ehcache,EhCache 是一個(gè)純Java的進(jìn)程內(nèi)緩存框架。
1、步驟
(1)創(chuàng)建SocketChannel實(shí)例,并將其配置為非阻塞模式,只有在SocketChannel實(shí)例中,任何I/O操作都是非阻塞的。
(2)使用connect()方法連接服務(wù)器,同時(shí)使用while循環(huán)連續(xù)檢測(cè)和完全連接。在需要立即進(jìn)行I/O操作之前,必須使用finishConnect()來(lái)完成連接過(guò)程。
(3)用ByteBuffer讀寫字節(jié),假如SelectableChannel是一種非阻塞模式,那么它的I/O操作讀寫字節(jié)可能比實(shí)際字節(jié)少,甚至沒(méi)有。因此,我們使用循環(huán)連續(xù)的讀寫來(lái)確保讀寫完成。
2、實(shí)例
public class NonBlockingTCPClient { public static void main(String[] args) { byte[] data = "hello".getBytes(); SocketChannel channel = null; try { // 1. open a socket channel channel = SocketChannel.open(); // adjust to be nonblocking channel.configureBlocking(false); // 2. init connection to server and repeatedly poll with complete // connect() and finishConnect() are nonblocking operation, both return immediately if (!channel.connect(new InetSocketAddress(InetAddress.getLocalHost(), 8899))) { while (!channel.finishConnect()) { System.out.print("."); } } System.out.println("Connected to server..."); ByteBuffer writeBuffer = ByteBuffer.wrap(data); ByteBuffer readBuffer = ByteBuffer.allocate(data.length); int totalBytesReceived = 0; int bytesReceived; // 3. read and write bytes while (totalBytesReceived < data.length) { if (writeBuffer.hasRemaining()) { channel.write(writeBuffer); } if ((bytesReceived = channel.read(readBuffer)) == -1) { throw new SocketException("Connection closed prematurely"); } totalBytesReceived += bytesReceived; System.out.print("."); } System.out.println("Server said: " + new String(readBuffer.array())); } catch (IOException e) { e.printStackTrace(); } finally { // 4 .close socket channel try { if (channel != null) { channel.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
名稱欄目:怎么在java中使用SocketChannel實(shí)現(xiàn)一個(gè)客戶端
文章位置:http://aaarwkj.com/article22/pdppcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、做網(wǎng)站、建站公司、App開發(fā)、網(wǎng)站策劃、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)