博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java socket
阅读量:4984 次
发布时间:2019-06-12

本文共 4425 字,大约阅读时间需要 14 分钟。

socke

Java.net.Socket 类 socket()  //创建一个未连接的套接字void connect(SocketAddress address)//将套接字连接到给定的地址void connect(SocketAddress address,int timeoutInMillisseconds)  //setTimeout 设置超时值,单位毫秒,如果超时没反应将返回void setSoTimeout(int timecoutInMilliseconds) //setTimeout 设置超时值,单位毫秒,如果之后的读写操作超时将抛出异常boolean isConnected() //如果套接字已连接,返回trueboolean isClosed() //如果套接字已关闭,返回true public void shutdownOutput() //将输出流设为结束 public void shutdownInput()  //将输入流设为"结束" public void isOutputShutdown() //如果输出流结束,返回true public void isInputShutdown()  //如果输入流"结束",返回true
 

 

Socket 单次连接实例

package test;//: MultiJabberClient.java //Client that tests the MultiJabberServer //by starting up multiple clients. import java.net.*; import java.io.*; public class MultiJabberClient {     public static void main(String[] args){    System.out.println("Making client " );      Socket socket = new Socket();      BufferedReader in;      PrintWriter out;      try {     //   socket =  //         new Socket(addr, 5000);          //InetAddress iis = Inet6Address.getByName("106.13.46.152");          //socket = new Socket(iis,5000);         SocketAddress address= new InetSocketAddress("106.13.46.152",5000);//获得连接端点       socket.connect(address,1000); //连接 参数1000是延时时间       // socket.setSoTimeout(100);//setTimeout 设置超时值,单位毫秒,如果之后的读写操作超时将抛出异常     } catch(IOException e) {        // If the creation of the socket fails,         // nothing needs to be cleaned up.      }      try {            in =           new BufferedReader(            new InputStreamReader(              socket.getInputStream()));        // Enable auto-flush:        out =           new PrintWriter(            new BufferedWriter(              new OutputStreamWriter(                socket.getOutputStream())), true);        for(int i = 0; i < 10; i++)       {           out.println("I" + ":" + i);           String str = in.readLine();           System.out.println(str);       }       out.println("END");     } catch(IOException e) {        // The socket should be closed on any         // failures other than the socket         // constructor:        try {          socket.close();        } catch(IOException e2) {}      }     }} ///:~

 

 

socket 多个客服端编程实例

package test;//: MultiJabberClient.java // Client that tests the MultiJabberServer // by starting up multiple clients. import java.net.*; import java.io.*;  class JabberClientThread extends Thread {   private Socket socket;   private BufferedReader in;   private PrintWriter out;   private static int counter = 0;   private int id = counter++;   private static int threadcount = 0;   public static int threadCount() {      return threadcount;    }   public JabberClientThread(InetAddress addr) {     System.out.println("Making client " + id);     threadcount++;     try {       socket =          new Socket(addr, 5000);     } catch(IOException e) {       // If the creation of the socket fails,        // nothing needs to be cleaned up.     }     try {           in =          new BufferedReader(           new InputStreamReader(             socket.getInputStream()));       // Enable auto-flush:       out =          new PrintWriter(           new BufferedWriter(             new OutputStreamWriter(               socket.getOutputStream())), true);       start();     } catch(IOException e) {       // The socket should be closed on any        // failures other than the socket        // constructor:       try {         socket.close();       } catch(IOException e2) {}     }     // Otherwise the socket will be closed by     // the run() method of the thread.   }   public void run() {     try {       for(int i = 0; i < 25; i++) {         out.println("Client " + id + ": " + i);         String str = in.readLine();         System.out.println(str);       }       out.println("END");     } catch(IOException e) {     } finally {       // Always close it:       try {         socket.close();       } catch(IOException e) {}       threadcount--; // Ending this thread     }   } }  public class MultiJabberClient {   static final int MAX_THREADS = 40;   public static void main(String[] args)        throws IOException, InterruptedException {     InetAddress addr =        InetAddress.getByName("106.13.46.152");     while(true) {       if(JabberClientThread.threadCount()           < MAX_THREADS)         new JabberClientThread(addr);       Thread.currentThread().sleep(100);     }   } } ///:~
 

转载于:https://www.cnblogs.com/jiangfeilong/p/10504029.html

你可能感兴趣的文章
多线程学习笔记五之读写锁实现分析
查看>>
linux内核分析(网课期末&地面课期中)
查看>>
Spring中的设计模式2
查看>>
vue项目向小程序迁移调研
查看>>
Jquery权威指南
查看>>
CSS hack大全(转)
查看>>
ZOJ - 3229 Shoot the Bullet (有源汇点上下界最大流)
查看>>
【14】redis
查看>>
蓝桥杯/第四届/猜年龄
查看>>
LeetCode-Letter Combinations of a Phone Number
查看>>
关于ubuntu的图形界面的关闭与开启
查看>>
Codeforces Round #400 E. The Holmes Children
查看>>
hdu 1759 Matrix Revolution(矩阵转BFS)
查看>>
LintCode-88.最近公共祖先
查看>>
WCF
查看>>
861. Score After Flipping Matrix
查看>>
青蛙的约会(扩展欧几里德)
查看>>
380. Insert Delete GetRandom O(1)
查看>>
6w5:第六周程序填空题2
查看>>
多线程——几中常用的线程池
查看>>