我求助于 StackOverflow,我一直在用头撞墙。
我正在做一些套接字编程,当我将 out.println("...") 代码行放在 while 循环(in.readLine())之前时,我可以看到它工作正常,但我需要它在循环内。
明确地说,我没有看到任何错误。我只是没有看到文本出现在此应用程序的客户端。它看起来好像在工作,但实际上没有。此外,我也不控制客户端,它由通过 TCP 连接到此 Socket Listener 应用程序的设备处理。有没有可能一读取数据就断开连接?
public class ConnectionHandlerTCP implements Runnable
{
private final Socket clientSocket;
ConnectionHandlerTCP(Socket socket)
{
clientSocket = socket;
}
public void run()
{
int uniqueId = 0;
try (
BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
){
String inputLine;
// This Line WORKS, I see it appear on the other side of the connection.
Log.debug("Sending now A");
out.println("text to send here");
while ((inputLine = in.readLine()) != null)
{
// This Line DOES NOT WORK, I do not see it on the other side of the connection.
//Log.debug("Sending now B");
//out.println("text to send here");
// ... bunch of commented out code here
}
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
Log.error("Exception caught when trying to read input.", e.getMessage());
} catch (Exception e) {
Log.error("Exception caught when trying to parse data.", e.getMessage());
}
}
}
如有任何帮助,我们将不胜感激。如果有帮助,这里是主类。
import java.net.*;
import java.io.*;
public class SocketListenerTCP
{
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
while(true)
{
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
) {
Socket clientSocket = serverSocket.accept();
new Thread(new ConnectionHandlerTCP(clientSocket)).start();
} catch (IOException e) {
Log.error("Exception caught", e.getMessage());
}
}
}
}
我稍微简化了一点,完全去掉了循环,但仍然无法正常工作。
inputLine = in.readLine();
Log.debug(inputLine);
Log.debug("Sending now");
out.println("text to send here");
最佳答案
里面的代码
while ((inputLine = in.readLine()) != null)
很可能没有运行,因为 in.readLine 一直在等待来自客户端的输入。
客户端程序/套接字需要有它在 PrintWriter 和 println 上,以便你的 while 循环可以读取它并响应(假设这就是您想要实现的目标)。
关于Java:无法在套接字 in.readLine() 之后写入套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53877025/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少