通过java编程中socket应用,编写一个基于c/s架构的局域网通信软件,

 我来答
百度网友471e72d
推荐于2016-01-20 · TA获得超过257个赞
知道小有建树答主
回答量:116
采纳率:0%
帮助的人:121万
展开全部
// 服务器端
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
 boolean started = false;
 ServerSocket ss = null;
 Socket socket = null;
 List<Client> clients = new ArrayList<Client>();
 
 public static void main(String[] args) {
  new ChatServer().start();
 }

 public void start() {
  
  try {
   ss = new ServerSocket(8888);
   started = true;
  } catch (BindException e) {
   System.out.println("端口使用中。。。。。");
   System.out.println("请关掉相关程序,并重新运行服务器!");
   System.exit(0);
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  try { 
   
   while(started) {
    socket = ss.accept();
    Client c = new Client(socket);

    new Thread(c).start();
    clients.add(c);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
 
 

 class Client implements Runnable {
  private Socket socket = null;
  private DataInputStream dis = null;
  private DataOutputStream dos = null;
  private boolean bConnected = false;
  
  public Client(Socket socket) {
   bConnected = true;
   this.socket = socket;
   try {
    dis = new DataInputStream(socket.getInputStream());
    dos = new DataOutputStream(socket.getOutputStream());
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  public void send(String str) {
   try {
    dos.writeUTF(str);
   }catch (IOException e) {
    clients.remove(this);
    System.out.println("对方退出了,我从List里面去掉了!");
   }
  }
  
  @Override
  public void run() {
    try {
     while(bConnected) {
      String s = dis.readUTF();
System.out.println(s);
      for(int i=0; i<clients.size(); i++) {
       Client c = clients.get(i);
       c.send(s);
      }

     }
    } catch (EOFException e) {
     System.out.println("client closed!");
    } catch (IOException e) {
     e.printStackTrace();
    } finally {
     try {
      if(dis != null) dis.close();
      if(dos != null) dos.close();
      if(socket != null) socket.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   
  }
 }

//客户端
 import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
 TextField tfTxt = new TextField();
 TextArea taContent = new TextArea();
 
 Socket socket = null;
 DataOutputStream dos = null;
 DataInputStream dis = null;
 boolean bConnected = false;
 Thread tRecv = new Thread(new Server());
 
 public static void main(String[] args) {
  new ChatClient().launchFrame();
 }
 
 public void launchFrame() {
  setBounds(400, 300, 300, 300);
  setVisible(true);
  this.setTitle("Chat Client");
  this.add(tfTxt, BorderLayout.SOUTH);
  this.add(taContent,BorderLayout.NORTH);
  this.pack();
  this.addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
    disconnect();
    setVisible(false);
    System.exit(0);
   }
   
  });
  tfTxt.addActionListener(new TFMonitor());
  
  connect();
  
  tRecv.start();
 }
 
 public void connect() {
  try {
   socket = new Socket("127.0.0.1", 8888);
   dos = new DataOutputStream(socket.getOutputStream());
   dis = new DataInputStream(this.socket.getInputStream());
System.out.println("Connected!");
   bConnected = true;
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public void disconnect() {
  try {
   dos.close();
   dis.close();
   socket.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  /*
  try {
   bConnected = false;
   tRecv.join();
  } catch (InterruptedException e) {
   e.printStackTrace();
  } finally {
   try {
    dos.close();
    dis.close();
    socket.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  */
 }
 
 
 private class TFMonitor implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
   String str = tfTxt.getText().trim();
   //taContent.setText(str);
   tfTxt.setText("");
   try {
//System.out.println(socket);
    dos.writeUTF(str);
    dos.flush();
    //dos.close();
   } catch (IOException e1) {
    e1.printStackTrace();
   }
  }
  
 }
 private class Server implements Runnable {
  
  @Override
  public void run() {
   try {
    while(bConnected) {
    String s = dis.readUTF();
//System.out.println(s);
    taContent.setText(taContent.getText() + s + '\n');
    }
   } catch (IOException e) {
    System.out.println("talk over,byebye");
    //e.printStackTrace();
   } 
  }
  
 }
}
匿名用户
2015-06-17
展开全部
看一下netty、mina框架的文章,很容易写一个出来。
追问
我不会写,你可以把代码发来吗?谢谢了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式