-
JAVA
实现即使通讯的代码
图形界面
/////////////////////////////////////////////////
/////////
//
用
Java
编写的聊天器,可以当
服务器
或者
是客户端,一对一,自定义对方
IP
及
端口。
//
虽然名为
LANChat
,但不限于局域网。对象甚至可以是某种
“
服务器
”
。
////////////
///////////////////////////////////////////////
import
.*;
import
.*;
import
.*;
import
.*;
import
.*;
//////////////////////////////////////////////////
////////
public
class
LANChatV12{
public
static
void
m
ain(String
args[]){
LoginFrame
lf
=
new
LoginFrame(
输入目标
();
}
}
<
/p>
////////////////////////////////////////
/////////////////
class
LoginFrame
extends
JFrame{
JButton
BOK;
JLabel
LdesAddr,
Lport;
JTextField
TFdesAddr,
TFport;
String
desAddr,
port;
LoginFrame(){}
LoginFrame(String
title){
super(title);
Fram
e
t
=
this;
BOK
=
new
JButton(
确定
LdesAddr
=
new
JLabel(
目标
IP
Lport
=
new
JLabel(
端口
TFdesAddr
=
new
JTextField(desAddr,12);
TFport
=
new
JTextField(port,12);
//oChar('*');
ionListener(new
BOKListener(t));
setBackground();
setBounds(350,250,200,128);
setLayout(new
FlowLayout(,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{
Fram
e
t;
BOKListener(){}
BOKListener(Frame
t){
this.t
=
t;
}
public
void
actionPerformed(ActionEvent
e){
desAddr
=
t();
port
=
t();
ible(false);
Messenger
m
=
new
Messenger(desAddr,
port);
();
}
}
}
//
//////////////////////////////////////////////////
/
class
Messenger
extends
Thread{
String
desAddr;
String
port;
int
iport;
TextArea
content,
send;
JButton
Bsend;
ChatFram
e
cf;
String
title;
Socket client;
ServerSocket
ss;
OutputStream
Writer
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(
content
=
new
TextArea(
send
=
new
TextArea(
ICAL_ONLY);
Bsend
=
new
JButton(
发送
bled(false);
title
=
与
+
desAddr
+
聊天
flag
=
true;
cbc
=
true;
cf
=
new
ChatFram
e(title,
content,
send,
Bsend);
();
r
=
desAddr;
=
port;
try{
iport
=
nt(port);
}catch(NumberFormatException
nfe){
(
非法的
端口,程序
4
秒后关闭。
n
flag
=
false;
}
}
public
void
run(){
if(!
flag){
try{
(4000);
}catch(InterruptedException
ie){
System
.exit(0);
}
System
.exit(0);
}
-
-
-
-
-
-
-
-
-
上一篇:英语专业[09-19]作文真题
下一篇:微机原理期末考试复习