法院查封房产流程:Java论坛 求助:我编的局域网通讯工具,运行服务器端后,再运行客户端,为什么客户端就死了一样啊

来源:百度文库 编辑:高校问答 时间:2024/05/03 09:59:21
代码在下面的网址里,有两个文件;
http://bbs.chinajavaworld.com/thread.jspa?threadID=722913

while死循环。。。

//client端的程序如下:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MyClient extends Frame implements ActionListener,ItemListener
{
MenuBar m_Menu_Bar;
Menu menuFile,menuEdit,menuHelp;
MenuItem mi_File_Open,mi_File_Close,mi_File_Exit,mi_Edit_Copy,mi_Edit_Paste;
MenuItem pi_New,pi_Del,pi_Pro,mi_Help_Sub;
CheckboxMenuItem mi_Edit_Cut;
PopupMenu popM;

Socket ClientSocket;
PrintStream os;
DataInputStream is;
String s;
Label MyLabel=new Label(" ☆欢迎使用本系统为您提供服务☆");
TextArea textarea;
Button MyButton=new Button("发 送 消 息");

public MyClient()
{
setTitle("Client Window(客户端窗口)");
setLayout(new BorderLayout());

this.addWindowListener(new WinAdptClient(this));
MyButton.addActionListener(this);

textarea=new TextArea(13,55);
//8888888888888888888888888888888888888888888888888888888888888888888888888888//
popM=new PopupMenu();
pi_New=new MenuItem(" 新建 ");
pi_New.addActionListener(this);
popM.add(pi_New);
pi_Del=new MenuItem(" 删除 ");
pi_Del.addActionListener(this);
popM.add(pi_Del);
pi_Pro=new MenuItem(" 属性 ");
pi_Pro.addActionListener(this);
popM.add(pi_Pro);

m_Menu_Bar=new MenuBar();
menuFile=new Menu("文件");
mi_File_Open=new MenuItem("打开");
mi_File_Open.setShortcut(new MenuShortcut('f'));
mi_File_Close=new MenuItem("关闭",new MenuShortcut('s'));
mi_File_Exit=new MenuItem("退出",new MenuShortcut('x'));

mi_File_Open.setActionCommand("打开");
mi_File_Close.setActionCommand("关闭");
mi_File_Exit.setActionCommand("退出");

mi_File_Open.addActionListener(this);
mi_File_Close.addActionListener(this);
mi_File_Exit.addActionListener(this);

menuFile.add(mi_File_Open);
menuFile.add(mi_File_Close);
menuFile.add(mi_File_Exit);

m_Menu_Bar.add(menuFile);

menuEdit=new Menu("编辑");
mi_Edit_Copy=new MenuItem("复制");
mi_Edit_Paste=new MenuItem("粘贴");
mi_Edit_Cut=new CheckboxMenuItem("CUT");
mi_Edit_Copy.setActionCommand("复制");
mi_Edit_Paste.setActionCommand("粘贴");

mi_Edit_Copy.addActionListener(this);
mi_Edit_Paste.addActionListener(this);
mi_Edit_Cut.addItemListener(this);

menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Paste);
menuEdit.addSeparator();
menuEdit.add(mi_Edit_Cut);
m_Menu_Bar.add(menuEdit);

menuHelp=new Menu("帮助");
mi_Help_Sub=new MenuItem("主题");
menuHelp.add(mi_Help_Sub);
m_Menu_Bar.add(menuHelp);
this.setMenuBar(m_Menu_Bar);
//*********************************************************************************//

add("North",MyLabel);
add("South",MyButton);
add("Center",textarea);
setResizable(false);
pack();
show();
connect();
}

