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

Java socket應用的幾個小實例

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

  用於本機上的客戶端與服務器對話的小程序

  客戶端

  import javaioBufferedReader;

  import javaioBufferedWriter;

  import javaioInputStreamReader;

  import javaioOutputStreamWriter;

  import Socket;

  public class Client extends Thread

  {

  public void Write() throws Exception

  {

  Socket s=new Socket();

  BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sgetOutputStream()));

  BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));

  String message=客戶端說:;

  while(!(messageequals()))

  {

  message=brreadLine();

  Systemoutprintln(客戶端說:+message);

  bwwrite(message+\n);

  bwflush();

  }

  bwclose();

  sclose();

  }

  public void run()

  {

  Socket m=null;

  BufferedReader dr=null;

  try

  {

  m=new Socket();

  dr=new BufferedReader(new InputStreamReader(mgetInputStream()));

  String msg=服務器說:;

  while(!(msgequals()))

  {

  msg=drreadLine();

  Systemoutprintln(服務器說:+msg);

  }

  }

  catch(Exception e){eprintStackTrace();}

  finally

  {

  try

  {

  drclose();

  mclose();

  }

  catch(Exception e){eprintStackTrace();}

  }

  }

  public static void main(String args[])throws Exception

  {

  Client c=new Client();

  cstart();

  cWrite();

  }

  }

  服務器

  import javaioBufferedReader;

  import javaioBufferedWriter;

  import javaioInputStreamReader;

  import javaioOutputStreamWriter;

  import ServerSocket;

  import Socket;

  public class Server extends Thread

  {

  private ServerSocket ss=null;

  public Server ()throws Exception

  {

  ss=new ServerSocket();

  }

  public void Write()throws Exception

  {

  Socket s=ssaccept();

  BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sgetOutputStream()));

  BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));

  String message=服務器說:;

  while(!(messageequals()))

  {

  message=brreadLine();

  Systemoutprintln(服務器說:+message);

  bwwrite(message+\n);

  bwflush();

  }

  //bwclose();

  sclose();

  }

  public void run()

  {

  Socket m=null;

  BufferedReader dr=null;

  try

  {

  m=ssaccept();

  dr=new BufferedReader(new InputStreamReader(mgetInputStream()));

  String msg=客戶端說:;

  while(!(msgequals()))

  {

  msg=drreadLine();

  Systemoutprintln(客戶端:+msg);

  }

  }

  catch(Exception e){eprintStackTrace();}

  finally

  {

  try

  {

  drclose();

  mclose();

  }

  catch(Exception e){eprintStackTrace();}

  }

  }

  public static void main(String args[])throws Exception

  {

  Server s=new Server();

  sstart();

  sWrite();

  }

  }

  用於本地群聊的小程序

  客戶端

  import javaioBufferedReader;

  import javaioBufferedWriter;

  import javaioInputStreamReader;

  import javaioOutputStreamWriter;

  import Socket;

  public class Friendclient extends Thread

  {

  public void Writer() throws Exception

  {

  Socket s=new Socket();

  BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sgetOutputStream()));

  BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));

  String message=本客戶端說:;

  while(!(messageequals(goodbye)))

  {

  message=brreadLine();

  Systemoutprintln(客戶端說:+message);

  bwwrite(message+\n);

  bwflush();

  }

  bwclose();sclose();

  }

  public void run()

  {

  BufferedReader bq=null;

  Socket st=null;

  try

  {

  st=new Socket();

  bq=new BufferedReader(new InputStreamReader(stgetInputStream()));

  String msg=其他客戶端說:;

  while(!(msgequals()))

  {

  msg=bqreadLine();

  Systemoutprintln(其他客戶端說:+msg);

  }

  }

  catch(Exception e){eprintStackTrace();}

  finally

  {

  try

  {

  bqclose();stclose();

  }

  catch(Exception e){eprintStackTrace();}

  }

  }

  public static void main(String args[])throws Exception

  {

  Friendclient ft=new Friendclient();

  ftstart();

  ftWriter();

  }

  }

  服務器端

  import javaioBufferedReader;

  import javaioBufferedWriter;

  import javaioInputStreamReader;

  import javaioOutputStreamWriter;

  import ServerSocket;

  import Socket;

  import javautilArrayList;

  public class Allserver

  {

  private ServerSocket ss=null;

  private static ArrayList<Socket> al=new ArrayList<Socket>();

  public Allserver() throws Exception

  {

  ss=new ServerSocket();

  while(true)

  {

  Socket s=ssaccept();

  aladd(s);

  new Mtherd(s)start();

  }

  }

  private class Mtherd extends Thread

  {

  private Socket sd=null;

  private BufferedReader br=null;

  //private BufferedWriter bw=null;

  public Mtherd(Socket sd)throws Exception

  {

  thissd=sd;

  br=new BufferedReader(new InputStreamReader(sdgetInputStream()));

  //bw=new BufferedWriter(new OutputStreamWriter(sdgetOutputStream()));

  }

  public void run()

  {

  try

  {

  String gh=;

  while(true)

  {

  gh=brreadLine();

  setAll(gh);

  }

  }

  catch(Exception e){eprintStackTrace();}

  }

  public void setAll(String x)throws Exception

  {

  Socket ss=null;

  BufferedWriter bv=null;

  for(int i=;i<alsize();i++)

  {

  ss=(Socket)alget(i);

  bv=new BufferedWriter(new OutputStreamWriter(ssgetOutputStream()));

  bvwrite(x+\n);

  bvflush();

  }

  }

  }

  public static void main(String args[])throws Exception

  {

  new Allserver();

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25528.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.