单词发音英[sli_p]美[sli_p]基本用法sleep用作动词的基本意思是“睡眠”,也可作“为(某数量的人)提供床位”解。
sleep与介词to连用时一般都省略冠词。sleep用作名词的意思是“睡眠”,是不可数名词加不定冠词时,表示“一段时间的睡眠”。
sleep的进行时可以表示按计划、安排或打算即将发生的动作,这时句中往往有表示将来的时间状语或特定的上下文。一站式出国留学攻略 http://www.offercoming.com
下面分别是服务器端和客户端的代码,先运行服务器端,再运行客户端服务器端:
import java.awt.event.*
import java.io.*
import java.net.*
import java.util.*
import java.util.List
import javax.swing.*
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000)
ServerFrame sf = new ServerFrame()
sf.launchFrame()
List list = new ArrayList()// 创建数组
while (true) { // 开启多线程
Socket s = ss.accept()
list.add(s)
Thread t = new ServerThread(s, list, sf)
t.start()
}
}
}
class ServerThread extends Thread { // 创建线程类
Socket s
BufferedReader in
PrintWriter out
ServerFrame sf
public ServerThread(Socket s, List list, ServerFrame sf) {
this.s = s
this.sf = sf
sf.sokectList = list
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()))
out = new PrintWriter(s.getOutputStream())
} catch (IOException e) {
e.printStackTrace()
}
}
public void run() {
while (true) {
try {
String str = in.readLine()
if (str == null)
continue
sf.jta.append("接收到:" + str + "\n")
} catch (IOException e) {
return
}
}
}
}
class ServerFrame {
List sokectList
JTextField jtf
JTextArea jta
PrintWriter out
public void launchFrame() {
JFrame frame = new JFrame("服务器端")// 创建 frame对象
frame.setSize(400, 300)// 设置fram大小
frame.setLocation(300, 250)
jta = new JTextArea()
jta.setEditable(false)
jtf = new JTextField()
jtf.addActionListener(new ActionListener() { // 注册监听器
public void actionPerformed(ActionEvent arg0) {
send()
}
})
frame.getContentPane().add(new JScrollPane(jta))
frame.getContentPane().add(jtf, "South")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setVisible(true)
}
public void send() { // 输出文字
String text = this.jtf.getText()
this.jtf.setText("")
if(sokectList==null) {
jta.append("无客户端,发送失败:" + text + "\n")
return
}
jta.append("发送指令:" + text + "\n")
Iterator it = sokectList.iterator()
while (it.hasNext()) {
Socket socket = (Socket) (it.next())
try {
out = new PrintWriter(socket.getOutputStream())
} catch (IOException e) {
// TODO Auto-generated catch block
jta.append("与客户端连接失败!\n")
continue
}
out.println(text)
out.flush()
}
}
}
客户端:
import java.awt.BorderLayout
import java.awt.Frame
import java.awt.Panel
import java.awt.TextArea
import java.awt.event.*
import java.io.*
import java.net.*
import javax.swing.*
public class ChatClient {
public static void main(String[] args) {
ChatClient cc = new ChatClient()
while(true)
cc.receive()
}
static boolean boo
Frame clientFrame
Panel topPanel
TextArea topTextArea
Socket s
BufferedReader in
PrintWriter out
public ChatClient() {
this.clientFrame = new Frame("客户端")
this.clientFrame.setBounds(350, 250, 150, 250)
this.clientFrame.setResizable(false)
this.clientFrame.setVisible(true)
this.topPanel = new Panel()
this.clientFrame.setBounds(350, 250, 150, 200)
this.topTextArea = new TextArea()
this.topPanel.add(this.topTextArea)
this.clientFrame.add(this.topPanel, BorderLayout.NORTH)
this.clientFrame.pack()
this.clientFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int var = JOptionPane.showConfirmDialog(null, "退出?",
"退出", JOptionPane.OK_CANCEL_OPTION)
if (var == JOptionPane.OK_OPTION)
System.exit(0)
}
})
try {
s = new Socket("127.0.0.1", 9000)//设置端口
in = new BufferedReader(new InputStreamReader(s.getInputStream()))//创建对象in
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()))//创建对象out
} catch(UnknownHostException e) {//捕捉异常
e.printStackTrace()
} catch(ConnectException e) {
JOptionPane.showMessageDialog(null, "与服务器连接失败,请确认服务器是否已经开启!")
System.exit(-1)
} catch(IOException e) {
e.printStackTrace()
}
}
public void receive() { // 读信息
try {
String text = in.readLine()
if("STOP".equals(text.toUpperCase())) {
if(boo) {
this.topTextArea.append("结束发送!\n")
boo = false
}
return
}
if("START".equals(text.toUpperCase())) {
boo = true
this.topTextArea.append("开始向服务器发送数据:\n")
new SendThread(this).start()
return
}
this.topTextArea.append("接收到无效指令:" + text + "\n")
} catch (SocketException e) {
JOptionPane.showMessageDialog(null, "与服务器断开连接!")
System.exit(-1)
} catch (IOException e) {
e.printStackTrace()
return
}
}
public void send() {
while(boo) {
String str = Math.random() + ""
out.println(str)
out.flush()
this.topTextArea.append("发送:" + str + "\n")
try {
Thread.sleep(1000)
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
class SendThread extends Thread {
ChatClient cc
SendThread(ChatClient cc) {
this.cc = cc
}
@Override
public void run() {
// TODO Auto-generated method stub
cc.send()
}
}