public void connect()
{
try{
ClientSocket=new Socket("localhost",6544);
os=new PrintStream(
new BufferedOutputStream(ClientSocket.getOutputStream()));
is=new DataInputStream(
new BufferedInputStream(ClientSocket.getInputStream()));
s=is.readLine();
textarea.appendText(s+"\n");
}
catch(Exception e){}
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//

public void itemStateChanged(ItemEvent e)//响应CHECKBOXMENUITEM被点击事件
{
if(e.getSource()==mi_Edit_Cut)
if(((CheckboxMenuItem)e.getSource()).getState())//查看是否被选中
textarea.setText("\n\n\n\n\n\n\t\t\t"+"you have chosen "+
((CheckboxMenuItem)e.getSource()).getLabel());
else
textarea.setText("\n\n\n\n\n\n\t\t\t"+"you have not chosen "+

((CheckboxMenuItem)e.getSource()).getLabel());

}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
public void actionPerformed(ActionEvent e)
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
if(e.getActionCommand()=="退出")
{
dispose();
System.exit(0);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//

if(e.getSource()==MyButton)
{
try{
os.print(textarea.getText());
os.flush();
}
catch(Exception e1){}
}
}
public static void main(String args[])
{
new MyClient();
}
}

class WinAdptClient extends WindowAdapter
{
MyClient m_Parent;
WinAdptClient(MyClient p)
{
m_Parent=p;
}
public void windowClosing(WindowEvent e)
{
try{//关闭窗口前先向SERVER端发送结束信息,并关闭各输入输出流与连接
m_Parent.os.println("Bye");
m_Parent.os.flush();
m_Parent.is.close();
m_Parent.os.close();
m_Parent.ClientSocket.close();
m_Parent.dispose();
System.exit(0);
}catch(IOException e2){}
}
}

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Server
{
public static void main(String[] args)
{
ServerService MyServer=new ServerService(6544,10);
}
}
class ServiceThread extends Frame implements Runnable
{//当CLIENT有请求时 SERVER端创建一个FRAME用于与之交互数据
ServerService FatherListener;
Socket ConnectedClient;
Thread ConnectThread;
Panel ListenerPanel;
TextArea ServerMeg;
public ServiceThread(ServerService sv,Socket s)//构造函数
{
FatherListener=sv;
ConnectedClient=s;
ConnectThread=new Thread(this);
setTitle("Server(服务器端)");
setLayout(new BorderLayout());
ServerMeg=new TextArea(13,50);
add("Center",ServerMeg);
setResizable(false);
pack();
show();
InetAddress ClientAddress=ConnectedClient.getInetAddress();
//获得请求服务的CLIENT端计算机的IP地址
ServerMeg.appendText("Server connect"+" to: \n\n"+ClientAddress.toString()+".\n");
}

public void run()
{
try{
DataInputStream in=new DataInputStream(//获得从CILENT读入的数据流
new BufferedInputStream(ConnectedClient.getInputStream()));
PrintStream out=new PrintStream(//获得向CILENT输出的数据流
new BufferedOutputStream(ConnectedClient.getOutputStream()));

out.println(" Hello!Welcome connect to me(Server)!\r");
out.flush();//向CLIENT端输出信息
String s=in.readLine();//从CLIENT端读入信息
while(!s.equals("Bye"))
{
ServerMeg.appendText("client端输入的信息为: \n"+s);
s=in.readLine();//读入CLIENT端写入的下一行信息
}
ConnectedClient.close();
}
catch(Exception e){}
FatherListener.addMeg("Client"+"closed."+"\n");
dispose();
}//run()
}

//*********************************************************************************************//
class ServerService extends Frame //服务器端的监听窗口
{
ServerSocket m_sListener; //监听器
TextArea ListenerMeg; //显示信息的监听器窗口
public ServerService(int Port,int Count)
{
try{
m_sListener=new ServerSocket(6544,10);//建立监听服务
setTitle("Server Listener(监听器窗口)"); //建立监听服务的窗口并显示
this.addWindowListener(new WinAdpt());
setLayout(new BorderLayout());
ListenerMeg=new TextArea(" [监听服务已启动啦]\n\n\n",10,50);
add("Center",ListenerMeg);
setResizable(false);
pack();
show();
while(true)
{
Socket Connected=m_sListener.accept();//接受来自Client端的请求
InetAddress ClientAddress=Connected.getInetAddress();

ListenerMeg.appendText("Client "+" connected "+" from:\n\n"+ClientAddress.toString()+" .\n");
//获得客户机的IP地址 建立新线程新窗口与这个CLIENT进行通信
ServiceThread MyST=new ServiceThread(this,Connected);
MyST.ConnectThread.start();
}
}

catch(IOException e){}//异常处理方法

}

public void addMeg(String s)
{
ListenerMeg.appendText(s);
}
}//ServerService Class

