java tcp socket实例

java tcp socket实例

2364人阅读 评论(1) 收藏 举报
[java] view plain copy
  1. package com.network;  
  2. import java.io.BufferedInputStream;  
  3. import java.io.BufferedOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.OutputStream;  
  8. import java.io.OutputStreamWriter;  
  9. import java.net.InetSocketAddress;  
  10. import java.net.ServerSocket;  
  11. import java.net.Socket;  
  12. import java.util.Arrays;  
  13.   
  14.   
  15. public class ServerChat {  
  16.     /** 
  17.      * @param args 
  18.      * @throws IOException  
  19.      */  
  20.     public static void main(String[] args) throws IOException {  
  21.         // TODO Auto-generated method stub  
  22.           
  23.         InetSocketAddress isa = new InetSocketAddress("10.140.86.98",5858);  
  24.           
  25.         ServerSocket server = new ServerSocket();  
  26.           
  27.         server.bind(isa);  
  28.         System.out.println("isBound: " + server.isBound());  
  29.         System.out.println("SocketAddress: " + server.getLocalSocketAddress());  
  30.           
  31.         new Thread(new WorkingServer(server)).start();  
  32.           
  33.         while(true){  
  34.             Socket client = server.accept();  
  35.             System.out.println("this main thread");  
  36.               
  37.             InputStream in = client.getInputStream();  
  38.               
  39.             InputStreamReader read = new InputStreamReader(new BufferedInputStream(in));  
  40.               
  41.             OutputStream out = client.getOutputStream();  
  42.               
  43.             OutputStreamWriter write = new OutputStreamWriter( new BufferedOutputStream(out));  
  44.               
  45.             write.write("hello");  
  46.             write.flush();  
  47.               
  48.             char [] cbuf = new char[100];  
  49.             Arrays.fill(cbuf, '/0');  
  50.             int len = read.read(cbuf, 0100);  
  51.             StringBuilder sb = new StringBuilder(100);  
  52.             sb.append(cbuf, 0, len);  
  53.               
  54.             System.out.println(sb.toString());  
  55.               
  56.             read.close();  
  57.             client.close();  
  58.             write.close();  
  59.               
  60.         }  
  61.           
  62.           
  63.       
  64.           
  65.       
  66.           
  67.     }  
  68. }  

 

 

[java] view plain copy
  1. package com.network;  
  2. import java.io.BufferedInputStream;  
  3. import java.io.BufferedOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.OutputStream;  
  8. import java.io.OutputStreamWriter;  
  9. import java.net.ServerSocket;  
  10. import java.net.Socket;  
  11. import java.util.Arrays;  
  12. public class WorkingServer implements Runnable {  
  13.     private ServerSocket server;  
  14.       
  15.     WorkingServer(ServerSocket s){  
  16.         server = s;  
  17.     }  
  18.       
  19.     void communicat( Socket client) throws IOException{  
  20.         System.out.println("this sub thread: " + Thread.currentThread().getId());  
  21.           
  22.         InputStream in = client.getInputStream();  
  23.           
  24.         InputStreamReader read = new InputStreamReader(new BufferedInputStream(in));  
  25.           
  26.         OutputStream out = client.getOutputStream();  
  27.           
  28.         OutputStreamWriter write = new OutputStreamWriter( new BufferedOutputStream(out));  
  29.           
  30.         write.write("hello");  
  31.         write.flush();  
  32.           
  33.         char [] cbuf = new char[100];  
  34.         Arrays.fill(cbuf, '/0');  
  35.         int len = read.read(cbuf, 0100);  
  36.         StringBuilder sb = new StringBuilder(100);  
  37.         sb.append(cbuf, 0, len);  
  38.           
  39.         System.out.println(sb.toString());  
  40.           
  41.         read.close();  
  42. //      /client.close();  
  43.         write.close();  
  44.       
  45.     }  
  46.       
  47.     @Override  
  48.     public void run() {  
  49.         // TODO Auto-generated method stub  
  50.         System.out.println(Thread.currentThread());  
  51.         while(true){  
  52.             try {  
  53.                 Socket client = server.accept();  
  54.                 Thread.sleep(5000);  
  55.                 communicat(client);  
  56.             } catch (IOException e) {  
  57.                 // TODO Auto-generated catch block  
  58.                 e.printStackTrace();  
  59.             } catch (InterruptedException e) {  
  60.                 // TODO Auto-generated catch block  
  61.                 e.printStackTrace();  
  62.             }  
  63.         }  
  64.     }  
  65. }  

 

 

 

[java] view plain copy
    1. package com.network;  
    2. import java.io.IOException;  
    3. import java.io.InputStream;  
    4. import java.io.InputStreamReader;  
    5. import java.io.OutputStreamWriter;  
    6. import java.net.Socket;  
    7. import java.net.UnknownHostException;  
    8. public class ClientSock {  
    9.     /** 
    10.      * @param args 
    11.      * @throws IOException  
    12.      * @throws UnknownHostException  
    13.      */  
    14.     public static void main(String[] args) throws UnknownHostException, IOException {  
    15.         // TODO Auto-generated method stub  
    16.         Socket s = new Socket("10.140.86.98"5858);  
    17.           
    18.         System.out.println( "remote socket " + s.getRemoteSocketAddress());  
    19.           
    20.         InputStream in = s.getInputStream();  
    21.           
    22.         InputStreamReader reader = new InputStreamReader(in);  
    23.           
    24.         char [] cbuf = new char[100];  
    25.         int len = reader.read(cbuf);  
    26.         StringBuilder sb = new StringBuilder(100);  
    27.           
    28.         sb.append(cbuf, 0, len);  
    29.         System.out.println(sb.toString());  
    30.           
    31.         OutputStreamWriter writer = new OutputStreamWriter(s.getOutputStream());  
    32.           
    33.         writer.write("from client");  
    34.           
    35.         writer.close();  
    36.         reader.close();  
    37.         s.close();  
    38.           
    39.     }  

你可能感兴趣的:(socket)