首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java怎么通过socket实现服务端与客户端交互

2012-09-01 
java如何通过socket实现服务端与客户端交互? 刚刚开始学习如何通过socket去发送信息下边的案例也是书上的

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

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

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

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

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

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

?

服务端类代码:

/** * <p>作者 Administrator</p> * <p>功能描述:</p> * <p>创建时间:20122012-2-2上午11:06:05</p> */package com.scok; import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;/** * <p>作者 Administrator</p> * <p>功能描述:</p> * <p>创建时间:20122012-2-2上午11:06:05</p> *//** * @author Administrator * */public class ClientServer {  public static void main(String[] args) {           //服务器接口类           ServerSocket sst=null;           //套接口类           Socket sc=null;                      InputStream is=null;           OutputStream os=null;           //dataInput为input的子类           DataInputStream in=null;           //printStream为output的子类           PrintStream ps=null;           try {                   //构造对象端口为8000                   sst =new ServerSocket(8000);                   //建立套接口                   sc=sst.accept();                   //获取套接口的输入流                   is=sc.getInputStream();                   //构造数据的输入流datainput对象in                   in=new DataInputStream(is);                   //获取套接口的输出流                   os=sc.getOutputStream();                   //构造数据的输出流PrintStream对象os                   ps=new PrintStream(os);                   //获取客户端的IP           InetAddress Ip=sc.getInetAddress();           //显示ip           System.out.println("连接地址ip:"+Ip);           int port;           port=sc.getPort();           System.out.println("客户端端口"+port);           ps.println("Welcome");           //在in上读取一行           String str=in.readLine();          while(!str.equals("quit")){                    System.out.println("客户说:"+str);          str=in.readLine();          }            System.out.println("客户离开");                                 } catch (IOException e) {                   e.printStackTrace();         }finally{                   //关闭                   try {                   is.close();                   os.close();                   sc.close();                   System.exit(0);                   } catch (IOException e) {                            e.printStackTrace();                   }                            }}}

?客户端类:

?

?

?

/** * <p>作者 Administrator</p> * <p>功能描述:</p> * <p>创建时间:20122012-2-2上午11:18:47</p> */package com.scok; import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException; /** * <p>作者 Administrator</p> * <p>功能描述:</p> * <p>创建时间:20122012-2-2上午11:18:47</p> *//** * @author Administrator * */public class Client {public static void main(String[] args) {            //套接口类           Socket sc=null;           InputStream is=null;           OutputStream os=null;           //dataInput为input的子类           DataInputStream in=null;           //printStream为output的子类           PrintStream ps=null;           String str=null;                      try {                     //创建客户端接口           sc=new Socket("192.168.12.48",8000);           System.out.println("come to server..");           is=sc.getInputStream();           os=sc.getOutputStream();           in=new DataInputStream(is);           ps=new PrintStream(os);           str=in.readLine();           System.out.println("server 说"+str);           byte bt[]=new byte[20];           System.in.read(bt);           String msg=new String (bt,0);           msg=msg.trim();           while(!msg.equals("quit")){                     ps.println(msg);                     System.in.read(bt);                     msg=new String(bt,0);                     msg=msg.trim();           }           //传输信息到服务端           ps.println(msg);           } catch (UnknownHostException e) {                   e.printStackTrace();         } catch (IOException e) {                   e.printStackTrace();         }finally{                   //关闭                   try {                   is.close();                   os.close();                   sc.close();                   System.exit(0);                   } catch (IOException e) {                            e.printStackTrace();                   }                            }}}

?

?

?

?

?

热点排行