可以了,只是服务器端出了点小状况;
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(6000,10);
}
}
class ServiceThread extends Frame implements Runnable
{
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(true);
pack();
setVisible(true);
InetAddress ClientAddress=ConnectedClient.getInetAddress();
ServerMeg.append("Server connect"+" to:\n\n"+ClientAddress.toString()+".\n");
}
public void run(){
try{
DataInputStream in =new DataInputStream(new BufferedInputStream(ConnectedClient.getInputStream()));
PrintStream out=new PrintStream(new BufferedOutputStream(ConnectedClient.getOutputStream()));
out.println("Hello!,Welcome connect to me(server)!\r");
out.flush();
String s=in.readLine();
System.out.println(s);
while(!s.equals("Bye")){
ServerMeg.append("client 端输入的信息为:\n"+s);
s=in.readLine();
}
ConnectedClient.close();
}
catch(Exception e){}
FatherListener.addMeg("Client"+"closed."+"\n");
dispose();
}
}
class ServerService extends Frame{
ServerSocket m_sListener;
TextArea ListenerMeg;
public ServerService(int Port,int Count){
try{
m_sListener=new ServerSocket(6000);
setTitle("Server Listener(监听器窗口)");
this.addWindowListener(new WinAdpt());
setLayout(new BorderLayout());
ListenerMeg=new TextArea("[监听服务已启动]\n\n\n",10,50);
add("Center",ListenerMeg);
setResizable(true);
pack();
setVisible(true);
while(true){
Socket Connected=m_sListener.accept();
InetAddress ClientAddress=Connected.getInetAddress();
ListenerMeg.append("Client "+" connected "+"from:\n\n"+ClientAddress.toString()+".\n");
ServiceThread MyST=new ServiceThread(this,Connected); MyST.ConnectThread.start();
}
}
catch (IOException e){}
}
public void addMeg(String s){
ListenerMeg.append(s);
}
}
class WinAdpt extends WindowAdapter{
public void windowClosing(WindowEvent e){
((Frame)e.getWindow()).dispose();
System.exit(0);
}
}