自己弄得第一个小项目,是按着视频做的 。

来源:互联网 发布:中国十大中心城市 知乎 编辑:程序博客网 时间: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......");

}
}
}
 
 
}

这是服务器端。用的是多线程

原创粉丝点击