首页 > 作文

解决Java中socket使用getInputStream()阻塞问题

更新时间:2023-04-04 02:49:25 阅读: 评论:0

目录
socket使用getinputstream()阻塞用线程解决socket的getinputstream阻塞1.背景2.问题3.原因4.解决办法5.socket通信注意事项

socket使用getinputstream()阻塞

今天用socket进行编程练习时,发现程序到了getinputstream()这里就进行不下去了

socket socket = new socket("127.0.0.1", 800);objectinputstream reader = new objectinputstream(socket.getinputstream());system.out.println("a");objectoutputstream writer = new objectoutputstream(socket.getoutputstream());

就这样的一个测试代码,a不会打印出来

后来发现是getinputstream()会一直阻塞在那里阻塞

我把两行代码调了一下就好了,还不太清楚原因,先记下来

socket socket = new socket("127.0.0.1", 800);objectoutputstream writer = new objectoutputstream(socket.getoutputstream());system.out.println("a");objectinputstream reader = new objectinputstream(socket.getinputstream());

用线程解决socket的getinputstream阻塞

1.背景

在socket通信中,当我们希望传输对象时,往往会用到输入/输出对象流。

objectinputstream in=new objectinputstream(socket.getinputstream());objectoutputstre对医护人员的赞美am out=new objectoutputstream(socket.getoutputstream());

2.问题

当程序调用socket.getinputstream()程序被被卡住。

3.原因

socket.getinputstream()方法会导致程序阻塞,直到inputstream收到对方发过来的报文消息,程序才会继续往下执行。

public objectinputstream(inputstream in) throws ioexception的官方api显示:

creates an objectinputstream that reads from the specified inputstream. a rialization stream header is read from the stream and verified. this constructor will block until the corresponding objectoutputstream has written and flushed the header. [1]

4.解决办法

用线程的方式处理输入流。以下为示例代码:

//===============客户端代码 socketclient.java=====================

import java.io.ioexception;import java.io.objectinputstream;import java.io.objectoutputstream;import java.net.socket;import java.net.unknownhostexception;  public class socketclient {private socket socket;private objectoutputstream out;private objectinputstream in;public socketclient(){try {socket=new socket("localhost",8081);out=new objectoutputstream(socket.getoutputstream());readthread readthread=new readthread();readthread.start();} catch (unknownhostexception e) {e.printstacktrace();} catch (ioexception e) {e.print和生stacktrace();}}public void ndmessage(string msg){system.out.println("nd message:"+msg);try {out.writeobject(msg);out.flush();} catch (ioexception e) {e.printstacktrace();}}class readthread extends thread{boolean runflag=true;public void run(){try {in=new objectinputstream(socket.getinputstream());} catch (ioexception e1) {e1.printstacktrace();}while(runflag){if(socket.isclod()){return;}try {object obj=in.readobject();if(obj instanceof string){system.out.println("client recive:"+obj);}} catch (ioexception e) {e.printstacktrace();} catch (classnotfoundexception e) {e.printstacktrace();}}}public void exit(){runflag=fal;}}public static void main(string[] args) {socketclient socketclient=new socketclient();system.out.println("build socketclient");try {thread.sleep(1000);} catch (interruptedexception e) {e.printstacktrace();}socketclient.ndmessage("hello first.");try {thread.sleep(1000);} catch (interruptedexception e) {e.printstacktrace();}socketclient.ndmessage("hello cond.");} }

//============服务器端代码 socketrvice.java===========

import java.io.ioexception;import java.io.objectinputstream;import java.io.objectoutputstream;import java.net.rversocket;import java.net.socket;import java.net.socketexception;import java.util.date; public class socketrvice {rversocket rversocket;publi上海财经大学分数线c socketrvice(){try {rversocket=new rversocket(8081);while(true){socket socket=rversocket.accept();socketrvicethread sst=new socketrvicethread(socket);sst.start();}} catch (ioexception e) {e.printstacktrace();}}class socketrvicethread extends thread{socket socket;objectinputstream in;objectoutputstream out;boolean runflag=true;public socketrvicethread(socket socket){if(null==socket){runflag=fal;re如何赚到100万turn;}this.socket=socket;try {out=new objectoutputstream(socket.getoutputstream());} catch (ioexception e) {e.printstacktrace();}}public void run(){if(null==socket){system.out.println("socket is null");return;}try {in=new objectinputstream(socket.getinputstream());while(runflag){if(socket.isclod()){system.out.println("socket is clod");return;}try {string obj=(string)in.readobject();if(obj instanceof string){system.out.println("rver recive:"+obj);date date=new date();out.writeobject("["+date+"]"+obj);out.flush();}el{system.out.println("rver recive:"+obj);}} catch (classnotfoundexception e) {e.printstacktrace();}catch (socketexception e){e.printstacktrace();return;}catch (ioexception e){e.printstacktrace();}}} catch (ioexception e1) {e1.printstacktrace();return;} catch (exception e){return;}}public void exit(){runflag=fal;}}public static void工作开展情况汇报 main(string[] args) {system.out.println("===============start rvice===============");new socketrvice();} }

5.socket通信注意事项

(1).writexxx()方法后一般用flush()来把缓存内容发送出去。

(2).发送对象时,对象必须串行化,即该对象需要实现rializable接口。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 02:49:24,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/5539a13ed6873198de23890d22258e88.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:解决Java中socket使用getInputStream()阻塞问题.doc

本文 PDF 下载地址:解决Java中socket使用getInputStream()阻塞问题.pdf

标签:对象   代码   程序   用线
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图