class WinAdpt extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
((Frame)e.getWindow()).dispose();
System.exit(0);
}
}

看看这个吧!跟你的差不多!我也不清楚你的错误在哪里,你自己改好了也给我看看!
以下是我给你修改的:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.JLabel;
import java.awt.ActiveEvent;

public class Login implements ActionListener{
private Frame f;
private TextField tf;
private Label l;
private Panel p1;
private Panel p2;
private Button b1;
private Button b2;
private String tfContent="";

public Login(){
f = new Frame("欢迎登陆");
l = new Label("请输入用户名");
JLabel label1=new JLabel();
JLabel label2=new JLabel();
p1 =new Panel();
p2 =new Panel();
tf = new TextField(20);
b1=new Button("登录");
b2=new Button("关闭");
b1.setBackground(Color.PINK);
b2.setBackground(Color.PINK);

f.setLayout(new FlowLayout());

p1.add(l);
p1.add(tf);

String filestr1="D://1//4.gif";
label1.setIcon( new ImageIcon(filestr1) ) ;
String filestr2="D://1//2.gif";
label2.setIcon( new ImageIcon(filestr2) ) ;
p2.add(label1);
p2.add(label2);
p2.setSize(200, 200);
p2.setVisible(true);

f.add(p2,"North");
f.add(p1,"Center");
f.add(b1,"South");
f.add(b2,"South");

f.setSize(300,210);
f.setBackground(Color.PINK);
f.show();

b1.addActionListener(this);
b2.addActionListener(this);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){
System.exit(0);
}

});

}
public void actionPerformed(ActionEvent e){

if (e.getSource()==b1)
{
tfContent = tf.getText();
Client_Frame cf = new Client_Frame("客户端-"+tfContent);

//System.exit(0);
}
else if (e.getSource()==b2)
{
System.exit(0);
}

}
public static void main(String[] args)
{
new Login();
}
}

//************************88888888888888888888

class Client_Frame extends Frame implements ActionListener
{
Button 发送1,退出1;
TextArea In_Message1;
TextArea Out_Message1;

Socket socket1=null;
DataInputStream in1=null;
DataOutputStream out1=null;

////////////////////////////////////////////////////////////////////

//public void actionPerformed(){}

public Client_Frame(String name)
{
super(name);
发送1=new Button("发送");
退出1=new Button("退出");
In_Message1=new TextArea(" ",5,40);
Out_Message1=new TextArea(" ",8,40);

发送1.setBackground(Color.CYAN);
退出1.setBackground(Color.CYAN);

Panel p11=new Panel();
Panel p21=new Panel();
Panel p31=new Panel();
// Panel p4=new Panel();

JLabel label11=new JLabel("输入待发送的消息:");
JLabel label21=new JLabel("接受来自服务端的消息:");
JLabel label31=new JLabel();

p11.add(label11);
p11.add(In_Message1);

p21.add(label21);
p21.add(Out_Message1);

p31.add(label31);
p31.add(退出1);
p31.add(发送1);
this.pack();

String filestr1="D://1//11.gif";
label11.setIcon( new ImageIcon(filestr1) ) ;
String filestr2="D://1//8.gif";
label21.setIcon( new ImageIcon(filestr2) ) ;
String filestr3="D://1//12.gif";
label31.setIcon( new ImageIcon(filestr3) ) ;

//p4.setSize(200, 200);
//p4.setVisible(true);

add(p11,"North");

add(p31,"Center");

add(p21,"South");
//add(p4,"West");

String s1=null;
this.setSize(550,400);
setBackground(Color.PINK);

setVisible(true);
validate();
发送1.addActionListener(this);
退出1.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

try
{
/* 当套结字连接socket建立后,可以看作一条通信"线路"已建立起来。
* socket可以调用方法getInputStream()获得一个输入流,然后
* 通过这个输入流读入服务器放入"线路"的信息;
* socket可以调用方法getOutputStream()获得一个输出流,然后
* 用这个输出流将信息写入"线路"。
*/

socket1 = new Socket("127.0.0.1",4331);
//参数1是服务端的IP地址,参数2是端口号
in1 =new DataInputStream(socket1.getInputStream());
out1 = new DataOutputStream(socket1.getOutputStream());
out1.writeUTF("你好,已建立与客户端的连接\n\n");
while(true)
{
try{
s1=in1.readUTF();//堵塞状态,除非读取到信息。
Out_Message1.append(new Date()+"\n");
Out_Message1.append("服务器端说: "+s1+"\n");
} catch(IOException e)
{
Out_Message1.append("与服务器已断开");
break;
}
Thread.sleep(500);
}
}catch (IOException e){ Out_Message1.append("ERROR"+ e ); }
catch(InterruptedException e) { }
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==发送1)
{
String s=In_Message1.getText();
if(s!=null)
{
try {
out1.writeUTF(s);
Out_Message1.append(new Date()+"服务器端说: "+"\n");
Out_Message1.append(s+"\n");
}
catch(IOException e1){}

}

}
else if (e.getSource()==退出1)
{
System.exit(0);
}
}
}

