JAVA实现即使通讯的代码 图形界面
//////////////////////////////////////////////////////////
//用 Java 编写的聊天器,可以当 服务器 或者 是客户端,一对一,自定义对方 IP 及 端口。
//虽然名为 LANChat ,但不限于局域网。对象甚至可以是某种 “服务器”。
///////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//////////////////////////////////////////////////////////
public class LANChatV12{
public static void main(String args[]){
LoginFrame lf = new LoginFrame("输入目标");
lf.show();
}
}
/////////////////////////////////////////////////////////
class LoginFrame extends JFrame{
JButton BOK;
JLabel LdesAddr, Lport;
JTextField TFdesAddr, TFport;
String desAddr, port;
LoginFrame(){}
LoginFrame(String title){
super(title);
Frame t = this;
BOK = new JButton("确定");
LdesAddr = new JLabel("目标 IP");
Lport = new JLabel("端口 ");
TFdesAddr = new JTextField(desAddr,12);
TFport = new JTextField(port,12);
//TFpassword.setEchoChar('*');
BOK.addActionListener(new BOKListener(t));
setBackground(Color.blue);
setBounds(350,250,200,128);
setLayout(new FlowLayout(FlowLayout.CENTER,5,7));
add(LdesAddr);
add(TFdesAddr);
add(Lport);
add(TFport);
add(BOK);
setResizable(false);
//setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class BOKListener implements ActionListener{
Frame t;
BOKListener(){}
BOKListener(Frame t){
this.t = t;
}
public void actionPerformed(ActionEvent e){
desAddr = TFdesAddr.getText();
port = TFport.getText();
t.setVisible(false);
Messenger m = new Messenger(desAddr, port);
m.start();
}
}
}
/////////////////////////////////////////////////////
class Messenger extends Thread{
String desAddr;
String port;
int iport;
TextArea content, send;
JButton Bsend;
ChatFrame cf;
String title;
Socket client;
ServerSocket ss;
OutputStreamWriter osw = null;
InputStreamReader isr = null;
BufferedReader br;
String line;
boolean flag;//端口号是否正确
boolean cbc; //can be client ?
int tryTurns = 3; //客户方式尝试的次数
Messenger(){}
Messenger(String desAddr, String port){
super("LANChatMessenger");
content = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
send = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
Bsend = new JButton("发送");
Bsend.setEnabled(false);
title = "与 " + desAddr +" 聊天";
flag = true;
cbc = true;
cf = new ChatFrame(title, content, send, Bsend);
cf.show();
this.desAddr = desAddr;
this.port = port;
try{
iport = Integer.parseInt(port);
}catch(NumberFormatException nfe){
content.append("非法的 端口,程序 4 秒后关闭。\n");
flag = false;
}
}
public void run(){
if(! flag){
try{
Thread.sleep(4000);
}catch(InterruptedException ie){
System.exit(0);
}
System.exit(0);
}
content.append("正在以客户端方式进行连接.....\n");
do{
cbc = true;
try{
client = new Socket(desAddr,iport);
}catch(Exception e){
content.append("错误,无法连接地址:" + desAddr +":"+ port + '\n');
content.append("等待 1 秒再连接,剩余 [ " + (tryTurns -1) + " ]次。\n");
cbc = false;
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
System.exit(0);
}
}
--tryTurns;
}while((tryTurns > 0) && ! cbc);
if(cbc){
content.append("连接成功,可以开始了。" + "\n\n");
cf.setTitle("与 " + desAddr + ":" + port + " 聊天");
send.requestFocus();
}
else{
content.append("客户端方式失败,现在启动服务器并等待连接。" + '\n');
try{
ss = new ServerSocket(iport);
}catch(IOException ioe){
content.append("\n无法创建服务,程序将在 4 秒后退出。\n");
try{
Thread.sleep(4000);
}catch(InterruptedException ie){
System.exit(0);
}
System.exit(0);
}
try{
cf.setTitle("等待连接中....在端口:" + port);
client = ss.accept();
}catch(Exception e){
content.append("\nss.accept() 方法失败,程序将在 4 秒后退出。\n");
try{
Thread.sleep(4000);
}catch(InterruptedException ie){
System.exit(0);
}
System.exit(0);
}
content.append("连接成功,可以开始。" + client.getInetAddress().toString() + "\n\n");
cf.setTitle("与 " + client.getInetAddress().toString() + ":" + port + " 聊天");
send.requestFocus();
}
try{
osw = new OutputStreamWriter(client.getOutputStream());
isr = new InputStreamReader(client.getInputStream());
br= new BufferedReader(isr);
//content.append("\nbr created\n");
}catch(IOException ioe){
content.append("创建流错误,程序将在 4 秒后退出。\n");
try{
Thread.sleep(4000);
}catch(InterruptedException ie){
System.exit(0);
}
System.exit(0);
}
Bsend.setEnabled(true);
Bsend.addActionListener(new BsendListener(content, send, osw));
try{
line = br.readLine();
}catch(IOException ioe){
content.append("流读取错误,程序将在 4 秒后退出。\n");
try{
Thread.sleep(4000);
}catch(InterruptedException ie){
System.exit(0);
}
System.exit(0);
}
while(true){
content.append("他说 : " + line + '\n');
try{
Thread.sleep(1000);
}catch(InterruptedException ie){
System.exit(0);
}
try{
line = br.readLine();
}catch(IOException ioe){
content.append("流读取错误,程序将在 4 秒后退出。\n");
try{
Thread.sleep(4000);
}catch(InterruptedException ie){
System.exit(0);
}
System.exit(0);
}
}
}
class BsendListener implements ActionListener{
TextArea content;
TextArea send;
OutputStreamWriter osw;
BsendListener(TextArea content, TextArea send, OutputStreamWriter osw){
this.content = content;
this.send = send;
this.osw = osw;
}
public void actionPerformed(ActionEvent e){
String input;
input = send.getText();
if(input.length() > 0){
content.append("我说 : " + input + '\n');
try{
osw.write(input + '\n', 0, input.length() + 1);
osw.flush();// 晕
}catch(Exception ee){
content.append("不能发送 \"" + input + "\" , 发生了错误 : " +ee.getMessage() + '\n');
}
send.setText("");
send.requestFocus();
}
}
}
}
///////////////////////////////////////////////////////////////
class ChatFrame extends JFrame{
TextArea content;
TextArea send;
JButton Bsend;
ChatFrame(){}
ChatFrame(String frameTitle,TextArea content, TextArea send, JButton Bsend){
super(frameTitle);
this.content = content;
this.send = send;
this.Bsend = Bsend;
this.send.setEditable(true);
setLayout(null);
setBounds(300,100,510,500);
add(content);
content.setBackground(Color.WHITE);
content.setBounds(2,0,500,340);
content.setFocusable(true);
add(send);
send.setBounds(2,355,500,70);
send.setFocusable(true);
add(Bsend);
Bsend.setBounds(410,432,60,30);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}