熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java網絡通信小程序

2022-06-13   來源: Java核心技術 

  //ChatServerjava文件

  import javaio*;

  import *;

  import javautil*;

  public class ChatServer {

  boolean started = false;

  ServerSocket ss = null;

  List<Client> clients = new ArrayList<Client>();

  public static void main(String[] args) {

  new ChatServer()start();

  }

  public void start() {

  try {

  ss = new ServerSocket();

  started = true;

  } catch (BindException e) {

  Systemoutprintln(端口使用中);

  Systemoutprintln(請關掉相關程序並重新運行服務器!);

  Systemexit();

  } catch (IOException e) {

  eprintStackTrace();

  }

  try {

  while(started) {

  Socket s = ssaccept();

  Client c = new Client(s);

  Systemoutprintln(a client connected!);

  new Thread(c)start();

  clientsadd(c);

  //disclose();

  }

  } catch (IOException e) {

  eprintStackTrace();

  } finally {

  try {

  ssclose();

  } catch (IOException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  }

  class Client implements Runnable {

  private Socket s;

  private DataInputStream dis = null;

  private DataOutputStream dos = null;

  private boolean bConnected = false;

  public Client(Socket s) {

  thiss = s;

  try {

  dis = new DataInputStream(sgetInputStream());

  dos = new DataOutputStream(sgetOutputStream());

  bConnected = true;

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  public void send(String str) {

  try {

  doswriteUTF(str);

  } catch (IOException e) {

  clientsremove(this);

  Systemoutprintln(對方退出了!我從List裡面去掉了!);

  //eprintStackTrace();

  }

  }

  public void run() {

  try {

  while(bConnected) {

  String str = disreadUTF();

  Systemoutprintln(str);

  for(int i=; i<clientssize(); i++) {

  Client c = clientsget(i);

  csend(str);

  //Systemoutprintln( a string send !);

  }

  /*

  for(Iterator<Client> it = erator(); ithasNext(); ) {

  Client c = itnext();

  csend(str);

  }

  */

  /*

  Iterator<Client> it = erator();

  while(ithasNext()) {

  Client c = itnext();

  csend(str);

  }

  */

  }

  } catch (EOFException e) {

  Systemoutprintln(Client closed!);

  } catch (IOException e) {

  eprintStackTrace();

  } finally {

  try {

  if(dis != null) disclose();

  if(dos != null) dosclose();

  if(s != null) {

  sclose();

  //s = null;

  }

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  }

  }

  }

  //ChatClientjava文件

  import javaawt*;

  import javaawtevent*;

  import javaio*;

  import *;

  public class ChatClient extends Frame {

  Socket s = null;

  DataOutputStream dos = null;

  DataInputStream dis = null;

  private boolean bConnected = false;

  TextField tfTxt = new TextField();

  TextArea taContent = new TextArea();

  Thread tRecv = new Thread(new RecvThread());

  public static void main(String[] args) {

  new ChatClient()launchFrame();

  }

  public void launchFrame() {

  setLocation( );

  thissetSize( );

  add(tfTxt BorderLayoutSOUTH);

  add(taContent BorderLayoutNORTH);

  pack();

  thisaddWindowListener(new WindowAdapter() {

  @Override

  public void windowClosing(WindowEvent arg) {

  disconnect();

  Systemexit();

  }

  });

  tfTxtaddActionListener(new TFListener());

  setVisible(true);

  connect();

  tRecvstart();

  }

  public void connect() {

  try {

  s = new Socket( );

  dos = new DataOutputStream(sgetOutputStream());

  dis = new DataInputStream(sgetInputStream());

  Systemoutprintln(connected!);

  bConnected = true;

  } catch (UnknownHostException e) {

  eprintStackTrace();

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  public void disconnect() {

  try {

  dosclose();

  disclose();

  sclose();

  } catch (IOException e) {

  eprintStackTrace();

  }

  /*

  try {

  bConnected = false;

  tRecvjoin();

  } catch(InterruptedException e) {

  eprintStackTrace();

  } finally {

  try {

  dosclose();

  disclose();

  sclose();

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  */

  }

  private class TFListener implements ActionListener {

  public void actionPerformed(ActionEvent e) {

  String str = tfTxtgetText()trim();

  //taContentsetText(str);

  tfTxtsetText();

  try {

  //Systemoutprintln(s);

  doswriteUTF(str);

  dosflush();

  //dosclose();

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  }

  private class RecvThread implements Runnable {

  public void run() {

  try {

  while(bConnected) {

  String str = disreadUTF();

  //Systemoutprintln(str);

  taContentsetText(taContentgetText() + str + \n);

  }

  } catch (SocketException e) {

  Systemoutprintln(退出了bye!);

  } catch (EOFException e) {

  Systemoutprintln(推出了bye bye!);

  } catch (IOException e) {

  eprintStackTrace();

  }

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25933.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.