//Login.java
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.*;
import javax.swing.JLabel;

class Server_Frame extends Frame implements ActionListener
{
Button 发送,退出;
TextArea In_Message;
TextArea Out_Message;

ServerSocket server=null;
Socket socket=null;
DataInputStream in=null; DataOutputStream out=null;

Server_Frame()
{
super("服务端");
发送=new Button("发送");
退出=new Button("退出");
In_Message=new TextArea(" ",5,40);
Out_Message=new TextArea(" ",8,40);

发送.setBackground(Color.CYAN);
退出.setBackground(Color.CYAN);

Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
// Panel p4=new Panel();

JLabel label1=new JLabel("输入待发送的消息:");
JLabel label2=new JLabel("接受来自服务端的消息:");
JLabel label3=new JLabel();

p1.add(label1);
p1.add(In_Message);

p2.add(label2);
p2.add(Out_Message);

p3.add(label3);
p3.add(退出);
p3.add(发送);
this.pack();

String filestr1="D://1//5.gif";
label1.setIcon( new ImageIcon(filestr1) ) ;
String filestr2="D://1//14.gif";
label2.setIcon( new ImageIcon(filestr2) ) ;
String filestr3="D://1//12.gif";
label3.setIcon( new ImageIcon(filestr3) ) ;

//p4.setSize(200, 200);
//p4.setVisible(true);

add(p1,"North");

add(p3,"Center");

add(p2,"South");
//add(p4,"West");
String s=null;
this.setSize(550,400);
setBackground(Color.PINK);

setVisible(true);
validate();
发送.addActionListener(this);
退出.addActionListener(this);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){
System.exit(0);
}

});

try{
server=new ServerSocket(4331);
}
catch (IOException ee)
{ Out_Message.append("\n\nERROR"+ee); }

try
{
Out_Message.append("Waiting...");
//while(true)
//{

socket = server.accept();
in =new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

out.writeUTF("你好,已建立与本服务端的连接\n\n");

while(true)
{
try{
s=in.readUTF();//堵塞状态,除非读取到信息。
Out_Message.append(new Date()+"客户端:"+"\n");
Out_Message.append(s+"\n");
} catch(IOException e)
{
Out_Message.append("客户端已断开");
break;
}
Thread.sleep(500);
}

//socket.close();
//dispose();
//}
//break;
}catch (IOException e){ Out_Message.append("ERROR"+ e ); }

catch(InterruptedException e) { }

}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==发送)
{
String s=In_Message.getText();
if(s!=null)
{
try {
out.writeUTF(s);
Out_Message.append(new Date()+"服务器端说: "+"\n");
Out_Message.append(s+"\n");
}
catch(IOException e1){}
}

}
else if (e.getSource()==退出)
{
System.exit(0);
}
}

//}

//public class Server
//{
public static void main(String args[])
{
Server_Frame server_win=new Server_Frame();

}
}