/*******************服务端的程序*************************/
//Server.java
package server;
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
public class Server{
public static void main(String arg[]){
ServerService MyServer=new ServerService(8000,10);
}
}
//交互端的服务窗口
class ServiceThread extends Frame
implements ActionListener,Runnable{
ServerService FatherListener;
Socket ConnectedClient;
Thread ConnectedThread;
Panel ListenePanel;
TextArea ServerMeg;
TextArea ServerWrite;
Label la;
Button bt;
BufferedReader in;
PrintWriter out;
public ServiceThread(ServerService sv,Socket s){
//容器与格子组建的结合
FatherListener=sv;
ConnectedClient=s;
ConnectedThread =new Thread(this);
//采用线程的目的是利用他的并发机制,实现多个任务
//在当前的类中(this)通过Runnabel()接口实现Thread()的Run()方案
setTitle("Answer Client");
setLayout(new GridLayout(4,1));
//采用格子组建的形式完成组建的安排
ServerMeg=new TextArea(null,5,45,TextArea.SCROLLBARS_VERTICAL_ONLY);
//添加各种组建到容器当中
ListenePanel=new Panel();
ListenePanel.add(ServerMeg);
add(ListenePanel);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//((Frame)e.getWindow()).dispose();//保留的语句
System.exit(0);
}});
//第二个Panel()
la=new Label("服务端消息输入框");
add(la);
ServerWrite=new TextArea(10,50);
add(ServerWrite);
//add(p2);
setSize(355,360);
Panel p3=new Panel();
bt=new Button("传送");
bt.addActionListener(this);
p3.add(bt);
add(p3);
setResizable(false);
show();
InetAddress ClientAddress=ConnectedClient.getInetAddress();
ServerMeg.append("Client Conneted from \n "+ClientAddress.getHostAddress()+"!");
}
//完成事件的部
//面临的一个问题是空发消息
public void actionPerformed(ActionEvent ex){
if(ex.getSource()==bt){
if((int)((ServerWrite.getText().toString()).length())!=0)
ServerMeg.append("服务端消息:"+ServerWrite.getText()+"\n");
out.println("\n"+ServerWrite.getText());
ServerWrite.setText("");
}
else {
ServerMeg.append("系统提示: 不能空法消息!\n");
}
}
// 覆盖线程的run()方法
public void run(){
try{
in=
new BufferedReader(new InputStreamReader(ConnectedClient.getInputStream()));
out=new PrintWriter(ConnectedClient.getOutputStream(),true);
out.println("Hello! Wellocme connect our server !");
out.flush();
String s=in.readLine();
if(s.equals("bye"))
out.println("已经断开与服务器的连接.............. \n 欢 迎 下 次 观 顾!");
while(!s.equals("bye")){
ServerMeg.append("\n\n"+s);
s=in.readLine();
}
ConnectedClient.close();
}catch(Exception e){}
FatherListener.addMeg("Client"+"Cloesd."+"\n");
}//run(),finished;
}
//服务窗口
class ServerService extends Frame{
ServerSocket Listener;
TextArea ListenerMeg;
public ServerService(int port,int Count){
try{
Listener=new ServerSocket(port,Count);//开启监听的端口
setTitle("服务监听");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
((Frame)e.getWindow()).dispose();//释放组建与内存
System.exit(0);
}});
setLayout(new BorderLayout());
ListenerMeg=new TextArea("监听服务已经启动 \n",10,50);
add("Center",ListenerMeg);
setResizable(false);//设计固定的状态
pack();//重新的打包
show();
while(true){
Socket connected=Listener.accept();//等待外部的连接,直到连接成功才返回一个套接字
InetAddress ClientAddress=connected.getInetAddress();
ListenerMeg.append("\n Client connected from :\n"+ ClientAddress.toString());
ServiceThread MyST=new ServiceThread(this,connected);//初使化服务线程
//this指向当前对象的指针
MyST.ConnectedThread.start();//启动线程
} //获取套接子,激活线程,采用循环的语句解决方式
}catch(Exception ep){}
}
public void addMeg(String s){
ListenerMeg.append(s);
}
}
哪位高手帮忙看看 要怎么添加!! 急用啊!!