很疑惑的一个问题,客户端、服务端同时初始化ObjectOutputStream、ObjectInputStream时就卡住在初始化那行了,下面就是测试的程序,很简单典型的通信;现在的状态是正常的,也就是客户端只用输出流ObjectOutputStream,服务端只用输入流ObjectInputStream,通信正常;但是如果把注释中说明出问题的地方打开,也就是去掉“//”使其生效的话,程序就卡在流初始化那行了,非常困惑,是我有什么概念上的错误??不能同时初始化?有懂的大大一定请指教指教
import java.net.*;
import java.io.*;//要发送的类样例:Factory
class Factory implements Serializable {
public int fid = 111;
}
public class ShowObjOutput {
public static void main(String[] arg) {
try {
ObjectOutputStream os;
//-------打开出问题
//ObjectInputStream is;
Socket sock = new Socket("10.10.1.248", 6000); //panda 为主机名
Factory fa = new Factory();
os = new ObjectOutputStream(new BufferedOutputStream(sock.
getOutputStream()));
///------打开出问题
///is = new ObjectInputStream(new
/// BufferedInputStream(sock.getInputStream()));
os.writeObject(fa);
os.flush();
os.close();
sock.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}//对象输入
import java.net.*;
import java.io.*;public class ShowObjInput {
public static void main(String[] arg) {
try {
ObjectInputStream is;
//------打开出问题
//ObjectOutputStream os;
ServerSocket servSock = new ServerSocket(6000);
Socket sock; sock = servSock.accept();
is = new ObjectInputStream(new
BufferedInputStream(sock.getInputStream()));
///------打开出问题
//os = new ObjectOutputStream(new BufferedOutputStream(sock.
// getOutputStream()));
Factory o = (Factory) is.readObject();
System.out.println("!!!:"+o.fid);
} catch (Exception ex) {System.out.println(ex);}
}
}
import java.net.*;
import java.io.*;//要发送的类样例:Factory
class Factory implements Serializable {
public int fid = 111;
}
public class ShowObjOutput {
public static void main(String[] arg) {
try {
ObjectOutputStream os;
//-------打开出问题
//ObjectInputStream is;
Socket sock = new Socket("10.10.1.248", 6000); //panda 为主机名
Factory fa = new Factory();
os = new ObjectOutputStream(new BufferedOutputStream(sock.
getOutputStream()));
///------打开出问题
///is = new ObjectInputStream(new
/// BufferedInputStream(sock.getInputStream()));
os.writeObject(fa);
os.flush();
os.close();
sock.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}//对象输入
import java.net.*;
import java.io.*;public class ShowObjInput {
public static void main(String[] arg) {
try {
ObjectInputStream is;
//------打开出问题
//ObjectOutputStream os;
ServerSocket servSock = new ServerSocket(6000);
Socket sock; sock = servSock.accept();
is = new ObjectInputStream(new
BufferedInputStream(sock.getInputStream()));
///------打开出问题
//os = new ObjectOutputStream(new BufferedOutputStream(sock.
// getOutputStream()));
Factory o = (Factory) is.readObject();
System.out.println("!!!:"+o.fid);
} catch (Exception ex) {System.out.println(ex);}
}
}
解决方案 »
- 关于字符串交叉判断
- 关于字符串不可修改的问题
- 关于String.format问题
- SWT的多线程问题 + 网络编程
- 如何让表格实现用户只能选取某行,而不能修改单元格的内容的功能
- 我好无助!!!一个文件有两个类,怎么编译阿?
- 大侠救我啊。。。。。在线等待,调试成功,立即给分。
- 请问,在JAVA中如何得到当前CLASS的当前路径?
- 同志们,请把http://expert.csdn.net/Expert/topic/1257/1257981.xml?temp=.5208246的答案写在这里!
- java中能不能让两个new出来的对象能用==判断放回true
- java里的scanf("%d,%s",i,s)是哪个函数?
- 如何搭建一个nightly building系统
ShowObjOutput 顺序问题os = new ObjectOutputStream(new BufferedOutputStream(sock.
getOutputStream()));os.writeObject(fa);
os.flush();
//然后才可 就是获得服务器的返回的意思
is = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
os.close();
sock.close();
is 是获得返回的概念
你写反了~
回mdxk(mdxk) 你说的顺序问题应该没要紧的 否则 线程处理就实现不起来了,我改了下程序是如下,偷懒没用线程,运行ok的,cs双方都正常打印出输出了,但是这里用的是基础的os、is,所以顺序应该不是问题所在。//对象输入
import java.net.*;
import java.io.*;public class ShowObjInput {
public static void main(String[] arg) {
try {
InputStream is;
OutputStream os;
ServerSocket servSock = new ServerSocket(6000);
Socket sock; sock = servSock.accept();
os = sock.getOutputStream();
is = sock.getInputStream();
os.write("54321".getBytes());
os.flush();
byte[] temp = new byte[5];
is.read(temp);
System.out.println(new String(temp));
} catch (Exception ex) {System.out.println(ex);}
}
}import java.net.*;
import java.io.*;public class ShowObjOutput {
public static void main(String[] arg) {
try {
OutputStream os;
InputStream is;
Socket sock = new Socket("10.10.1.248", 6000); //panda 为主机名
Factory fa = new Factory();
os = sock.getOutputStream();
is = sock.getInputStream();
os.write("12345".getBytes());
os.flush();
byte[] temp = new byte[5];
is.read(temp);
System.out.println(new String(temp));
} catch (IOException ex) {
System.out.println(ex);
}
}
}