实现序列化借口就是在对象上加一句implements Serializable 吗?
对内部对象、数据、方法还有其他要求吗?还有我查了JDK的文挡,好象ObjectInputStream真的只能对文件操作,是不是哦?
有谁有用ObjectInputStream处理SOCKET的例子或经验吗?如果真的不能用ObjectInputStream,用DataInputStream可以模拟吗?(全部转成BYTE)
对内部对象、数据、方法还有其他要求吗?还有我查了JDK的文挡,好象ObjectInputStream真的只能对文件操作,是不是哦?
有谁有用ObjectInputStream处理SOCKET的例子或经验吗?如果真的不能用ObjectInputStream,用DataInputStream可以模拟吗?(全部转成BYTE)
是对内部对象、数据、方法还有其他要求吗?
没有还有我查了JDK的文挡,好象ObjectInputStream真的只能对文件操作,是不是哦?
不是如果真的不能用ObjectInputStream,用DataInputStream可以模拟吗?(全部转成BYTE)
肯定能用
真是感谢您我那一段程序:
oin=new ObjectInputStream(socket.getInputStream());
是刚刚创建ObjectInputStream,还没有传输任何对象,就在那里不动了,不是对象没有序列化的原因吧?您做过这样的操作吗(用SOCKET传对象)?有什么需要特别注意的地方呢?最好能给我一段程序,我好好找找其中的差别,多谢了。
你把程序贴出来 我给你调调
Client 的程序:import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Vector;
import java.io.*;
import java.net.*;
import java.util.*;public class Client2
{
static NetEngin NE=new NetEngin();
public static void main(String args[])
{ NE.BeginLink();
}
}class PassPortData implements Serializable
{
static long UsedID=0; //已经使用了的ID
long ID; //编号
boolean Replay=false; //是否是回复数据
long ReplayID=-1; //回复的ID号
String To; //描述是从服务器到客户还是客户到服务器。
Vector data=null; //真正存放数据的地方
String Use; //这个对象的用处
String Conditin; //取得数据的条件
PassPortData()
{
To="Server";
ID=GetNextID();
}
static long GetNextID()
{
if (UsedID==65500)
UsedID=0;
else
UsedID++;
return UsedID;
}
}
class NetEngin
{
static int NowNetState=0;//网络的所处状态
Socket socket=null;
// DataOutputStream dout=null;
DataInputStream din=null;
ObjectInputStream oin=null;
ObjectOutputStream oout=null;
ClientThread thread=null;
PassPortData GetOrder=null;//接受的命令
PassPortData ReplayData=null;//返回的数据
boolean Replayed=false; //已经收到了回复
String BeginLink()
{
String LinkOK;
LinkOK= "Success";
try
{
socket= new Socket("127.0.0.1",5100);
din=new DataInputStream(socket.getInputStream());
System.out.println("Get Here now");
oin=new ObjectInputStream(socket.getInputStream());
System.out.println("Get Here now too");
oout=new ObjectOutputStream(socket.getOutputStream());
}
catch(IOException e)
{
LinkOK= "Fail";
return LinkOK;
}
if (thread==null)
{
thread=new ClientThread();
thread.setPriority(Thread.MIN_PRIORITY);
NowNetState=1;
thread.start();
}
return LinkOK;
} class ClientThread extends Thread
{
PassPortData s=null;
public void run()
{
while (true)
{
try
{
try
{
s=(PassPortData)oin.readObject();
AnalyseData(s);
}
catch (ClassNotFoundException event)
{
System.out.println("不能读出对象,这是我说的。");
}
}
catch (IOException e)
{
System.out.println("error");
break;
}
}
}
}
void AnalyseData(PassPortData s)
{
if(s.Replay&&s.ReplayID==PassPortData.UsedID)
{
ReplayData=s;
Replayed=true;
}
}
void SendToServer(PassPortData datatosend)
{
if(NowNetState==1)//处于连通状?
{
try
{
oout.writeObject(datatosend);
}
catch (IOException e)
{
System.out.println("error");
}
Replayed=false;
}
}
}
//服务器端主程序
package OnLineOrderSystem.Server;import javax.swing.UIManager;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import java.util.Vector;public class Server
{
// static DBEngin DB=new DBEngin();
static NetEnginServer NE=new NetEnginServer();
// public Server()
// {
// ServerFrame frame = new ServerFrame();
// frame.validate();
// //窗口居中
// Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
// Dimension frameSize = frame.getSize();
// if (frameSize.height > screenSize.height)
// {
// frameSize.height = screenSize.height;
// }
// if (frameSize.width > screenSize.width)
// {
// frameSize.width = screenSize.width;
// }
// frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2-20);
// frame.setVisible(true);
// }
public static void main(String[] args)
{
//界面的环境选择
// try
// {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// }
// catch(Exception e)
// {
// e.printStackTrace();
// }
// new Server(); NE.Begin();
}
}
class NetEnginServer extends Thread implements Runnable
{
ServerSocket server=null;
Vector ClientsLinkData=new Vector();
static long ClientID_Used=0;
//ServerThread thread;
Socket sockettry=null;
void Begin()
{
this.start();
}
public void run()
{
while(true)
{
try
{
server=new ServerSocket(5100);
}
catch(IOException e1)
{
}
try
{
sockettry=server.accept();
}
catch(IOException e)
{
System.out.println("本程序已经有一个实例在运行了,请不要打开第二个。");
}
if (sockettry!=null)
{
ClientData cd=new ClientData(sockettry);
ClientsLinkData.add(cd);
}
else
{
continue;
}
}
}
class ClientData
{
long CID; // 客户的编号
String CIP; // 客户的IP
ServerThread thread; // 监听的线程
int NowNetState=0; // 所处的状态
ClientData(Socket socket)
{
CIP=socket.getLocalAddress().getHostAddress();
CID=NetEnginServer.ClientID_Used++;
thread=new ServerThread(socket);
thread.start();
NowNetState=1;
}
public class ServerThread extends Thread
{
Socket socket;
ObjectInputStream oin=null;
ObjectOutputStream oout=null;
PassPortData s=null;
ServerThread(Socket t)
{
socket=t;
try
{
oin=new ObjectInputStream(socket.getInputStream());
oout=new ObjectOutputStream(socket.getOutputStream());
}
catch(IOException e)
{
}
}
public void run()
{
PassPortData s=null;
while(true)
{
try
{
try
{
s=(PassPortData)oin.readObject();
//AnalyseData(s);
}
catch (ClassNotFoundException event)
{
System.out.println("不能读出对象,这是我说的。");
}
}
catch (IOException e)
{
System.out.println("error");
break;
}
}
}
void SendToClient(PassPortData datatosend)
{
if(NowNetState==1)//处于连通状?
{
try
{
oout.writeObject(datatosend);
}
catch (IOException e)
{
System.out.println("error");
}
}
}
}
}
}
// void AnalyseData(PassPortData s)
// {
//
// //分析收到的数据
// if(s.Use.equals("GetGoodData"))
// {
//
// PassPortData ReplayData=new PassPortData();
// Vector gooddata=Server.DB.RsUserData(s.Conditin);
//
// ReplayData.data=gooddata;
// ReplayData.Use="Goods Data Replay";
// ReplayData.ReplayID=s.ID;
// SendToClient(ReplayData);
// }
// else if(s.Use.equals("UserLogin"))
// {
// PassPortData ReplayData=new PassPortData();
// UserDataDef ud=(UserDataDef)s.data.get(1);
//
// ReplayData.Use="User Login Result";
// String condition="UName='"+ud.UName+"'";
// Vector rsvector=Server.DB.RsUserData(condition);
// if (rsvector.size()==0)
// ReplayData.Conditin="不存在这个用户";
// else
// {
// UserDataDef ud1=(UserDataDef)rsvector.get(1);
// if(!ud1.PassWord.equals(ud.PassWord))
// ReplayData.Conditin="登录的密码不正确";
// else
// ReplayData.Conditin="登录成功了";
// }
// ReplayData.ReplayID=s.ID;
// SendToClient(ReplayData);
// }
// else if(s.Use.equals("OrderNow"))
// {
// }
// else if(s.Use.equals("OrderNow"))
// {
// }
//
// }
// }
// }
//}class PassPortData implements Serializable
{
static long UsedID=0; //已经使用了的ID
long ID; //编号
boolean Replay=true; //是否是回复数据
long ReplayID=-1; //回复的ID号
String To; //描述是从服务器到客户还是客户到服务器。
Vector data=null; //真正存放数据的地方
String Use; //这个对象的用处
String Conditin; //取得数据的条件
PassPortData()
{
To="Server";
ID=GetNextID();
}
static long GetNextID()
{
if (UsedID==65500)
UsedID=0;
else
UsedID++;
return UsedID;
}
}
我改了一下,应该可以直接运行的。
import java.net.*;class Server {
public static void main (String args[]) {
try {
ServerSocket ss = new ServerSocket(5050);
Socket c = ss.accept();
ObjectInputStream in = new ObjectInputStream(c.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(c.getOutputStream()); //接收客\u6237端\u53D1送的信息
Untitled17 untitled17 = (Untitled17)in.readObject();
System.out.println(untitled17.i); //向客\u6237端\u53D1送信息
in.close();
out.close();
c.close();
ss.close(); } catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
}
import java.net.*;class Client {
public static void main (String args[]) {
try {
Socket s = new Socket("localhost", 5050);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
Untitled17 untitled17 = new Untitled17();
untitled17.add();
out.writeObject(untitled17);
out.close();
s.close();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
}
public int i = 0;
public Untitled17() {
i = 10;
}
public void add() {
i = 20;
}
public static void main(String[] args) {
Untitled17 untitled171 = new Untitled17();
}}
您的程序是在Client中用的ObjectOutputStream,我试了在自己的程序中不要ObjectInputStream,只用ObjectOutputStream,就不会在那儿死等了,可是如果加上ObjectInputStream的话,又是那样了。
是不是Client不能用In啊?
直接使用:oin=(ObjectInputStream)is;即可。我试了一下好像没问题!!
import java.net.*;class Client {
public static void main (String args[]) {
try {
Socket s = new Socket("localhost", 5050);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
Untitled17 untitled17 = new Untitled17();
untitled17.add();
out.writeObject(untitled17);
out.close();
s.close();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
}这个问题现在我也不太清楚 不过先写out流 后写in流可以
不是
ObjectOutputStream
ObjectInputStream
实现有关只有
in out
in out
组合
有阻塞情况挺奇怪