J
juicy
//Object.html
<html>
<head>
<title>VE</title>
</head>
<BODY>
<DIV align="left"> </DIV>
<APPLET id="Mainvirtual_dispatch1" height="176" width="568"
code="Client.class"
VIEWASTEXT mayscript></APPLET>
<DIV></DIV>
<DIV align="left"></APPLET>
</DIV>
</BODY>
</html>
//Server.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.Event;
public class Server extends Thread {
public ServerSocket arrayServer;
public static void main(String argv[]) throws Exception {
new Server();
}
public Server() throws Exception {
arrayServer = new ServerSocket(1053);
System.out.println("Server listening on port 1053.");
this.start();
}
public void run() {
while(true) {
try {
System.out.println("Waiting for connections.");
Socket client = arrayServer.accept();
System.out.println("Accepted a connection from: "+
client.getInetAddress());
Connect c = new Connect(client);
} catch(Exception e) {}
}
}
}
class Connect extends Thread {
public Socket client = null;
public ObjectInputStream ois = null;
public ObjectOutputStream oos = null;
public Connect() {}
public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream(client.getInputStream());
oos = new ObjectOutputStream(client.getOutputStream());
} catch(Exception e1) {
try {
client.close();
}catch(Exception e) {
System.out.println(e.getMessage());
}
return;
}
this.start();
}
public void run() {
Object x = new Object();
try {
x = ois.readObject();
if(x!=null)
System.out.println("object has been read");
oos.writeObject(x);
oos.flush();
ois.close();
oos.close();
client.close();
} catch(Exception e) {e.getMessage();}
}
}
//Client.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client extends Applet implements Runnable{
Socket client;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Button Connect,write,read;
Thread recvThread;
String host;
boolean running=true;
public void init()
{
host = getCodeBase().getHost();
add(Connect = new java.awt.Button());
Connect.setLabel("Connect");
Connect.setBackground(new Color(0xd3ceac));
Connect.setForeground(new Color(0x000000));
Connect.show(true);
add(write = new java.awt.Button());
write.setLabel("Write");
write.setBackground(new Color(0xd3ceac));
write.show(true);
add(read = new java.awt.Button());
read.setLabel("Read");
read.setBackground(new Color(0xd3ceac));
read.show(true);
}
public void start(){
if(recvThread==null){
recvThread = new Thread(this);
recvThread.start();}
}
public void destroy(){
running = false;
recvThread = null;
}
public boolean action(Event event, Object what) {
if (recvThread == null) {
start();
}
if (event.target instanceof Button){
Button b = (Button) event.target;
if(b.getLabel()=="Connect"){
try{
Socket socket = new Socket(host, 1053);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());}
catch(Exception e) {e.getMessage();}
}
if (event.arg == "Write") {
try{
oos.writeObject((Object)event);
oos.flush();
}
catch(Exception e){ }
}
if (event.arg == "Read") {
try{
Object get=null;
get=ois.readObject();
if(get!=null)
Connect.setLabel("success");
else
write.setLabel("error");
oos.close();
ois.close();
}
catch (IOException e) {Connect.setLabel("ex1");}
catch (ClassNotFoundException e){Connect.setLabel("ex2");}
catch (Exception e){Connect.setLabel("ex3");}
}
}
return true;
}
public void run(){}
}
The problem is:
the client cannot read the object send back from server and
ClassNotFoundException occur.
<html>
<head>
<title>VE</title>
</head>
<BODY>
<DIV align="left"> </DIV>
<APPLET id="Mainvirtual_dispatch1" height="176" width="568"
code="Client.class"
VIEWASTEXT mayscript></APPLET>
<DIV></DIV>
<DIV align="left"></APPLET>
</DIV>
</BODY>
</html>
//Server.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.Event;
public class Server extends Thread {
public ServerSocket arrayServer;
public static void main(String argv[]) throws Exception {
new Server();
}
public Server() throws Exception {
arrayServer = new ServerSocket(1053);
System.out.println("Server listening on port 1053.");
this.start();
}
public void run() {
while(true) {
try {
System.out.println("Waiting for connections.");
Socket client = arrayServer.accept();
System.out.println("Accepted a connection from: "+
client.getInetAddress());
Connect c = new Connect(client);
} catch(Exception e) {}
}
}
}
class Connect extends Thread {
public Socket client = null;
public ObjectInputStream ois = null;
public ObjectOutputStream oos = null;
public Connect() {}
public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream(client.getInputStream());
oos = new ObjectOutputStream(client.getOutputStream());
} catch(Exception e1) {
try {
client.close();
}catch(Exception e) {
System.out.println(e.getMessage());
}
return;
}
this.start();
}
public void run() {
Object x = new Object();
try {
x = ois.readObject();
if(x!=null)
System.out.println("object has been read");
oos.writeObject(x);
oos.flush();
ois.close();
oos.close();
client.close();
} catch(Exception e) {e.getMessage();}
}
}
//Client.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client extends Applet implements Runnable{
Socket client;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Button Connect,write,read;
Thread recvThread;
String host;
boolean running=true;
public void init()
{
host = getCodeBase().getHost();
add(Connect = new java.awt.Button());
Connect.setLabel("Connect");
Connect.setBackground(new Color(0xd3ceac));
Connect.setForeground(new Color(0x000000));
Connect.show(true);
add(write = new java.awt.Button());
write.setLabel("Write");
write.setBackground(new Color(0xd3ceac));
write.show(true);
add(read = new java.awt.Button());
read.setLabel("Read");
read.setBackground(new Color(0xd3ceac));
read.show(true);
}
public void start(){
if(recvThread==null){
recvThread = new Thread(this);
recvThread.start();}
}
public void destroy(){
running = false;
recvThread = null;
}
public boolean action(Event event, Object what) {
if (recvThread == null) {
start();
}
if (event.target instanceof Button){
Button b = (Button) event.target;
if(b.getLabel()=="Connect"){
try{
Socket socket = new Socket(host, 1053);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());}
catch(Exception e) {e.getMessage();}
}
if (event.arg == "Write") {
try{
oos.writeObject((Object)event);
oos.flush();
}
catch(Exception e){ }
}
if (event.arg == "Read") {
try{
Object get=null;
get=ois.readObject();
if(get!=null)
Connect.setLabel("success");
else
write.setLabel("error");
oos.close();
ois.close();
}
catch (IOException e) {Connect.setLabel("ex1");}
catch (ClassNotFoundException e){Connect.setLabel("ex2");}
catch (Exception e){Connect.setLabel("ex3");}
}
}
return true;
}
public void run(){}
}
The problem is:
the client cannot read the object send back from server and
ClassNotFoundException occur.