牛人帮帮忙啊。
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;import javax.swing.JOptionPane;
public class NewpbClient extends Applet implements ActionListener,MouseListener{
private Newpbdata thepbdata = new Newpbdata(0,0,0,0,1);
private Newpbcanvas theCanvas;
private Newpbconnect theconnect;
private int connectflag = 0;
public void init(){
Panel thepanel = new Panel();
//add button on panel
Button drawLine = new Button("Line");
drawLine.addActionListener(this);
thepanel.add(drawLine);
Button drawOval = new Button("Oval");
drawOval.addActionListener(this);
thepanel.add(drawOval);
Button drawString = new Button("Text");
drawString.addActionListener(this);
thepanel.add(drawString);
Button pbconnect = new Button("Connect");
pbconnect.addActionListener(this);
thepanel.add(pbconnect);
//add canvas
theCanvas = new Newpbcanvas();
theCanvas.addMouseListener(this);
//layout setting
this.setSize(400, 400);
this.setLayout(new BorderLayout());
this.add("North",thepanel);
this.add("Center",theCanvas);
} public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Line")){
thepbdata.setDrawflag(1);
}
if(e.getActionCommand().equals("Oval")){
thepbdata.setDrawflag(2);
}
if(e.getActionCommand().equals("Text")){
thepbdata.setDrawflag(3);
}
if(e.getActionCommand().equals("Connect")){
this.connectflag = 1;
this.theconnect = new Newpbconnect(theCanvas,thepbdata);
this.theconnect.start();
}
} //mouse listener
public void mouseClicked(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mousePressed(MouseEvent e) {
this.thepbdata.setX1(e.getX());
this.thepbdata.setY1(e.getY());
} public void mouseReleased(MouseEvent e) {
this.thepbdata.setX2(e.getX());
this.thepbdata.setY2(e.getY());
if(connectflag == 1){
this.theconnect.send();
System.out.println("x1 : "+this.thepbdata.getX1());
System.out.println("y1 : "+this.thepbdata.getY1());
}
this.theCanvas.updateDraw(this.thepbdata);
}
}
class Newpbconnect extends Thread{ private Socket socket;
private ObjectOutputStream os;
private ObjectInputStream is;
private Newpbcanvas thecanvas;
private Newpbdata thedata; // the constructor expects the IP address of the server - the port is fixed at 5050
public Newpbconnect(Newpbcanvas thecanvas,Newpbdata thepbdata)
{
this.thecanvas = thecanvas;
this.thedata = thepbdata;
String serverIP = "localhost";
try{ // open a new socket to port: 5050 and create streams
this.socket = new Socket(serverIP,5050);
this.os = new ObjectOutputStream(this.socket.getOutputStream());
this.is = new ObjectInputStream(this.socket.getInputStream());
System.out.print("Connected to Server\n");
System.out.println("client socket : "+socket);
}
catch (Exception ex){
System.out.print("Failed to Connect to Server\n" + ex.toString());
System.out.println(ex.toString());
}
}
public void run(){
while(this.receive()){}
}
// method to send a generic object.
public void send() {
try{
System.out.println("Sending " + this.thedata);
Newpbdata d = this.thedata;
System.out.println("x1 : "+d.getX1());
System.out.println("y1 : "+d.getY1());
Object o = d;
this.os.writeObject(o);
this.os.flush();
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
public boolean receive(){ System.out.println("waiting for receive");
Object o = null;
try
{
System.out.println("client socket : "+this.socket);
o = this.is.readObject();
System.out.println("Client : Receive a data " + o);
}
catch (Exception ex)
{
System.out.println("is error");
System.out.println(ex.toString());
}
if(o == null){
this.sockeClose();
System.out.println("client error");
return false;
}
else{
this.thedata = (Newpbdata)o;
System.out.println("x1 : "+this.thedata.getX1());
System.out.println("y1 : "+this.thedata.getY1());
System.out.println("received a data" + this.thedata);
this.thecanvas.updateDraw(this.thedata);
}
return true;
}
public void sockeClose(){
try
{
this.os.close();
this.is.close();
this.socket.close();
}
catch (Exception ex)
{
System.err.println(ex.toString());
}
}
}class Newpbdata implements Serializable{
private int x1,y1,x2,y2;
private int drawflag;
Newpbdata(int x1,int y1,int x2,int y2,int drawflag){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.drawflag = drawflag;
} public int getX1() {
return x1;
} public void setX1(int x1) {
this.x1 = x1;
} public int getY1() {
return y1;
} public void setY1(int y1) {
this.y1 = y1;
} public int getX2() {
return x2;
} public void setX2(int x2) {
this.x2 = x2;
} public int getY2() {
return y2;
} public void setY2(int y2) {
this.y2 = y2;
} public int getDrawflag() {
return drawflag;
} public void setDrawflag(int drawflag) {
this.drawflag = drawflag;
}
}//class newpbcanvas
class Newpbcanvas extends Canvas{
private int x1,y1,x2,y2,drawflag;
private int width,height;
public synchronized void updateDraw(Newpbdata d){
this.x1 = d.getX1();
this.y1 = d.getY1();
this.x2 = d.getX2();
this.y2 = d.getY2();
this.drawflag = d.getDrawflag();
System.out.println("x1 : "+x1+"y1 : "+y1+"x2 : "+x2+"y2 : "+y2);
this.repaint();
}
//paint;
public void paint (Graphics g){
switch(drawflag){
case 1:
g.drawLine(x1, y1, x2, y2);
break;
case 2:
int x,y;
if(x1 > x2){x =x2; width = x1 - x2;}
else{x = x1; width = x2 - x1;}
if(y1 > y2){y = y2; height = y1 - y2;}
else{y = y1; height = y2 - y1;}
g.drawOval(x, y, width, height);
break;
case 3:
String drawtext;
drawtext = JOptionPane.showInputDialog(
"Please input the text you want!");
g.drawString(drawtext,x1,y1);
break;
default:
}
}
//update drawing
public void update(Graphics g) {
paint(g);
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;import javax.swing.JOptionPane;
public class NewpbClient extends Applet implements ActionListener,MouseListener{
private Newpbdata thepbdata = new Newpbdata(0,0,0,0,1);
private Newpbcanvas theCanvas;
private Newpbconnect theconnect;
private int connectflag = 0;
public void init(){
Panel thepanel = new Panel();
//add button on panel
Button drawLine = new Button("Line");
drawLine.addActionListener(this);
thepanel.add(drawLine);
Button drawOval = new Button("Oval");
drawOval.addActionListener(this);
thepanel.add(drawOval);
Button drawString = new Button("Text");
drawString.addActionListener(this);
thepanel.add(drawString);
Button pbconnect = new Button("Connect");
pbconnect.addActionListener(this);
thepanel.add(pbconnect);
//add canvas
theCanvas = new Newpbcanvas();
theCanvas.addMouseListener(this);
//layout setting
this.setSize(400, 400);
this.setLayout(new BorderLayout());
this.add("North",thepanel);
this.add("Center",theCanvas);
} public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Line")){
thepbdata.setDrawflag(1);
}
if(e.getActionCommand().equals("Oval")){
thepbdata.setDrawflag(2);
}
if(e.getActionCommand().equals("Text")){
thepbdata.setDrawflag(3);
}
if(e.getActionCommand().equals("Connect")){
this.connectflag = 1;
this.theconnect = new Newpbconnect(theCanvas,thepbdata);
this.theconnect.start();
}
} //mouse listener
public void mouseClicked(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mousePressed(MouseEvent e) {
this.thepbdata.setX1(e.getX());
this.thepbdata.setY1(e.getY());
} public void mouseReleased(MouseEvent e) {
this.thepbdata.setX2(e.getX());
this.thepbdata.setY2(e.getY());
if(connectflag == 1){
this.theconnect.send();
System.out.println("x1 : "+this.thepbdata.getX1());
System.out.println("y1 : "+this.thepbdata.getY1());
}
this.theCanvas.updateDraw(this.thepbdata);
}
}
class Newpbconnect extends Thread{ private Socket socket;
private ObjectOutputStream os;
private ObjectInputStream is;
private Newpbcanvas thecanvas;
private Newpbdata thedata; // the constructor expects the IP address of the server - the port is fixed at 5050
public Newpbconnect(Newpbcanvas thecanvas,Newpbdata thepbdata)
{
this.thecanvas = thecanvas;
this.thedata = thepbdata;
String serverIP = "localhost";
try{ // open a new socket to port: 5050 and create streams
this.socket = new Socket(serverIP,5050);
this.os = new ObjectOutputStream(this.socket.getOutputStream());
this.is = new ObjectInputStream(this.socket.getInputStream());
System.out.print("Connected to Server\n");
System.out.println("client socket : "+socket);
}
catch (Exception ex){
System.out.print("Failed to Connect to Server\n" + ex.toString());
System.out.println(ex.toString());
}
}
public void run(){
while(this.receive()){}
}
// method to send a generic object.
public void send() {
try{
System.out.println("Sending " + this.thedata);
Newpbdata d = this.thedata;
System.out.println("x1 : "+d.getX1());
System.out.println("y1 : "+d.getY1());
Object o = d;
this.os.writeObject(o);
this.os.flush();
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
public boolean receive(){ System.out.println("waiting for receive");
Object o = null;
try
{
System.out.println("client socket : "+this.socket);
o = this.is.readObject();
System.out.println("Client : Receive a data " + o);
}
catch (Exception ex)
{
System.out.println("is error");
System.out.println(ex.toString());
}
if(o == null){
this.sockeClose();
System.out.println("client error");
return false;
}
else{
this.thedata = (Newpbdata)o;
System.out.println("x1 : "+this.thedata.getX1());
System.out.println("y1 : "+this.thedata.getY1());
System.out.println("received a data" + this.thedata);
this.thecanvas.updateDraw(this.thedata);
}
return true;
}
public void sockeClose(){
try
{
this.os.close();
this.is.close();
this.socket.close();
}
catch (Exception ex)
{
System.err.println(ex.toString());
}
}
}class Newpbdata implements Serializable{
private int x1,y1,x2,y2;
private int drawflag;
Newpbdata(int x1,int y1,int x2,int y2,int drawflag){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.drawflag = drawflag;
} public int getX1() {
return x1;
} public void setX1(int x1) {
this.x1 = x1;
} public int getY1() {
return y1;
} public void setY1(int y1) {
this.y1 = y1;
} public int getX2() {
return x2;
} public void setX2(int x2) {
this.x2 = x2;
} public int getY2() {
return y2;
} public void setY2(int y2) {
this.y2 = y2;
} public int getDrawflag() {
return drawflag;
} public void setDrawflag(int drawflag) {
this.drawflag = drawflag;
}
}//class newpbcanvas
class Newpbcanvas extends Canvas{
private int x1,y1,x2,y2,drawflag;
private int width,height;
public synchronized void updateDraw(Newpbdata d){
this.x1 = d.getX1();
this.y1 = d.getY1();
this.x2 = d.getX2();
this.y2 = d.getY2();
this.drawflag = d.getDrawflag();
System.out.println("x1 : "+x1+"y1 : "+y1+"x2 : "+x2+"y2 : "+y2);
this.repaint();
}
//paint;
public void paint (Graphics g){
switch(drawflag){
case 1:
g.drawLine(x1, y1, x2, y2);
break;
case 2:
int x,y;
if(x1 > x2){x =x2; width = x1 - x2;}
else{x = x1; width = x2 - x1;}
if(y1 > y2){y = y2; height = y1 - y2;}
else{y = y1; height = y2 - y1;}
g.drawOval(x, y, width, height);
break;
case 3:
String drawtext;
drawtext = JOptionPane.showInputDialog(
"Please input the text you want!");
g.drawString(drawtext,x1,y1);
break;
default:
}
}
//update drawing
public void update(Graphics g) {
paint(g);
}
}
*
*/
package newpb;import java.applet.Applet;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;/**
* @author Administrator
*
*/
public class NewpbServer extends Applet implements ActionListener{
private TextArea status;
private Newpbserverconnect serverconnect;
public void init(){
System.out.println("start");
this.setLayout(new FlowLayout());
Button connectbutton = new Button("connect");
connectbutton.addActionListener(this);
this.add(connectbutton);
Button closebutton = new Button("close");
closebutton.addActionListener(this);
this.add(closebutton);
status = new TextArea(10,40);
this.add(status); this.setSize(400, 300);
this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("connect")){
this.serverconnect = new Newpbserverconnect(this.status);
}
}
}class Newpbserverconnect {
private ServerSocket serverSocket;
private Socket clientSocket;
private Vector<ObjectOutputStream> alloutsteam = new Vector<ObjectOutputStream>();
private TextArea status;
private ObjectOutputStream serveros;
private ObjectInputStream serveris;
public Newpbserverconnect(TextArea s){
this.status = s;
this.serverConnect();
this.handleConnect();
}
public void serverConnect(){
try{
this.serverSocket = new ServerSocket(5050);
this.status.append("Server has started listening on port 5050 \n");
this.status.append("Server Socket: "+this.serverSocket+"\n");
}
catch (IOException e)
{
System.out.println("Error: Cannot listen on port 5050: " + e);
System.exit(1);
}
}
@SuppressWarnings("unused")
public void handleConnect(){
while (true) // infinite loop - loops once for each client
{
try
{
this.clientSocket = serverSocket.accept(); //waits here (forever) until a client connects
System.out.println("accept clientsocket : "+this.clientSocket);
this.status.append("Server has just accepted socket connection from a client\n");
this.status.append("client socket was " + clientSocket+"\n");
this.serveris = new ObjectInputStream(this.clientSocket.getInputStream());
this.serveros = new ObjectOutputStream(this.clientSocket.getOutputStream());
}
catch (IOException e)
{
this.status.append("Accept failed: 5050 " + e.getMessage());
break;
} // Create the Handle Connection object - only create it
NewpbHandleconnection con = new NewpbHandleconnection(this.serveris,this.serveros,this.clientSocket,this.alloutsteam); if (con == null) //If it failed send and error message
{
try
{
ObjectOutputStream os = new ObjectOutputStream(clientSocket.getOutputStream());
os.writeObject("error: Cannot open socket thread");
os.flush();
os.close();
}
catch (Exception ex) //failed to even send an error message
{
this.status.setText("Cannot send error back to client: 5050 " + ex);
}
}
else { con.start(); } // otherwise we have not failed to create the HandleConnection object
// run it now.
}
try // do not get here at the moment
{
this.status.append("Closing server socket.");
serverSocket.close();
}
catch (IOException e)
{
this.status.append("Could not close server socket. " + e.getMessage());
}
}
}class NewpbHandleconnection extends Thread
{private Socket clientSocket; // Client socket object
private ObjectInputStream is; // Input stream
private ObjectOutputStream os; // Output stream
private Vector<ObjectOutputStream> allstream;// The constructor for the connecton handlerpublic NewpbHandleconnection(ObjectInputStream serveris,ObjectOutputStream serveros,
Socket clientSocket,Vector<ObjectOutputStream> alloutsteam)
{
this.clientSocket = clientSocket;
this.allstream = alloutsteam;
this.os = serveros;
this.is = serveris;
this.allstream.addElement(this.os);
}// The main execution method
public void run()
{
//try
//{
System.out.println("Server1 : "+this.clientSocket+this.is+this.os);
//input new
//this.is = new ObjectInputStream(this.clientSocket.getInputStream());
System.out.println("Server2 : "+this.clientSocket+this.is);
//output new
//this.os = new ObjectOutputStream(this.clientSocket.getOutputStream());
System.out.println("Server3 : "+this.clientSocket+this.os);
//this.allstream.addElement(this.os);
while(this.readCommand()){} //}
//catch (IOException e)
//{
// e.printStackTrace();
//}
}private synchronized void sendall(Object o){
for(int i=0;i<this.allstream.size();i++){
ObjectOutputStream tempstream =null;
tempstream = this.allstream.elementAt(i);
if(this.os!=tempstream){
try{
System.out.println("Server6 : Sending to all " + o);
System.out.println("Server7 : send to client "+tempstream);
System.out.println("Server8 : from port "+this.clientSocket);
Newpbdata d = (Newpbdata)o;
System.out.println("Server9 : the data x1 = "+d.getX1()+"y1 = "+d.getY1());
tempstream.writeObject(o);
tempstream.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
// Receive and process incoming command from client socket private boolean readCommand()
{
Object o = null;
try{
System.out.println("Server4 : client"+this.clientSocket+is);
o = this.is.readObject();
System.out.println("Server5 : receive a data "+o);
}
catch (Exception ex){
System.out.println(ex.toString());
}
if (o == null)
{
this.closeSocket();
return false;
} // invoke the appropriate function based on the command
this.sendall(o);
return true;
}// Send a pre-formatted error message to the client
public void sendError(String msg)
{
this.sendall("error:" + msg); //remember a string IS-A object!
}// Close the client socket
public void closeSocket() //close the socket connection
{
try
{
this.os.close();
this.is.close();
this.clientSocket.close();
}
catch (Exception ex)
{
System.err.println(ex.toString());
}
}}
os.writeObject(d);要么这么写 os.writeUnshared(thedata);
这是因为同一个对象如果前面发过了,后面就会直接引用前面的那个。上面两种改法都避免了向前引用。