- 2
- 0
- 约1.07万字
- 约 48页
- 2017-06-28 发布于湖北
- 举报
第十一章 Java的网络功能 主要内容: 1、了解URL的构成,学会利用URL读取网络资源 2、熟悉包 3、了解TCP/IP协议 4、掌握Socket的基本使用方法,学会建立Socket连接,实现客户端和服务器端通信 5、掌握利用UDP通信的方法 6、了解多客户端通信概念,掌握进行多客户通信的方法 11.1基本知识 11.1.1 计算机网络就是利用通信线路连接起来的、相互独立的,计算机集合 11.1.2 计算机协议就是针对计算机进行通信时,关于信息交互方式、交互秩序等所作出的规定。 常用协议: TCP、IP、UDP、SNMP、SMTP、FTP、HTTP 11.2网络功能与使用方法 11.2.2 Socket 是指两个程序通过网络进行通信的一种方式。在TCP/ IP协议下,客户端和服务器端通常使用socket来进行信息交流。这种方式也是传统中常用的一种方式。 URL对象是通过定义在J包中的URL类来进行构造的。构造方法有四种: 1.Public URL(String spec) spec--表示URL的字符串 例:URL url1=new URL(“/”) 2.Public URL(URL context,String spec) 用于表示相对URL位置的定义。 例:URL url2=new URL(getCodeBase(),”myfile.text”) 3.public URL(String protocol, String host,String file) 4. public URL(String protocol, String host,int port,String file) String protocol--协议 String host--主机名 int port--端口 String file--文件名 收到客户套接字后,放到一个Socket对象sc中,那么服务器套接字sc就是客户端套接字mysocket。 服务器端也可以通过服务器套接字sc使用方法 getInputStream() 获得一个输出流有,用于向客户端发送信息 使用方法 getOutputStream() 获得一个输入流,用于接收客户端发来的信息 catch(IOException e){ System.out.println(not connected);} System.out.println(s); } } while(true) { s=in.readUTF(); if(s!=null) break; } out.writeUTF(Hello,i am server.); you.close(); } catch(IOException e){ System.out.println(Erro:+e);} }} public void run() { try { DataInputstream in = new DataInputstream (incoming.getInputStream()); DataOutputStream out = new DataOutputStream (incoming.getOutputStream()); out.println( Hello! Enter BYE to exit. ); boolean done = false; while (!done) { String str = in.readLine(); if (str == null) done = true; 。。。 例:下面为一接收数据报程序 public class UDPGet extends Frame implements ActionListener {TextArea textArea1=new TextArea(); Button button1=new Button(); public static void main(String args[]) { UDPGet get=new UDPGet(); get.setLocation(100,100); get.setSize(300,200); get.show()
原创力文档

文档评论(0)