package DD;
import java.io.*;
import java.net.*;
import java.awt.*;
public class Server
extends Frame
implements Runnable {
ServerSocket server;
Socket connection;
OutputStream output;
InputStream input;
Thread outThread;
TextArea display;
TextField text1;
Button startButton;
public static void main(String args[]) {
Server s = new Server();
}
public Server() {
super("Server");
startButton = new Button("Start the server");
text1 = new TextField(20);
display = new TextArea(7, 30);
display.setEditable(false);
add("North", startButton);
add("Center", display);
add("South", text1);
resize(300, 600);
show();
}
public boolean action(Event e, Object o) {
if (e.target == startButton) {
display.setText("启动服务器...\n");
startButton.setEnabled(false);
try {
server = new ServerSocket(5000, 100);
connection = server.accept();
output = connection.getOutputStream();
input = connection.getInputStream();
outThread = new Thread(this);
outThread.start();
}
catch (IOException ee) {}
}
else if (e.target == text1) {
byte writeBytes[] = new byte[50];
String s = "Server: " + text1.getText() + "\n";
text1.setText("");
writeBytes = s.getBytes();
display.append(s);
try {
output.write(writeBytes);
}
catch (IOException ee) {}
if (s.trim().equals("Server: exit")) {
outThread.stop();
quit();
}
}
return true;
}
public void run() {
while (true) {
byte readBytes[] = new byte[50];
try {
input.read(readBytes);
}
catch (IOException e) {}
String s = new String(readBytes);
display.append(s);
if (s.trim().equals("Client: exit"))
break;
}
quit();
}
public void quit() {
try {
output.close();
input.close();
connection.close();
}
catch (IOException e) {}
startButton.setEnabled(true);
}
public boolean handleEvent(Event e) {
if (e.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(e);
}
}
聊天室---BS架构jsp版.rar
1星 需积分: 9 195 浏览量
2009-01-17
16:45:16
上传
评论 1
收藏 12KB RAR 举报
易极赞
- 粉丝: 503
- 资源: 88
最新资源
- 已过基于Hadoop+Spark招聘推荐可视化系统 大数据项目 毕业设计(源码下载)
- python爬虫开发题答案及题目-100(1).zip
- Python 小游戏 (贪吃蛇、五子棋、扫雷、俄罗斯方块)-3 (2).zip
- c语言实现的数独小游戏.zip
- 高德地图中国行政区划省、市、县经纬度
- March 2024 Expiration Of The OAM Out Of The Box Certificates
- 二叉搜索树迭代器(java代码).docx
- 解决keil MDK 5.38版本 在Debug配置使用STlink调试时软件闪退的问题
- py小项目:用户登录和注册系统开发欢迎图片
- TCCEE-x64-v6.2.3(9.51)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