MultiServer.java
import java.io.*;
import java.net.*;
public class MultiServer
{ static int clientnum=0;
public static void main(String args[]) throws IOException
{
ServerSocket serverSocket=null;
boolean listening=true;
try{
serverSocket=new ServerSocket(4700);
}
catch(IOException e)
{
System.out.println("Could not listen on port:4700.");
System.exit(-1);
}
while(listening)
{
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++;
}
serverSocket.close();
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ServerThread extends Thread
{
private Frame f;
private Panel p;
private TextArea l, t;
private Button b;
private static PrintWriter os;
private static BufferedReader is;
Socket socket = null;
int clientnum;
public ServerThread(Socket socket,int num)
{
this.socket = socket;
clientnum = num+1;
}
public void run()
{
try{
is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
os =new PrintWriter(socket.getOutputStream());
String initstr=is.readLine();
if(!initstr.equalsIgnoreCase(""))
{
f = new Frame("Chat Client");
l = new TextArea(" Welcome to this room!\t已经有"
+clientnum+"人了。\n Client: "+initstr,5,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
p = new Panel();
t = new TextArea("",1,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
b = new Button("Send");
p.setLayout(new GridLayout(1,2));
p.add(t);
p.add(b);
f.add(l,"Center");
f.add(p,"South");
f.setTitle("Chat Server");
f.setBounds(400, 200, 300,100);
f.setVisible(true);
t.requestFocus();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
f.setVisible(false);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(!t.getText().equalsIgnoreCase("bye")&&!t.getText().equalsIgnoreCase("")){
String line = t.getText();
os.println(line);
os.flush();
l.append("\n Server: "+line);
t.setText("");
t.requestFocus();
}else if(t.getText().equalsIgnoreCase("")){
l.append("\n不允许发空消息!");
}else{
System.exit(1);
}
}
});
String rel;
while(!(rel=is.readLine()).equalsIgnoreCase(""))
{
f.setVisible(true);
l.append("\n Client: "+rel);
t.requestFocus();
}
}
}catch(Exception e){
System.exit(1);
}
}
}
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class CClient
{
private Frame f;
private Panel p;
private TextArea l, t;
private Button b;
private static Socket client;
private static PrintWriter os;
private static BufferedReader is;
public static void main(String[] args)
{
new CClient();
}
CClient()
{
try {
client = new Socket("127.0.0.1",4700);
os = new PrintWriter(client.getOutputStream());
is = new BufferedReader(new InputStreamReader(client.getInputStream()));;
f = new Frame("Chat Client");
l = new TextArea(" Welcome to this room!",5,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
p = new Panel();
t = new TextArea("",1,40,TextArea.SCROLLBARS_VERTICAL_ONLY);
b = new Button("Send");
p.setLayout(new GridLayout(1,2));
p.add(t);
p.add(b);
f.add(l,"Center");
f.add(p,"South");
f.setTitle("Chat Client");
f.setBounds(100, 100, 300,100);
f.setVisible(true);
t.requestFocus();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(1);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(!t.getText().equalsIgnoreCase("bye")&&!t.getText().equalsIgnoreCase("")){
String line = t.getText();
os.println(line);
os.flush();
l.append("\n Client: "+line);
t.setText("");
t.requestFocus();
}else if(t.getText().equalsIgnoreCase("")){
l.append("\n不允许发空消息!");
}else{
System.exit(1);
}
}
});
String rel;
while(!(rel=is.readLine()).equals(""))
{
l.append("\n Server: "+rel);
t.requestFocus();
}
} catch (UnknownHostException e) {
System.out.println("UnknownHost");
} catch (IOException e) {
System.out.println("????????????");
}
}
}
收藏了,,