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); } } } ///:~