java如何通过socket实现服务端与客户端交互

刚刚开始学习如何通过socket去发送信息下边的案例也是书上的在这留下笔记

   Socket API:java .net.Socket继承于java.lang.Object,有八个构造器,其方法并不多,下面介绍使用最频繁的三个方法,其它方法大家可以见JDK文档。

  Accept方法用于产生"阻塞",直到接受到一个连接,并且返回一个客户端的Socket对象实例。"阻塞"是一个术语,它使程序运行暂时"停留"在这个地方,直到一个会话产生,然后程序继续;通常"阻塞"是由循环产生的。

  getInputStream方法获得网络连接输入,同时返回一个InputStream对象实例。

getOutputStream方法连接的另一端将得到输入,同时返回一个OutputStream对象实例。注意:其中getInputStream和getOutputStream方法均可能会产生一个IOException,它必须被捕获,因为它们返回的流对象,通常都会被另一个流对象使用。

 

服务端类代码:

Java代码   收藏代码
  1. /** 
  2.  
  3.  * <p>作者 Administrator</p> 
  4.  
  5.  * <p>功能描述:</p> 
  6.  
  7.  * <p>创建时间:20122012-2-2上午11:06:05</p> 
  8.  
  9.  */  
  10.   
  11. package com.scok;  
  12.   
  13.    
  14.   
  15. import java.io.DataInputStream;  
  16.   
  17. import java.io.IOException;  
  18.   
  19. import java.io.InputStream;  
  20.   
  21. import java.io.OutputStream;  
  22.   
  23. import java.io.PrintStream;  
  24.   
  25. import java.net.InetAddress;  
  26.   
  27. import java.net.ServerSocket;  
  28.   
  29. import java.net.Socket;  
  30.   
  31. /** 
  32.  
  33.  * <p>作者 Administrator</p> 
  34.  
  35.  * <p>功能描述:</p> 
  36.  
  37.  * <p>创建时间:20122012-2-2上午11:06:05</p> 
  38.  
  39.  */  
  40.   
  41. /** 
  42.  
  43.  * @author Administrator 
  44.  
  45.  * 
  46.  
  47.  */  
  48.   
  49. public class ClientServer {  
  50.   
  51.   public static void main(String[] args) {  
  52.   
  53.            //服务器接口类  
  54.   
  55.            ServerSocket sst=null;  
  56.   
  57.            //套接口类  
  58.   
  59.            Socket sc=null;  
  60.   
  61.              
  62.   
  63.            InputStream is=null;  
  64.   
  65.            OutputStream os=null;  
  66.   
  67.            //dataInput为input的子类  
  68.   
  69.            DataInputStream in=null;  
  70.   
  71.            //printStream为output的子类  
  72.   
  73.            PrintStream ps=null;  
  74.   
  75.            try {  
  76.   
  77.                    //构造对象端口为8000  
  78.   
  79.                    sst =new ServerSocket(8000);  
  80.   
  81.                    //建立套接口  
  82.   
  83.                    sc=sst.accept();  
  84.   
  85.                    //获取套接口的输入流  
  86.   
  87.                    is=sc.getInputStream();  
  88.   
  89.                    //构造数据的输入流datainput对象in  
  90.   
  91.                    in=new DataInputStream(is);  
  92.   
  93.                    //获取套接口的输出流  
  94.   
  95.                    os=sc.getOutputStream();  
  96.   
  97.                    //构造数据的输出流PrintStream对象os  
  98.   
  99.                    ps=new PrintStream(os);  
  100.   
  101.                    //获取客户端的IP  
  102.   
  103.            InetAddress Ip=sc.getInetAddress();  
  104.   
  105.            //显示ip  
  106.   
  107.            System.out.println("连接地址ip:"+Ip);  
  108.   
  109.            int port;  
  110.   
  111.            port=sc.getPort();  
  112.   
  113.            System.out.println("客户端端口"+port);  
  114.   
  115.            ps.println("Welcome");  
  116.   
  117.            //在in上读取一行  
  118.   
  119.            String str=in.readLine();  
  120.   
  121.           while(!str.equals("quit")){  
  122.   
  123.                     System.out.println("客户说:"+str);  
  124.   
  125.           str=in.readLine();  
  126.   
  127.           }   
  128.   
  129.            System.out.println("客户离开");  
  130.   
  131.              
  132.   
  133.              
  134.   
  135.            } catch (IOException e) {  
  136.   
  137.                    e.printStackTrace();  
  138.   
  139.          }finally{  
  140.   
  141.                    //关闭  
  142.   
  143.                    try {  
  144.   
  145.                    is.close();  
  146.   
  147.                    os.close();  
  148.   
  149.                    sc.close();  
  150.   
  151.                    System.exit(0);  
  152.   
  153.                    } catch (IOException e) {  
  154.   
  155.                             e.printStackTrace();  
  156.   
  157.                    }  
  158.   
  159.                      
  160.   
  161.          }  
  162.   
  163. }  
  164.   
  165. }  

 客户端类:

 

 

 

