套接字只能工作一次
我正在编写一个小程序,用于检查在线商店中特定部门的新商品。当它确实发现添加了新项目时,它应该将该信息发送到一个简单的 IRC 机器人,该机器人又将其写在频道中,以便我可以立即看到它。
问题是 IRC 部分只能工作一次。因此,虽然解析器成功地在我的控制台中宣布商店中的每个新项目,但 IRC 机器人仅显示第一个。所以问题很可能是我的套接字代码。
我会告诉你,我是一个完全的套接字菜鸟,所以这可能是一个非常简单的问题(对你来说)。
说得够多了,这里是相关代码:
机器人:
public void openSocket() throws IOException{
serversock = new ServerSocket(1551);
Socket sock = serversock.accept();
BufferedReader incoming = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String inString = incoming.readLine();
sendMessage("#channel", inString);
}
public static void main(String[] args) throws Exception {
XanaduBot bot = new XanaduBot();
bot.setVerbose(true);
bot.connect("irc.server.net");
bot.joinChannel("#channel");
bot.openSocket();
}
以及抓取器上的套接字部分:
if(old == false){
Socket client = new Socket("localhost",1551);
DataOutputStream toBot = new DataOutputStream(client.getOutputStream());
String infoToSend = "New product! "+ info;
toBot.writeBytes(infoToSend + '\n');
toBot.flush();
System.out.println(infoToSend);
}
每当程序发现新项目时,变量 old 就是 false。我知道这里的异常处理非常糟糕,对此感到抱歉!
任何帮助表示赞赏!
I'm coding a little program that checks an online store for new items in a specific department. When it does find that a new item has been added, it's supposed to send that info to a simple IRC-bot, which in turn writes it out in a channel so I can see it immediately.
The problem is that the IRC part only works once. So while the parser successfully announce every new item at the store in my console, the IRC bot only shows the first. So the problem is most likely my socket code.
I shall tell you that I'm a complete noob with sockets, so it can be a really simple problem(for you, that is).
Enough talk, here's the relevant code:
The bot:
public void openSocket() throws IOException{
serversock = new ServerSocket(1551);
Socket sock = serversock.accept();
BufferedReader incoming = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String inString = incoming.readLine();
sendMessage("#channel", inString);
}
public static void main(String[] args) throws Exception {
XanaduBot bot = new XanaduBot();
bot.setVerbose(true);
bot.connect("irc.server.net");
bot.joinChannel("#channel");
bot.openSocket();
}
And the socket part on the scraper:
if(old == false){
Socket client = new Socket("localhost",1551);
DataOutputStream toBot = new DataOutputStream(client.getOutputStream());
String infoToSend = "New product! "+ info;
toBot.writeBytes(infoToSend + '\n');
toBot.flush();
System.out.println(infoToSend);
}
The variable old is false whenever the program finds a new item. And I know that exceptions are handled pretty bad here, sorry for that!
Any help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论