我正在将客户端的对象发送到服务器,在服务器端修改该对象并将其重新发送到客户端。将对象形式的客户端发送到服务器可以正常工作,但是当我发送回对象时,它给出异常 Socket is closed。这是代码。 IntString 和 ParentObj 是我发送对象的类。
Client1类:
import java.net.*;
import java.io.*;
public class Client1 {
public static void main(String args[]) {
int arr[] = new int[10];
int length = 6, i, counter_1;
ParentObj obj1;
for (i = 0; i < length; i++) {
arr[i] = i + 10;
}
try {
Socket s = new Socket("localhost", 6789);
IntString obj = new IntString(arr, length);
/*
* OutputStream os = s.getOutputStream();
* ObjectOutputStream oos = new ObjectOutputStream(os);
*
* oos.writeObject(obj);
* oos.close();
* os.close();
*/
Send_recv snd = new Send_recv(s);
snd.sendObj((ParentObj) obj);
if (s.isClosed()) {
System.out.println("Closed");
// s.connect(null);
}
obj1 = snd.recObj();
obj = (IntString) obj1;
if (obj != null) {
for (counter_1 = 0; counter_1 < obj.length_of_row; counter_1++) {
System.out.println(obj.row[counter_1]);
obj.row[counter_1]++;
}
}
// s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
服务器类
import java.net.*;
import java.io.*;
public class Server1 {
public static void main(String args[]) {
IntString obj;
ParentObj obj1;
int port = 6789, counter_1;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
Send_recv rev = new Send_recv(s);
/*
* InputStream is = s.getInputStream();
* ObjectInputStream ois = new ObjectInputStream(is);
* IntString obj = (IntString)ois.readObject();
*/
obj1 = rev.recObj();
obj = (IntString) obj1;
System.out.println(s.getInetAddress());
System.out.println(s.getLocalAddress());
if (obj != null) {
for (counter_1 = 0; counter_1 < obj.length_of_row; counter_1++) {
System.out.println(obj.row[counter_1]);
obj.row[counter_1]++;
}
}
if (ss.isClosed()) {
System.out.println("Closed ss");
}
if (s.isClosed()) {
System.out.println("Closed in Server");
}
Send_recv snd = new Send_recv(s);
snd.sendObj((ParentObj) obj);
} catch (Exception e) {
System.out.println(e + "In Server");
}
}
}
发送和接收对象的 Send_recv 类。
import java.io.*;
import java.net.*;
public class Send_recv {
Socket s;
IntString obj1;
public Send_recv(Socket s) {
this.s = s;
}
public void sendObj(ParentObj obj) {
try {
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
oos.close();
os.close();
} catch (Exception e) {
System.out.println(e);
}
}
public ParentObj recObj() {
try {
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
obj1 = (IntString) ois.readObject();
ois.close();
is.close();
} catch (Exception e) {
System.out.println(e);
}
return (obj1);
}
}
最佳答案
java.net.SocketException socket is closed
此异常意味着您关闭了套接字,然后继续尝试使用它。
os.close();
你在这里关闭了它。关闭 Socket 的输入流或输出流会关闭另一个流和 Socket。
关于java.net.SocketException 套接字已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15239398/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
下面的代码在我第一次运行它时就可以正常工作:require'rubygems'require'spreadsheet'book=Spreadsheet.open'/Users/me/myruby/Mywks.xls'sheet=book.worksheet0row=sheet.row(1)putsrow[1]book.write'/Users/me/myruby/Mywks.xls'当我再次运行它时,我会收到更多消息,例如:/Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:11
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候