设为首页 加入收藏

TOP

计算机二级辅导:java网络通信小程序
2014-11-20 02:43:23 】 浏览:1063
Tags:计算机 二级 辅导 :java 网络通信 程序

  //ChatServer.java文件


  import java.io.*;


  import java.net.*;


  import java.util.*;


  public class ChatServer {


  boolean started = false;


  ServerSocket ss = null;


  List clients = new ArrayList ();


  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 s = ss.accept();


  Client c = new Client(s);


  System.out.println("a client connected!");


  new Thread(c).start();


  clients.add(c);


  //dis.close();


  }


  } catch (IOException e) {


  e.printStackTrace();


  } finally {


  try {


  ss.close();


  } catch (IOException e) {


  // TODO Auto-generated catch block


  e.printStackTrace();


  }


  }


  }


  class Client implements Runnable {


  private Socket s;


  private DataInputStream dis = null;


  private DataOutputStream dos = null;


  private boolean bConnected = false;


  public Client(Socket s) {


  this.s = s;


  try {


  dis = new DataInputStream(s.getInputStream());


  dos = new DataOutputStream(s.getOutputStream());


  bConnected = true;


  } catch (IOException e) {


  e.printStackTrace();


  }


  }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇计算机二级考试java辅导:JAVA日.. 下一篇java小程序输入三个数求最大数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目