import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ChatOneToOne extends Frame{
Button clientBtn,serverBtn;
TextArea ta;
TextField tfaddress, tfport, tftype;
int port;
clientskt client;
serverskt server;
boolean iamserver;
static ChatOneToOne frm;
public ChatOneToOne(){
clientBtn =new Button("客户端");
serverBtn =new Button("服务器");
ta =new TextArea("",10,50,TextArea.SCROLLBARS_BOTH);
tfaddress =new TextField("在此输入IP地址");
tfport =new TextField("连接站");
tftype =new TextField(50);
tftype.addKeyListener(new TFListener());
ta.setEditable(false);
setLayout(new FlowLayout());
add(tfaddress);
add(tfport);
add(clientBtn);
add(serverBtn);
add(ta);
add(tftype);
setSize(400,300);
setTitle("一对一聊天程序");
clientBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
port = Integer.parseInt(tfport.getText());
client =new clientskt(tfaddress.getText(),port,frm);
client.start();
tfaddress.setEnabled(false);
serverBtn.setEnabled(false);
clientBtn.setEnabled(false);
}
}
);
serverBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
port = Integer.parseInt(tfport.getText());
server=new serverskt(port,frm);
server.start();
iamserver=true;
tfaddress.setText("成为服务器");
tfaddress.setEnabled(false);
tfport.setEnabled(false);
serverBtn.setEnabled(false);
clientBtn.setEnabled(false);
}
}
);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
show();
}
public static void main(String args[]){
frm=new ChatOneToOne();
}
private class TFListener implements KeyListener{
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
ta.append(">"+tftype.getText()+"\n");
if(iamserver)
server.dataout(tftype.getText());
else
client.dataout(tftype.getText());
tftype.setText("");
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class clientskt extends Thread{
Socket skt;
InetAddress host;
int port;
BufferedReader theInputStream;
PrintStream theOutputStream;
String readin;
ChatOneToOne chat;
public clientskt(String ip,int p,ChatOneToOne chat){
try {
host = InetAddress.getByName(ip);
port = p;
this.chat = chat;
} catch (IOException e) {
chat.ta.append(e.toString());
}
}
public void run(){
try{
chat.ta.append("尝试联机......");
skt=new Socket(host,port);
chat.ta.append("联机成功\n");
theInputStream=new BufferedReader(new InputStreamReader(skt.getInputStream()));
theOutputStream=new PrintStream(skt.getOutputStream());
while(true){
readin=theInputStream.readLine();
chat.ta.append(readin+"\n");
}
}catch(SocketException e){
chat.ta.append("联机中断!\n");
chat.clientBtn.setEnabled(true);
chat.serverBtn.setEnabled(true);
chat.tfaddress.setEnabled(true);
chat.tfport.setEnabled(true);
try {
skt.close();
} catch (IOException err) {
chat.ta.append(err.toString());
}
}
}
public void dataout(String data){
theOutputStream.println(data);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class serverskt extends Thread{
ServerSocket skt;
Socket Client;
TextArea in;
BufferedReader theInputStream;
PrintStream theOutputStream;
String readin;
ChatOneToOne chat;
public serverskt(int port,ChatOneToOne chat){
try{
skt=new ServerSocket(port);
this.chat=chat;
}catch(IOException e){ chat.ta.append(e.toString());}
}
public void run(){
try{
chat.ta.append("等待联机......");
Client=skt.accept();
chat.ta.append("客户端"+Client.getInetAddress()+"已联机\n");
theInputStream=new BufferedReader(new InputStreamReader(Client.getInputStream()));
theOutputStream=new PrintStream(Client.getOutputStream());
while(true){
readin=theInputStream.readLine();
chat.ta.append(readin+"\n");
}
}catch(SocketException e){
chat.ta.append("联机中断!\n");
chat.clientBtn.setEnabled(true);
chat.serverBtn.setEnabled(true);
chat.tfaddress.setEnabled(true);
chat.tfport.setEnabled(true);
try{
skt.close();
Client.close();
}catch(IOException err){ chat.ta.append(err.toString());}
}catch(IOException e){
chat.ta.append(e.toString());
}
}
public void dataout(String data){
theOutputStream.println(data);
}
}
[此贴子已经被作者于2006-4-3 23:54:26编辑过]