java关于ServerSocket.accept()的问题

我在写一个利用socket通信的程序,我在Server类(服务器类)中写一个start方法,此方法中用ServerSocket.accept()方法来监听客户机后,调用线程进入等待阶段,直到服务器响应。
现在,我在mian方法中实例化一个服务器类,调用start方法监听客户机,我想在客户机响应之前关掉socket,但由于调用start后调用线程进入了等待状态,因此在mian方法中无法继续操作。

1个回答

 把ServerSocket.accept()放到一个线程中就不堵塞主线程(main)了。
guoguo2234
guoguo2234 回复HCodeRunner: 请问是怎么解决啊,遇到了一样的问题
8 个月之前 回复
HCodeRunner
HCodeRunner 问题已经解决了,谢谢你
3 年多之前 回复
HCodeRunner
HCodeRunner 非常感谢,我试一下
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
java 关于sock通信问题 serverSocket.accept()阻塞
大神们怎么能让 serverSocket.accept()等待一段时间如果没人连接就退出啊 ```public List<Socket> socketAccept(ServerSocket server){ try { long time=System.currentTimeMillis(); while (System.currentTimeMillis()-time<2000) {//10毫秒后退出 Socket socket = server.accept(); sockets.add(socket);//得到socket连接数 } } catch (IOException e) { e.printStackTrace(); try { server.close(); } catch (IOException e1) { e1.printStackTrace(); } } return sockets; } ``` 如果没有连接Socket socket = server.accept();会一直在这等着,有什么办法跳出循环吗
tomcat 无法启动,log如下。
2010-4-5 15:05:57 org.apache.jk.common.ChannelSocket acceptConnections 警告: Exception executing accept java.net.SocketException: select failed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at org.apache.jk.common.ChannelSocket.accept(ChannelSocket.java:307) at org.apache.jk.common.ChannelSocket.acceptConnections(ChannelSocket.java:661) at org.apache.jk.common.ChannelSocket$SocketAcceptor.runIt(ChannelSocket.java:872) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) at java.lang.Thread.run(Unknown Source) 2010-4-5 15:05:57 org.apache.tomcat.util.net.JIoEndpoint$Acceptor run 严重: Socket accept failed java.net.SocketException: select failed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:317) at java.lang.Thread.run(Unknown Source) 2010-4-5 15:05:57 org.apache.jk.common.ChannelSocket acceptConnections 警告: Exception executing accept java.net.SocketException: select failed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at org.apache.jk.common.ChannelSocket.accept(ChannelSocket.java:307) at org.apache.jk.common.ChannelSocket.acceptConnections(ChannelSocket.java:661) at org.apache.jk.common.ChannelSocket$SocketAcceptor.runIt(ChannelSocket.java:872) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) at java.lang.Thread.run(Unknown Source) 2010-4-5 15:05:57 org.apache.tomcat.util.net.JIoEndpoint$Acceptor run 严重: Socket accept failed java.net.SocketException: select failed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:317) at java.lang.Thread.run(Unknown Source) 2010-4-5 15:05:57 org.apache.jk.common.ChannelSocket acceptConnections 警告: Exception executing accept java.net.SocketException: select failed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at org.apache.jk.common.ChannelSocket.accept(ChannelSocket.java:307) at org.apache.jk.common.ChannelSocket.acceptConnections(ChannelSocket.java:661) at org.apache.jk.common.ChannelSocket$SocketAcceptor.runIt(ChannelSocket.java:872) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) at java.lang.Thread.run(Unknown Source) 2010-4-5 15:05:57 org.apache.tomcat.util.net.JIoEndpoint$Acceptor run 严重: Socket accept failed java.net.SocketException: select failed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:317) at java.lang.Thread.run(Unknown Source) 2010-4-5 15:05:57 org.apache.tomcat.util.net.JIoEndpoint$Acceptor run 严重: Socket accept failed java.net.SocketException: select failed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:61) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:317) at java.lang.Thread.run(Unknown Source) 2010-4-5 15:05:57 org.apache.jk.common.ChannelSocket acceptConnections 昨天都还是好好的,今天已启动就不行了,一直不停地循环出现上面的错误信息,小弟我看着头晕呀,百度谷歌都找不到解决方案,还请各位javaEye上的高手给指点指点,小弟我这先谢谢了。
java中Socket的相关问题
ServerSocket serverSocket=new ServerSocket(8989); while(true) { Socket socket = serverSocket.accept(); new Thread(new ServerThread(socket)).start(); } 在这段代码中,如果serverSocket.accept()处于阻塞状态时,程序是否会执行子线程中的代码,如果不能,如何才能让它执行子线程中的代码?求各位大佬们告知一个方法
关于java的socket通信问题
我的项目在自己的电脑上时,可以正常建立连接,接收客户端的消息和主动下发消息到客户端,但是程序部署到服务器后,只能接收客户端发来的消息,而且查看tomcat日志时如下: 2016-07-29 15:18:21 Commons Daemon procrun stdout initialized log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). log4j:WARN Please initialize the log4j system properly. log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). log4j:WARN Please initialize the log4j system properly. socket start log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). log4j:WARN Please initialize the log4j system properly. SocketThread创建socket服务出错 java.net.SocketException: socket closed at java.net.DualStackPlainSocketImpl.accept0(Native Method) at java.net.DualStackPlainSocketImpl.socketAccept(Unknown Source) at java.net.AbstractPlainSocketImpl.accept(Unknown Source) at java.net.PlainSocketImpl.accept(Unknown Source) at java.net.ServerSocket.implAccept(Unknown Source) at java.net.ServerSocket.accept(Unknown Source) at com.deposit.websocket.SocketThread.run(SocketThread.java:39) log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository. log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository. log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.
Socket编程java.net.BindException: Address already in use: JVM_Bind
本人最近想学Socket编程:无奈遇到个问题.不知道是啥原因,还请各位不吝赐教. 问题是这样的:我想用Socket在服务端读取文本文件,第一次运行的时候,什么都没数出来,(不停止第一次开启的服务)第二次输出了文本内容,却报了个地址被占用的错.这是为什么呢? Socket客户端: package socket; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.nio.channels.SocketChannel; public class socketClient { private String ip; public socketClient() { // TODO Auto-generated constructor stub } public socketClient(String ip){ this.ip=ip; } public void client(){ PrintWriter print=null; BufferedReader read=null; File file=null; try { Socket ket=new Socket(ip,9000);//与服务器取得链接. file=new File("e:\\我的地址.txt"); read=new BufferedReader(new InputStreamReader(new FileInputStream(file))); String readLine=null; print=new PrintWriter(ket.getOutputStream(),true); while((readLine=read.readLine())!=null){ print.print(readLine); } } catch (Exception e) { e.printStackTrace(); }finally{ try { read.close(); print.close(); } catch (IOException e) { e.printStackTrace(); } } } } Socket服务器: package socket; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class SocketRemot { private Map map=new HashMap(); public void reMoteRun(){ BufferedReader read=null; PrintWriter write=null; try { while(true){ ServerSocket soc=new ServerSocket(9000);//为服务器Socket指定端口 Socket socket=soc.accept();//服务器端阻塞,一直等待客户端来访问. map.put(socket.getPort()+"",socket); System.out.println("我在等待......."); //读取客户端文件. //得到socket读取文件的流//字节流 /* * InputStream是用于读取二进制文件的流,而服务器端读取的文件很有可能是文本形式的.所以要转换为字符流 * 字节流和字符流之间的转换通过InputStreamReader,和OutputStreamWrite来转换. */ read=new BufferedReader(new InputStreamReader(socket.getInputStream()));//获取读文件的字符流 write=new PrintWriter(socket.getOutputStream(),true);//通过现有的output流来创建新的printWrite流 System.out.println(read.readLine()); // String str=null; // while((str=read.readLine())!=null){ // System.out.println(read.readLine());//边读边写. // } // write.close(); // read.close(); // socket.close(); } } catch (IOException e) { e.printStackTrace(); }finally{ try { if(read!=null){ read.close(); } if(write!=null){ write.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void closeSocket(){ Iterator tor=map.values().iterator(); while(tor.hasNext()){ Socket soc=(Socket)tor.next(); if(!soc.isClosed()){ try { soc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } Test: package socket; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { public static void main(String[] args) { SocketRemot remot=new SocketRemot(); socketClient ket=new socketClient(getAddIp()); remot.reMoteRun(); ket.client(); remot.closeSocket(); } public static String getAddIp(){ String serverIp=null; try { serverIp = InetAddress.getLocalHost().toString(); serverIp = serverIp.substring(serverIp.indexOf("/")+1); System.out.println(serverIp); } catch (UnknownHostException e) { e.printStackTrace(); } return serverIp; } } error: java.net.BindException: Address already in use: JVM_Bind at java.net.DualStackPlainSocketImpl.bind0(Native Method) at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source) at java.net.AbstractPlainSocketImpl.bind(Unknown Source) at java.net.PlainSocketImpl.bind(Unknown Source) at java.net.ServerSocket.bind(Unknown Source) at java.net.ServerSocket.<init>(Unknown Source) at java.net.ServerSocket.<init>(Unknown Source) at socket.SocketRemot.reMoteRun(SocketRemot.java:25) at socket.Test.main(Test.java:10)
java网络编程的小疑惑
在服务器的循环语句中,这段代码只能进行服务器的一次响应 try{ while(true){ **serversocket=new ServerSocket(6000);** socket=serversocket.accept(); Thread workthread=new Thread(new ClientRun(socket)); workthread.start(); } } catch(IOException e){ } finally{ } 当将serversocket=new ServerSocket(6000);取出循环后,服务器就能进行多次响应,这是为啥呢。。。 try{ **serversocket=new ServerSocket(6000);** while(true){ serversocket=new ServerSocket(6000); socket=serversocket.accept(); Thread workthread=new Thread(new ClientRun(socket)); workthread.start(); } } catch(IOException e){ } finally{ }
服务器端accept访问阻塞,导致两个客户端无法同时访问服务器
package server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args){ ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8888); System.out.println("服务器端准备就绪"); while(true){ Socket recieve = serverSocket.accept(); ThreadInServer TS = new ThreadInServer(recieve); TS.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 其中ThreadInServer是新开的线程,用来进行文件传输 当两个客户端同时访问时,会显示“正在等待可用的套接字” 如果我想两个及以上客户端同时访问这个服务器,这里的代码如何改进?
osx eclipse 启动tomcat时报错:严重: Socket accept failed java.net.SocketException: Socket closed
如题,使用mac版eclipse启动 已经关闭ipv6,eclipse连接Oracle可以连通。 控制台信息如下: Init datasource error [design] can not get connection,please check the DBSet 三月 20, 2019 3:53:23 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-127.0.0.1-8099"] 三月 20, 2019 3:53:24 下午 org.apache.catalina.core.StandardService startInternal 信息: Starting service bright 三月 20, 2019 3:53:24 下午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: UAP Server 三月 20, 2019 3:53:25 下午 org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment 信息: No global web.xml found ESA Server starting NTBService6.3 started.~~~~~~ ESA Server started: 51965 三月 20, 2019 3:54:37 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-bio-127.0.0.1-8099"] 三月 20, 2019 3:54:38 下午 org.apache.tomcat.util.net.JIoEndpoint$Acceptor run 严重: Socket accept failed java.net.SocketException: Socket closed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398) at java.net.ServerSocket.implAccept(ServerSocket.java:530) at java.net.ServerSocket.accept(ServerSocket.java:498) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:216) at java.lang.Thread.run(Thread.java:745) 三月 20, 2019 3:54:38 下午 org.apache.tomcat.granite.BrightTomcat start 信息: Server startup in 76169 ms 三月 20, 2019 3:54:38 下午 org.apache.coyote.AbstractProtocol pause 信息: Pausing ProtocolHandler ["http-bio-127.0.0.1-8099"] 三月 20, 2019 3:54:38 下午 org.apache.tomcat.util.net.JIoEndpoint$Acceptor run 严重: Socket accept failed java.net.SocketException: Socket closed at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398) at java.net.ServerSocket.implAccept(ServerSocket.java:530) at java.net.ServerSocket.accept(ServerSocket.java:498) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:216) at java.lang.Thread.run(Thread.java:745) 三月 20, 2019 3:54:38 下午 org.apache.catalina.core.StandardService stopInternal 信息: Stopping service bright ESA Server stopping ESA Server stopped 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [check-offline-msg-valid] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-6] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-7] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-8] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-10] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-9] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-11] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-12] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-13] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-15] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-16] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-17] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-18] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Timer-1] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-20] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-21] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-22] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-23] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-24] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-25] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-26] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-27] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-28] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-29] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-30] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-31] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-32] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-33] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-34] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-35] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-36] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-37] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-38] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-39] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-40] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [transport-check-msg-repeat-thread] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [antesearch-1-thread] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [antesearch-2-thread] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [antesearch-3-thread] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [antesearch-4-thread] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [antesearch-5-thread] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-42] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-44] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-45] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-46] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-47] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-48] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-49] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-50] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 严重: The web application [/] appears to have started a thread named [Thread-51] but has failed to stop it. This is very likely to create a memory leak. 三月 20, 2019 3:54:39 下午 org.apache.coyote.AbstractProtocol stop 信息: Stopping ProtocolHandler ["http-bio-127.0.0.1-8099"] 三月 20, 2019 3:54:39 下午 org.apache.coyote.AbstractProtocol destroy 信息: Destroying ProtocolHandler ["http-bio-127.0.0.1-8099"]
使用TCP编程客户端代码运行起来报错
本人是新手开始学习TCP编程: 如下是客户端的代码 ```package Day23.Internet.TCP; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; //TCP的客户端 public class Demo1Client { //TCP的客户端使用步骤 public static void main(String[] args) throws IOException { //1.建立TCP的服务 Socket socket = new Socket(InetAddress.getLocalHost(), 60000); //2.获取到对应的流对象(获取到Socket 的输出流对象) OutputStream outputStream = socket.getOutputStream(); //3.利用输出流对案发把数据写出即可 outputStream.write("服务器你好".getBytes()); System.out.println("发送了!!"); outputStream.flush(); //4.关闭TCP服务 socket.close(); System.out.println("关闭了客户端!!"); } } ``` 以下是服务器的代码 ```package Day23.Internet.TCP; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; //TCP的服务端 //使用ServerSocket public class Demo1Server { public static void main(String[] args) { try { // 1.建立TCP的服务端,并监听一个端口 ServerSocket serverSocket = new ServerSocket(60000); // 2.获取输入溜对象,读取客户端发送的信息 //ServerSocket并没有下面的方法 但是Socket有呀 // serverSocket. getInputStream() //可以先利用ServerSocet的accept方法获取连接到的Socket对象 Socket socket = serverSocket.accept(); System.out.println("hehe"); InputStream inputStream = socket.getInputStream(); byte[] buf = new byte[1024]; int length = inputStream.read(buf); System.out.println(new String()); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 运行起来后 报错如下(是客户端的问题) ``` Exception in thread "main" java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.<init>(Unknown Source) at java.net.Socket.<init>(Unknown Source) ```
Springboot刚启动就报内存溢出?
1.问题描述: 本地系统是Ubuntu,jdk1.8,工具是eclipse,新创建的最简单的springboot项目,其中只导入了web、config的包。 只写了一个controller,启动就会报错。 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode): "DestroyJavaVM" #39 prio=5 os_prio=0 tid=0x00007f0ae800f000 nid=0x697c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "http-nio-8080-Acceptor-0" #37 daemon prio=5 os_prio=0 tid=0x00007f0ae8224800 nid=0x69ae runnable [0x00007f0ac26ed000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250) - locked <0x000000076f448c40> (a java.lang.Object) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:448) at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:70) at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:95) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-ClientPoller-1" #36 daemon prio=5 os_prio=0 tid=0x00007f0ae821f000 nid=0x69ad runnable [0x00007f0ac27ee000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method) at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269) at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93) at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86) - locked <0x000000076f5c05c0> (a sun.nio.ch.Util$3) - locked <0x000000076f5c05b0> (a java.util.Collections$UnmodifiableSet) - locked <0x000000076f5c0498> (a sun.nio.ch.EPollSelectorImpl) at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97) at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:743) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-ClientPoller-0" #35 daemon prio=5 os_prio=0 tid=0x00007f0ae821d000 nid=0x69ac runnable [0x00007f0ac28ef000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method) at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269) at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93) at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86) - locked <0x000000076f5afb78> (a sun.nio.ch.Util$3) - locked <0x000000076f5afb68> (a java.util.Collections$UnmodifiableSet) - locked <0x000000076f5afa50> (a sun.nio.ch.EPollSelectorImpl) at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97) at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:743) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-10" #34 daemon prio=5 os_prio=0 tid=0x00007f0ae82b5800 nid=0x69ab waiting on condition [0x00007f0ac29f0000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-9" #33 daemon prio=5 os_prio=0 tid=0x00007f0ae82b4000 nid=0x69aa waiting on condition [0x00007f0ac2af1000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-8" #32 daemon prio=5 os_prio=0 tid=0x00007f0ae82b2000 nid=0x69a9 waiting on condition [0x00007f0ac2bf2000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-7" #31 daemon prio=5 os_prio=0 tid=0x00007f0ae82b0000 nid=0x69a8 waiting on condition [0x00007f0ac2cf3000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-6" #30 daemon prio=5 os_prio=0 tid=0x00007f0ae82ae000 nid=0x69a7 waiting on condition [0x00007f0ac2df4000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-5" #29 daemon prio=5 os_prio=0 tid=0x00007f0ae8256800 nid=0x69a6 waiting on condition [0x00007f0ac2ef5000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-4" #28 daemon prio=5 os_prio=0 tid=0x00007f0ae8254800 nid=0x69a5 waiting on condition [0x00007f0ac2ff6000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-3" #27 daemon prio=5 os_prio=0 tid=0x00007f0ae8252800 nid=0x69a4 waiting on condition [0x00007f0ac30f7000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-2" #26 daemon prio=5 os_prio=0 tid=0x00007f0ae8251000 nid=0x69a3 waiting on condition [0x00007f0ac31f8000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "http-nio-8080-exec-1" #25 daemon prio=5 os_prio=0 tid=0x00007f0ae824f800 nid=0x69a2 waiting on condition [0x00007f0ac32f9000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076f59e010> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:107) at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "NioBlockingSelector.BlockPoller-1" #24 daemon prio=5 os_prio=0 tid=0x00007f0ae82e3800 nid=0x69a1 runnable [0x00007f0ac33fa000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method) at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269) at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93) at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86) - locked <0x000000076f44a298> (a sun.nio.ch.Util$3) - locked <0x000000076f44a210> (a java.util.Collections$UnmodifiableSet) - locked <0x000000076f44a090> (a sun.nio.ch.EPollSelectorImpl) at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97) at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:304) "spring.cloud.inetutils" #23 daemon prio=5 os_prio=0 tid=0x00007f0ae82fb800 nid=0x69a0 waiting on condition [0x00007f0ac34fb000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x000000076e593b10> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) "SimplePauseDetectorThread_0" #22 daemon prio=9 os_prio=0 tid=0x00007f0a84015000 nid=0x699f waiting on condition [0x00007f0ac37fc000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at org.LatencyUtils.TimeServices.sleepNanos(TimeServices.java:62) at org.LatencyUtils.SimplePauseDetector$SimplePauseDetectorThread.run(SimplePauseDetector.java:116) "Thread-4" #21 daemon prio=9 os_prio=0 tid=0x00007f0a84011800 nid=0x699e waiting on condition [0x00007f0ac38fd000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x00000006c7b7a928> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442) at org.LatencyUtils.PauseDetector$PauseDetectorThread.run(PauseDetector.java:85) "container-0" #20 prio=5 os_prio=0 tid=0x00007f0ae8c72800 nid=0x699d waiting on condition [0x00007f0ac39fe000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at org.apache.catalina.core.StandardServer.await(StandardServer.java:568) at org.springframework.boot.web.embedded.tomcat.TomcatWebServer$1.run(TomcatWebServer.java:181) "Catalina-utility-2" #19 prio=1 os_prio=0 tid=0x00007f0a68314800 nid=0x699c waiting on condition [0x00007f0ac8dfd000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x00000006c7ab4300> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039) at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088) at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "Catalina-utility-1" #18 prio=1 os_prio=0 tid=0x00007f0ae8c6a800 nid=0x699b waiting on condition [0x00007f0ad0143000] java.lang.Thread.State: TIMED_WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x00000006c7ab4300> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078) at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093) at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809) at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) "RMI TCP Accept-0" #13 daemon prio=5 os_prio=0 tid=0x00007f0ae850a000 nid=0x6995 runnable [0x00007f0ad056d000] java.lang.Thread.State: RUNNABLE at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409) at java.net.ServerSocket.implAccept(ServerSocket.java:545) at java.net.ServerSocket.accept(ServerSocket.java:513) at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(LocalRMIServerSocketFactory.java:52) at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:405) at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:377) at java.lang.Thread.run(Thread.java:748) "RMI TCP Accept-43459" #12 daemon prio=5 os_prio=0 tid=0x00007f0ae84ca000 nid=0x6994 runnable [0x00007f0ad0694000] java.lang.Thread.State: RUNNABLE at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409) at java.net.ServerSocket.implAccept(ServerSocket.java:545) at java.net.ServerSocket.accept(ServerSocket.java:513) at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:405) at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:377) at java.lang.Thread.run(Thread.java:748) "RMI TCP Accept-0" #11 daemon prio=5 os_prio=0 tid=0x00007f0ae84b5000 nid=0x6993 runnable [0x00007f0ad0795000] java.lang.Thread.State: RUNNABLE at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409) at java.net.ServerSocket.implAccept(ServerSocket.java:545) at java.net.ServerSocket.accept(ServerSocket.java:513) at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:405) at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:377) at java.lang.Thread.run(Thread.java:748) "Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00007f0ae80d8000 nid=0x6992 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007f0ae80cc800 nid=0x6991 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007f0ae80ca800 nid=0x6990 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f0ae80c9000 nid=0x698f waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f0ae80c6000 nid=0x698e waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f0ae80c4800 nid=0x698d waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f0ae8091800 nid=0x698c in Object.wait() [0x00007f0ad26de000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000006c701d718> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x00000006c701d718> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) "Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f0ae808f000 nid=0x698b in Object.wait() [0x00007f0ad27df000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000006c7034928> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x00000006c7034928> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "VM Thread" os_prio=0 tid=0x00007f0ae8085800 nid=0x698a runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f0ae8025000 nid=0x6980 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f0ae8026800 nid=0x6981 runnable "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f0ae8028800 nid=0x6982 runnable "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f0ae802a000 nid=0x6983 runnable "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00007f0ae802c000 nid=0x6984 runnable "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00007f0ae802d800 nid=0x6987 runnable "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00007f0ae802f800 nid=0x6988 runnable "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00007f0ae8031000 nid=0x6989 runnable "VM Periodic Task Thread" os_prio=0 tid=0x00007f0ae850c000 nid=0x6996 waiting on condition JNI global references: 1235 Heap PSYoungGen total 144384K, used 45005K [0x000000076d000000, 0x0000000777b00000, 0x00000007c0000000) eden space 126976K, 35% used [0x000000076d000000,0x000000076fbf3608,0x0000000774c00000) from space 17408K, 0% used [0x0000000775d00000,0x0000000775d00000,0x0000000776e00000) to space 17408K, 0% used [0x0000000774c00000,0x0000000774c00000,0x0000000775d00000) ParOldGen total 175616K, used 22110K [0x00000006c7000000, 0x00000006d1b80000, 0x000000076d000000) object space 175616K, 12% used [0x00000006c7000000,0x00000006c85978a8,0x00000006d1b80000) Metaspace used 35192K, capacity 37248K, committed 37504K, reserved 1081344K class space used 4819K, capacity 5214K, committed 5248K, reserved 1048576K 2019-04-01 14:22:57.992 INFO 27001 --- [on(2)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested. 2019-04-01 14:22:57.994 INFO 27001 --- [on(2)-127.0.0.1] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2019-04-01 14:22:58.004 INFO 27001 --- [on(2)-127.0.0.1] o.apache.catalina.core.StandardService : Stopping service [Tomcat] ``` ```
websocket连接java服务器出错
局域网没有问题 但是外网 访问就会报Connection reset Socket s=serverSocket.accept(); InputStream in=s.getInputStream(); byte[] buf=new byte[1024]; int len=in.read(buf);//就是此句报错
java的Socket编程的一个问题
服务器端 package 手写服务器一; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Server { private ServerSocket server; public static void main(String[] args) { Server s=new Server(); s.start(); } //创建客户端 private void start(){ try { server=new ServerSocket(9999); this.receive(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //接收数据 private void receive(){ try { Socket socket=server.accept(); StringBuilder msg=new StringBuilder(); String str=null; BufferedReader reader=new BufferedReader( new InputStreamReader(socket.getInputStream())); while((str=reader.readLine()).length()>0) { msg.append(str); msg.append("\r\n"); } System.out.println(msg.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 客户端 package 客户端; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.Socket; public class Client { public static void main(String[] args) throws IOException{ @SuppressWarnings("resource") Socket socket=new Socket(InetAddress.getLocalHost(),9999); BufferedWriter writer=new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())); writer.write("我是"); writer.newLine(); writer.flush(); } } 报的错为: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at sun.nio.cs.StreamDecoder.readBytes(Unknown Source) at sun.nio.cs.StreamDecoder.implRead(Unknown Source) at sun.nio.cs.StreamDecoder.read(Unknown Source) at java.io.InputStreamReader.read(Unknown Source) at java.io.BufferedReader.fill(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at 手写服务器一.Server.receive(Server.java:34) at 手写服务器一.Server.start(Server.java:20) at 手写服务器一.Server.main(Server.java:14) 在服务器端的接收数据的while那里会出错,这是为什么呢,又应该怎么改进呢,使 服务器可以循环接收,但是但我用浏览器访问服务器端时确不会出错,求大神解答。 主要问一下为什么会出这样的错,改进可以次要。
【小白求教】JAVA多线程跑ServerSocket内存逐渐爆满死机
第一个是教材上正确的代码,可以正常多线程运行的 主程序: package com.server; import java.io.*; import java.util.*; import java.net.*; import com.bean.*; import com.biz.ServerBiz; public class MainServer { private ServerSocket serverSocket; private Socket socket; private InputStream in; private OutputStream out; public MainServer() { //创建服务器的套接字 try { serverSocket=new ServerSocket(8888); System.out.println("服务器已经启动....."); while(true){ socket=serverSocket.accept();//监听并且接收客户端请求 ServerBiz serverBiz=new ServerBiz(socket); Thread td=new Thread(serverBiz);//将serverBiz设置到线程中,每开一个客户端,会创建一个新的线程处理客户端的数据 td.start();//开始 } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { new MainServer(); } } ===================================== package com.biz; import java.io.*; import java.util.*; import java.net.*; import com.bean.*; public class ServerBiz implements Runnable { private Socket socket; private InputStream in; private OutputStream out; public ServerBiz(Socket socket) { this.socket=socket; try { in=socket.getInputStream(); out=socket.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { try { /************接收***************************************/ in=socket.getInputStream(); ObjectInputStream objin=new ObjectInputStream(in); Users us=(Users) objin.readObject(); int code=0;//状态码 if(us!=null){ if(us.getUname()!=null&&!us.getUname().trim().equals("") &&us.getPasswd()!=null&&!us.getPasswd().trim().equals("")){ System.out.println("账号:"+us.getUname()); System.out.println("密码:"+us.getPasswd()); code=1;//登陆成功! } } socket.shutdownInput();//结束接收 /******************************************************/ /*****向客户端发送响应***************************************/ out=socket.getOutputStream(); out.write(code);//向客户端发送状态码 out.flush(); socket.shutdownOutput();//结束输出 /******************************************************/ } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ================================== 以上是教材正确的代码, 我奇怪为什么不能将 socket=serverSocket.accept() 写在runnable中的run方法中,于是我修改成了下面的代码,可是这个是不能正常运行的。 先打开任务管理器,再运行这个程序,可以看到内存在很短的时间内逐渐爆满,从而导致电脑无法使用 主程序: package com.server; import java.net.*; import java.io.*; import com.bean.Users; import com.biz.ServerBiz; public class MainServer { private ServerSocket ss; public MainServer() { try { ss = new ServerSocket(8888); System.out.println("服务器已经启动......"); /************************ 客户端发送来的消息 **************************/ while (true) { ServerBiz sb=new ServerBiz(ss); Thread t=new Thread(sb); t.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { new MainServer(); } } =============================== 下面是设置的Runnable package com.biz; import java.net.*; import java.io.*; import com.bean.Users; public class ServerBiz implements Runnable { private ServerSocket ss; public ServerBiz(ServerSocket ss) { this.ss = ss; } @Override public void run() { try { Socket sk = ss.accept(); InputStream ips = sk.getInputStream(); ObjectInputStream objectin = new ObjectInputStream(ips); Users user = new Users(); user = (Users) objectin.readObject(); int code = 0; if (user != null && user.getPassword() != null && user.getUname() != null && !user.getPassword().trim().equals("") &&!user.getUname().trim().equals("")) { System.out.println("用户名:" + user.getUname()); System.out.println("密码:" + user.getPassword()); code = 1; } sk.shutdownInput(); /**************************************************************/ /************************ 向客户端发送的消息 **************************/ OutputStream ops = sk.getOutputStream(); ops.write(code); ops.flush(); sk.shutdownOutput(); /**************************************************************/ } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ========================= (本来还有个client端,这里忽略) 求大神指点什么将 socket=serverSocket.accept() 写在Runnable中 ,运行后就死机啊,非常感谢啊!
java 通过浏览器访问本地服务器打开一个html文件总是找不到路径
文件路径正确,并且绝对路径和相对路径都已经尝试,为啥一直报相同错误。 ``` package day29; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class BS { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(8888); while (true){ Socket ac = ss.accept(); new Thread(new Runnable() { @Override public void run() { try { InputStream is = ac.getInputStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(is)); String s = bf.readLine(); System.out.println(s); String[] s1 = s.split(" "); String sub = s1[1].substring(1); System.out.println(sub); FileInputStream fis = new FileInputStream(sub); OutputStream os = ac.getOutputStream(); // 写入HTTP协议响应头,固定写法 os.write("HTTP/1.1 200 OK\r\n".getBytes()); os.write("Content-Type:text/html\r\n".getBytes()); // 必须要写入空行,否则浏览器不解析 os.write("\r\n".getBytes()); int len = 0; byte[] b = new byte[1024]; while ((len = fis.read(b)) != -1){ os.write(b); fis.close(); ac.close(); } }catch (IOException E){ E.printStackTrace(); } } }).start(); } } } ``` ![图片说明](https://img-ask.csdn.net/upload/202001/16/1579188286_656266.jpg)
关于java的Socket的一个问题
服务端 package 手写服务器一; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; ////////////////// public class Server { private ServerSocket server; public static void main(String[] args) { Server s=new Server(); s.start(); } //创建客户端 private void start(){ try { server=new ServerSocket(9999); this.receive(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //接收数据 private void receive(){ try { Socket socket=server.accept(); StringBuilder msg=new StringBuilder(); String str=null; BufferedReader reader=new BufferedReader( new InputStreamReader(socket.getInputStream())); while((str=reader.readLine()).length()>0) { msg.append(str); msg.append("\r\n"); } System.out.println(msg.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 客户端 package 客户端; /////////// import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.Socket; //////////// public class Client { public static void main(String[] args) throws IOException{ @SuppressWarnings("resource") Socket socket=new Socket(InetAddress.getLocalHost(),9999); BufferedWriter writer=new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())); writer.write("我是"); writer.newLine(); writer.flush(); } } ////////// 错误为:java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at sun.nio.cs.StreamDecoder.readBytes(Unknown Source) at sun.nio.cs.StreamDecoder.implRead(Unknown Source) at sun.nio.cs.StreamDecoder.read(Unknown Source) at java.io.InputStreamReader.read(Unknown Source) at java.io.BufferedReader.fill(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at 手写服务器一.Server.receive(Server.java:34) at 手写服务器一.Server.start(Server.java:20) at 手写服务器一.Server.main(Server.java:14) /////////////// 客户端访问会报上面的错误,错误在服务端的接收数据的while循环那里; 主要想问一下为什么会报这样的错,改进可以不要, 但是如果我用浏览器访问这个服务器,却不会报错,这是为什么呢, 这里主要问一下为什么会出这样的错,改进可以不要。
socket连接,关于String index out of range:-1?求大神帮忙!
为什么客户端一发消息,服务器端就出现String index out of range:-1? 服务器程序: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class teste implements Runnable { public String latitude,longitude; public int i=0; public void run() { try { //创建ServerSocket ServerSocket serverSocket = new ServerSocket(8080); while (true) { //接受客户端请求 Socket client = serverSocket.accept(); System.out.println("accept"); try { //接收客户端消息 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = in.readLine(); System.out.println(" "+str); in.close(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } finally { //关闭 socket.close(); serverSocket.close(); client.close(); } } } catch (Exception e) { System.out.println(e.getMessage()); } } //main函数,开启服务器 public static void main(String[] args) { Thread desktopServerThread = new Thread(new teste()); desktopServerThread.start(); } } 客户端程序: import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class SocketClient { public static void main(String[] args) { try { Socket socket =new Socket("192.168.1.238",8080); socket.setSoTimeout(60000); PrintWriter printWriter =new PrintWriter(socket.getOutputStream(),true); BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(socket.getInputStream())); String result =""; while(result.indexOf("bye") == -1){ BufferedReader sysBuff =new BufferedReader(new InputStreamReader(System.in)); printWriter.println(sysBuff.readLine()); printWriter.flush(); result = bufferedReader.readLine(); System.out.println("Server say : " + result); } printWriter.close(); bufferedReader.close(); socket.close(); }catch (Exception e) { System.out.println("Exception:" + e); } } }
这段java-socket服务端程序是否稳定?有哪里可以改进?
[b]面对socket客户端每分钟发送一次信息,这段java-socket服务端程序是否稳定?有哪里可以改进?[/b] [code="java"] package test; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ServeSocket{ //private static Logger logger = Logger.getLogger(ServeSocket.class); private Socket socket; private ServerSocket serverSocket; //端口 private int socketPort = 10000; //队列长度 private int queueLength = 10; //IP地址 private String socketAddress = "127.0.0.1"; private ExecutorService executeService = Executors.newFixedThreadPool(10); private BufferedReader br; private FileOutputStream fos; private OutputStreamWriter out; public void serveSocketProgram(){ try { serverSocket = new ServerSocket(socketPort,queueLength,InetAddress.getByName(socketAddress)); while(true){ socket = serverSocket.accept(); executeService.submit(new Runnable() { public void run() { try { br = new BufferedReader(new InputStreamReader(socket.getInputStream())); while(br.read()!=-1){ String message = br.readLine(); System.out.println(message); } } catch (IOException e) { e.printStackTrace(); } finally{ try { br.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { new ServeSocket().serveSocketProgram(); } } [/code]
socket C Java java.io.EOFException
问题是这样的 socket server 是Java实现的 socket 客户端是C语言实现的 当客户端发请求时,server端input.readUTF()时报java.io.EOFException,网上查了,说是读取输入流末尾还读 会报这个错,try{...} catch{java.io.EOFException e}就可以了。但是我添加了后,日志不报错,对方说还是没有响应。说明还是有问题。 不知该怎么解决了。请各位看看。 请求报文为xml格式的String 我本地自己Java写的客户端测试是可以的。 代码如下: public class SocketServer extends Thread implements Runnable { private ServletConfig sce; private WebApplicationContext appConent; //构造函数 public SocketServer(ServletConfig sce,WebApplicationContext appConent){ this.sce=sce; this.appConent=appConent; } public void run() { try{ ServerSocket listener=new ServerSocket(5555); Socket server; while(true){ //开始监听 server=listener.accept() ; NetWorkServer nwServer=new NetWorkServer(server,sce,appConent); nwServer.start(); } } catch (Exception e){ e.printStackTrace(); } } } protected Socket socket; private static final Log log = LogFactory.getLog(NetWorkServer.class); private ServletConfig sce; private WebApplicationContext appConent; public NetWorkServer(Socket sThread,ServletConfig sce,WebApplicationContext appConent){ this.socket =sThread; this.sce=sce; this.appConent=appConent; } public void run() { try{ DataInputStream input = new DataInputStream(socket.getInputStream()); DataOutputStream output = new DataOutputStream(socket.getOutputStream()); //客户端请求传过来的String(这里是可以的,能收到客户端请求的String) String paraInfoXML = input.readUTF(); /*返回结果*/ String result = "" ; SAXReader reader=new SAXReader(); StringReader sr=new StringReader(paraInfoXML); org.xml.sax.InputSource is=new org.xml.sax.InputSource(sr); try { /*业务逻辑*/ Document doc=reader.read(is); } catch (DocumentException e) { e.printStackTrace(); } output.writeUTF(result); output.flush(); output.close(); input.close(); //关闭连接 socket.close(); } catch (IOException e){ e.printStackTrace(); } }
关于Java socket 聊天程序中如何把线程1的文本传到其他线程中
服务器端的代码: package 实验室项目一; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.ServerSocket; import java.net.Socket; import java.io.IOException; import java.net.InetAddress; public class Server { public static void main(String[] args) { try { //1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口 ServerSocket serverSocket=new ServerSocket(10086); Socket socket=null; //记录客户端的数量 int count=0; System.out.println("***服务器即将启动,等待客户端的连接***"); //循环监听等待客户端的连接 while(true){ //调用accept()方法开始监听,等待客户端的连接 socket=serverSocket.accept(); //创建一个新的线程 ServerThread serverThread=new ServerThread(socket); //启动线程 serverThread.start(); count++;//统计客户端的数量 System.out.println("客户端的数量:"+count); InetAddress address=socket.getInetAddress(); System.out.println("当前客户端的IP:"+address.getHostAddress()); } } catch (IOException e) { e.printStackTrace(); } } } package 实验室项目一; /** * 服务器线程处理类 */ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; public class ServerThread extends Thread { // 和本线程相关的Socket Socket socket = null; public ServerThread(Socket socket) { this.socket = socket; } //线程执行的操作,响应客户端的请求 public void run(){ String msg;//客户端的消息 DataInputStream is=null; DataOutputStream os=null; try { //获取输入流,并读取客户端信息 is = new DataInputStream(socket.getInputStream()); msg=is.readUTF(); //获取输出流,响应客户端的请求 os = new DataOutputStream(socket.getOutputStream()); os.writeUTF(msg);//注意 啊,写到这儿不知道怎么把数据传到其他的线程中 } catch (IOException e) { e.printStackTrace(); }finally{ //关闭资源 try { if(is!=null) is.close(); if(socket!=null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
爬虫福利二 之 妹子图网MM批量下载
爬虫福利一:27报网MM批量下载    点击 看了本文,相信大家对爬虫一定会产生强烈的兴趣,激励自己去学习爬虫,在这里提前祝:大家学有所成! 目标网站:妹子图网 环境:Python3.x 相关第三方模块:requests、beautifulsoup4 Re:各位在测试时只需要将代码里的变量 path 指定为你当前系统要保存的路径,使用 python xxx.py 或IDE运行即可。
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、PDF搜索网站推荐 对于大部
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 顺便拉下票,我在参加csdn博客之星竞选,欢迎投票支持,每个QQ或者微信每天都可以投5票,扫二维码即可,http://m234140.nofollow.ax.
Vue + Spring Boot 项目实战(十四):用户认证方案与完善的访问拦截
本篇文章主要讲解 token、session 等用户认证方案的区别并分析常见误区,以及如何通过前后端的配合实现完善的访问拦截,为下一步权限控制的实现打下基础。
比特币原理详解
一、什么是比特币 比特币是一种电子货币,是一种基于密码学的货币,在2008年11月1日由中本聪发表比特币白皮书,文中提出了一种去中心化的电子记账系统,我们平时的电子现金是银行来记账,因为银行的背后是国家信用。去中心化电子记账系统是参与者共同记账。比特币可以防止主权危机、信用风险。其好处不多做赘述,这一层面介绍的文章很多,本文主要从更深层的技术原理角度进行介绍。 二、问题引入  假设现有4个人
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 欢迎 改进 留言。 演示地点跳到演示地点 html代码如下`&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;music&lt;/title&gt; &lt;meta charset="utf-8"&gt
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。 1. for - else 什么?不是 if 和 else 才
数据库优化 - SQL优化
前面一篇文章从实例的角度进行数据库优化,通过配置一些参数让数据库性能达到最优。但是一些“不好”的SQL也会导致数据库查询变慢,影响业务流程。本文从SQL角度进行数据库优化,提升SQL运行效率。 判断问题SQL 判断SQL是否有问题时可以通过两个表象进行判断: 系统级别表象 CPU消耗严重 IO等待严重 页面响应时间过长
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 c/c++ 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7
通俗易懂地给女朋友讲:线程池的内部原理
餐厅的约会 餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”我楞了一下,心里想女朋友今天是怎么了,怎么突然问出这么专业的问题,但做为一个专业人士在女朋友面前也不能露怯啊,想了一下便说:“我先给你讲讲我前同事老王的故事吧!” 大龄程序员老王 老王是一个已经北漂十多年的程序员,岁数大了,加班加不动了,升迁也无望,于是拿着手里
经典算法(5)杨辉三角
写在前面: 我是 扬帆向海,这个昵称来源于我的名字以及女朋友的名字。我热爱技术、热爱开源、热爱编程。技术是开源的、知识是共享的。 这博客是对自己学习的一点点总结及记录,如果您对 Java、算法 感兴趣,可以关注我的动态,我们一起学习。 用知识改变命运,让我们的家人过上更好的生活。 目录一、杨辉三角的介绍二、杨辉三角的算法思想三、代码实现1.第一种写法2.第二种写法 一、杨辉三角的介绍 百度
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹
面试官:你连RESTful都不知道我怎么敢要你?
面试官:了解RESTful吗? 我:听说过。 面试官:那什么是RESTful? 我:就是用起来很规范,挺好的 面试官:是RESTful挺好的,还是自我感觉挺好的 我:都挺好的。 面试官:… 把门关上。 我:… 要干嘛?先关上再说。 面试官:我说出去把门关上。 我:what ?,夺门而去 文章目录01 前言02 RESTful的来源03 RESTful6大原则1. C-S架构2. 无状态3.统一的接
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看
SQL-小白最佳入门sql查询一
一 说明 如果是初学者,建议去网上寻找安装Mysql的文章安装,以及使用navicat连接数据库,以后的示例基本是使用mysql数据库管理系统; 二 准备前提 需要建立一张学生表,列分别是id,名称,年龄,学生信息;本示例中文章篇幅原因SQL注释略; 建表语句: CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // dosho
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,
程序员:我终于知道post和get的区别
IT界知名的程序员曾说:对于那些月薪三万以下,自称IT工程师的码农们,其实我们从来没有把他们归为我们IT工程师的队伍。他们虽然总是以IT工程师自居,但只是他们一厢情愿罢了。 此话一出,不知激起了多少(码农)程序员的愤怒,却又无可奈何,于是码农问程序员。 码农:你知道get和post请求到底有什么区别? 程序员:你看这篇就知道了。 码农:你月薪三万了? 程序员:嗯。 码农:你是怎么做到的? 程序员:
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU
加快推动区块链技术和产业创新发展,2019可信区块链峰会在京召开
      11月8日,由中国信息通信研究院、中国通信标准化协会、中国互联网协会、可信区块链推进计划联合主办,科技行者协办的2019可信区块链峰会将在北京悠唐皇冠假日酒店开幕。   区块链技术被认为是继蒸汽机、电力、互联网之后,下一代颠覆性的核心技术。如果说蒸汽机释放了人类的生产力,电力解决了人类基本的生活需求,互联网彻底改变了信息传递的方式,区块链作为构造信任的技术有重要的价值。   1
程序员把地府后台管理系统做出来了,还有3.0版本!12月7号最新消息:已在开发中有github地址
第一幕:缘起 听说阎王爷要做个生死簿后台管理系统,我们派去了一个程序员…… 996程序员做的梦: 第一场:团队招募 为了应对地府管理危机,阎王打算找“人”开发一套地府后台管理系统,于是就在地府总经办群中发了项目需求。 话说还是中国电信的信号好,地府都是满格,哈哈!!! 经常会有外行朋友问:看某网站做的不错,功能也简单,你帮忙做一下? 而这次,面对这样的需求,这个程序员
Android 9.0系统新特性,对刘海屏设备进行适配
其实Android 9.0系统已经是去年推出的“老”系统了,这个系统中新增了一个比较重要的特性,就是对刘海屏设备进行了支持。一直以来我也都有打算针对这个新特性好好地写一篇文章,但是为什么直到拖到了Android 10.0系统都发布了才开始写这篇文章呢?当然,一是因为我这段时间确实比较忙,今年几乎绝大部分的业余时间都放到写新书上了。但是最主要的原因并不是这个,而是因为刘海屏设备的适配存在一定的特殊性
网易云6亿用户音乐推荐算法
网易云音乐是音乐爱好者的集聚地,云音乐推荐系统致力于通过 AI 算法的落地,实现用户千人千面的个性化推荐,为用户带来不一样的听歌体验。 本次分享重点介绍 AI 算法在音乐推荐中的应用实践,以及在算法落地过程中遇到的挑战和解决方案。 将从如下两个部分展开: AI 算法在音乐推荐中的应用 音乐场景下的 AI 思考 从 2013 年 4 月正式上线至今,网易云音乐平台持续提供着:乐屏社区、UGC
【技巧总结】位运算装逼指南
位算法的效率有多快我就不说,不信你可以去用 10 亿个数据模拟一下,今天给大家讲一讲位运算的一些经典例子。不过,最重要的不是看懂了这些例子就好,而是要在以后多去运用位运算这些技巧,当然,采用位运算,也是可以装逼的,不信,你往下看。我会从最简单的讲起,一道比一道难度递增,不过居然是讲技巧,那么也不会太难,相信你分分钟看懂。 判断奇偶数 判断一个数是基于还是偶数,相信很多人都做过,一般的做法的代码如下
日均350000亿接入量,腾讯TubeMQ性能超过Kafka
整理 | 夕颜出品 | AI科技大本营(ID:rgznai100) 【导读】近日,腾讯开源动作不断,相继开源了分布式消息中间件TubeMQ,基于最主流的 OpenJDK8开发的
8年经验面试官详解 Java 面试秘诀
    作者 | 胡书敏 责编 | 刘静 出品 | CSDN(ID:CSDNnews) 本人目前在一家知名外企担任架构师,而且最近八年来,在多家外企和互联网公司担任Java技术面试官,前后累计面试了有两三百位候选人。在本文里,就将结合本人的面试经验,针对Java初学者、Java初级开发和Java开发,给出若干准备简历和准备面试的建议。   Java程序员准备和投递简历的实
面试官如何考察你的思维方式?
1.两种思维方式在求职面试中,经常会考察这种问题:北京有多少量特斯拉汽车? 某胡同口的煎饼摊一年能卖出多少个煎饼? 深圳有多少个产品经理? 一辆公交车里能装下多少个乒乓球? 一
相关热词 c# clr dll c# 如何orm c# 固定大小的字符数组 c#框架设计 c# 删除数据库 c# 中文文字 图片转 c# 成员属性 接口 c#如何将程序封装 16进制负数转换 c# c#练手项目
立即提问