自己弄得第一个小项目,是按着视频做的 。
来源:互联网 发布:中国十大中心城市 知乎 编辑:程序博客网 时间:2024/04/27 16:41
自己弄得第一个小项目,是按着视频做的 。还有很多小毛病 ,但自己还是很高兴,20多天的学习也算有点成果了。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatCliend{
public static void main(String[] args){
MyFrame mf=new MyFrame();
mf.launchFrame();
}
}
class MyFrame extends Frame{
TextField text;
TextArea text2;
Socket s;
DataOutputStream dos=null;
DataInputStream dis=null;
boolean on = false;
public void launchFrame(){
this.setLocation( 400, 100);
this.setSize(320, 500);
text=new TextField(10);
text2=new TextArea(100,1);
text.addActionListener(new TextMonitor());
add(text,BorderLayout.SOUTH);
add(text2,BorderLayout.CENTER);
this.addWindowListener(new WindowMonitor());
this.setVisible(true);
net();
new Thread(new Server()).start();
}
//此方法联网
public void net (){
try {
s= new Socket("10.50.140.53",8888);
dos= new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
on=true;
text2.setText("亲,您已经连接到服务器了,可以和您的朋友聊天了 "+'\n');
} catch (UnknownHostException e) {
text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");
} catch (IOException e) {
text2.setText("亲,您没有成功连接到服务器了 ,请与杨圣博联系... ");
}
}
//把内容发送给服务器
public void sent(String str){
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//这个类用于监听text的行为,给予一定的响应。然后把内容发送给服务器
public class TextMonitor implements ActionListener{
//本地处理text中的内容 然后调用sent方法 把他发送给服务器
public void actionPerformed(ActionEvent e){
String str=text.getText().trim();
text.setText("");
sent(str);
}
}
//这个类用于窗口的关毕
class WindowMonitor extends WindowAdapter{
public void windowClosing(WindowEvent e){
off();
System.exit(0);
}
}
//用于起一个多线程
class Server implements Runnable{
public void run(){
try {
while(on){
String s1=dis.readUTF();
text2.setText(text2.getText()+'\n'+s1+'\n');
}
} catch (IOException e) {
}
}
}
public void off(){
try {
if(s!=null)s.close();
if(dis!=null)dis.close();
if(dos!=null)dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上面的是客户端
import java.io.*;
import java.net.*;
import java.nio.CharBuffer;
import java.util.*;
public class ChatServer {
ServerSocket ss=null;
Socket s=null;
boolean on =false;
List<Client> clients=new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start(){
try {
ss=new ServerSocket(8888);
on=true;
}catch(Exception e){
System.out.println("亲,端口已经被占用......");
}
try{
while(on){
s=ss.accept();
Client c=new Client(s);
clients.add(c);
new Thread(c).start();
System.out.println("已经连上了");
}
}catch(IOException e) {
System.out.println(" 亲,不好意思,服务器已经崩溃...");
}
}
class Client implements Runnable{
private Socket s=null;
private boolean on1=false;
private DataInputStream dis=null;
private DataOutputStream dos=null;
public Client(Socket s){
this.s=s;
try {
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
on1=true;
} catch (IOException e) {
try {
if(s != null) s.close();
if(dis != null)dis.close();
if(dos != null) dos.close();
} catch (IOException e1) {
System.out.print("load......");
}
}
}
public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
System.out.print("load......");
}
}
public void run() {
try {
while(on1){
String str=dis.readUTF();
dos.writeUTF(str);
System.out.println(str);
for(int i=0;i<clients.size();i++){
Client c = clients.get(i);
c.send(str);
}
}
}catch (IOException e) {
System.out.print("load......");
}
}
}
}
这是服务器端。用的是多线程
- 自己弄得第一个小项目,是按着视频做的 。
- 自己的第一个小项目
- 自己做的第一个小测试吧 算
- 第一个自己做的ios小demo-星座
- 自己做的第一个小型Ext项目
- 自己做的小项目
- 自己做的第一个MTV
- 第一个自己做的东西!
- BW 做的第一个小报表
- Android做的第一个小程序
- 自己写的第一个小网站
- 自己的第一个C++小程序
- 第一个自己写的小程序
- 创建自己的第一个小程序
- 自己做的第一个能够被公司用到的项目
- 对做第一个项目的认识
- 我做第一个项目的经历
- 记录自己的第一个上线项目
- 复选框checkbox实现批量删除
- 虚拟机(Virtualbox-Ubuntu)与主机(WIN7)共享文件夹设置 &&19条小技巧让你快速上手Ubuntu 11.04
- JQuery EasyUI DataGrid 编程经验
- 解决building workplace 导致的卡死,使得eclipse加速
- Oracle 窗口函数
- 自己弄得第一个小项目,是按着视频做的 。
- 灯火阑珊,一夜芳醉年华:QQ空间伤感日志
- js 锚点操作
- Qt 模态与非模态
- asp.net(c#)数据类型格式转换大全
- 【小技巧】UltraEdit中替换操作如何替换特殊字符(比如回车)
- QC插件大全
- D3D9Resource.cpp
- php书籍