Java代码   收藏代码
  1. /** 
  2.  
  3.  * <p>作者 Administrator</p> 
  4.  
  5.  * <p>功能描述:</p> 
  6.  
  7.  * <p>创建时间:20122012-2-2上午11:18:47</p> 
  8.  
  9.  */  
  10.   
  11. package com.scok;  
  12.   
  13.    
  14.   
  15. import java.io.DataInputStream;  
  16.   
  17. import java.io.IOException;  
  18.   
  19. import java.io.InputStream;  
  20.   
  21. import java.io.OutputStream;  
  22.   
  23. import java.io.PrintStream;  
  24.   
  25. import java.net.ServerSocket;  
  26.   
  27. import java.net.Socket;  
  28.   
  29. import java.net.UnknownHostException;  
  30.   
  31.    
  32.   
  33. /** 
  34.  
  35.  * <p>作者 Administrator</p> 
  36.  
  37.  * <p>功能描述:</p> 
  38.  
  39.  * <p>创建时间:20122012-2-2上午11:18:47</p> 
  40.  
  41.  */  
  42.   
  43. /** 
  44.  
  45.  * @author Administrator 
  46.  
  47.  * 
  48.  
  49.  */  
  50.   
  51. public class Client {  
  52.   
  53. public static void main(String[] args) {  
  54.   
  55.    
  56.   
  57.            //套接口类  
  58.   
  59.            Socket sc=null;  
  60.   
  61.            InputStream is=null;  
  62.   
  63.            OutputStream os=null;  
  64.   
  65.            //dataInput为input的子类  
  66.   
  67.            DataInputStream in=null;  
  68.   
  69.            //printStream为output的子类  
  70.   
  71.            PrintStream ps=null;  
  72.   
  73.            String str=null;  
  74.   
  75.              
  76.   
  77.            try {  
  78.   
  79.                      //创建客户端接口  
  80.   
  81.            sc=new Socket("192.168.12.48",8000);  
  82.   
  83.            System.out.println("come to server..");  
  84.   
  85.            is=sc.getInputStream();  
  86.   
  87.            os=sc.getOutputStream();  
  88.   
  89.            in=new DataInputStream(is);  
  90.   
  91.            ps=new PrintStream(os);  
  92.   
  93.            str=in.readLine();  
  94.   
  95.            System.out.println("server 说"+str);  
  96.   
  97.            byte bt[]=new byte[20];  
  98.   
  99.            System.in.read(bt);  
  100.   
  101.            String msg=new String (bt,0);  
  102.   
  103.            msg=msg.trim();  
  104.   
  105.            while(!msg.equals("quit")){  
  106.   
  107.                      ps.println(msg);  
  108.   
  109.                      System.in.read(bt);  
  110.   
  111.                      msg=new String(bt,0);  
  112.   
  113.                      msg=msg.trim();  
  114.   
  115.            }  
  116.   
  117.            //传输信息到服务端  
  118.   
  119.            ps.println(msg);  
  120.   
  121.            } catch (UnknownHostException e) {  
  122.   
  123.                    e.printStackTrace();  
  124.   
  125.          } catch (IOException e) {  
  126.   
  127.                    e.printStackTrace();  
  128.   
  129.          }finally{  
  130.   
  131.                    //关闭  
  132.   
  133.                    try {  
  134.   
  135.                    is.close();  
  136.   
  137.                    os.close();  
  138.   
  139.                    sc.close();  
  140.   
  141.                    System.exit(0);  
  142.   
  143.                    } catch (IOException e) {  
  144.   
  145.                             e.printStackTrace();  
  146.   
  147.                    }  
  148.   
  149.                      
  150.   
  151.          }  
  152.   
  153. }  
  154.   
  155. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值