1.Java.net 包提供若干支持基于套接字的客户端/服务器通讯的类。java
2.java.net包中常有的类有Socket、ServerSocket、DatagramPacket、InetAddress、URL、URLConnection和URLEncoder等编程
3.为了监听客户端的链接请求,可使用ServerSocket类。Socket类实现用于网络上进程间通讯的套接字.DatagramSocket类使用UDP协议实现客户端和服务器套接字。DatagramPacket类使用DatagramSocket类的对象封装设置和收到的收据情报。InetAddress类表示Internet地址。在建立数据报报文和Socket对象时,可使用InetAddress类。服务器
,Socket通讯模式图网络
3.2个断点在TCP协议的Socket编程中,常常一个做为客户端,一个做为服务器端。也就是client-server模型,如上图;socket
4.Socket类this
1)Socket对象在客户端和服务器之间创建链接。构造方法建立套接字,并将套接字链接至给定的主机和端口。如下是与此Socket对象关联的构造方法和一些经常使用方法。spa
(1)构造方法.net
第一种构造方法以主机名和端口号做为参数来建立一个Socket对象。建立Socket对象时可能抛出异常UnknowHostException或IOException异常,必须捕捉它们。线程
Socket s=new Socket(Hhost,port);code
两一种构造方法以InetAddress对象和端口做为参数来建立一个Socket对象。建立Socket对象时可能抛出异常UnknowHostException或IOException异常,必须捕捉它们。
Socket s=new Socket(address,port);
(2)经常使用方法
InetAddress getInetAddress();//返回与Socket对象关联的InetAddress
int getPort();//返回此Socket 对象所链接的远程端口
int getLocalPort();//返回此Socket对象所链接的本地端口
InputStream getInputStream();//返回与此套接关联的InputStream
OutputStream getOutputStream();//返回于此套接关联的OutputStream
void close();//关闭该Socket
5.ServerSocket类
ServerSocket对象等待客户创建链接,链接创建之后进行通讯。
1)构造方法
第一种构造方法接收端口号做为参数建立ServerSocket对象。建立此对象时,可能抛出IOExption异常,必须捕捉和处理。
ServerSocket ss=new ServerSocket(port);
另外一种构造方法 接收端口号和最大队列长度做为参数。队列长度表示系统在拒绝链接前能够拥有的最大客户端数。
ServerSocket ss=new ServerSocket(port,maxqu);
Socket类中列出的方法也适用于ServerSocket类。此外,ServerSocket类具备的方法有accept(),此方法用于等待客户端触发通讯,这样Socket对象就可用于进一步的数据传送。
6.实现单用户登陆
Socket 网络编程通常能够划分以下4个步骤进行:
(1)创建链接
(2)打开Socket关联的输入/输出流
(3)从数据流中写入信息和读取信息
(4)关闭全部的数据流和Socket
.
/** * 客户端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { //创建客户端Socket链接,指定服务器的位置为本机当即端口8800 Socket socket=new Socket("localhost",8800); OutputStream os=socket.getOutputStream(); InputStream is = socket.getInputStream(); String str="用户名:tom,用户密码:123456"; os.write(str.getBytes()); socket.shutdownOutput(); //接收服务器端的响应,即从输入流读取信息 String reply=null; BufferedReader br=new BufferedReader(new InputStreamReader(is)); while ((reply=br.readLine())!=null){ System.out.println("我是客户端,服务器的响应为:"+reply); } br.close(); is.close(); os.close(); }
/** * @param args * 服务端 * @throws IOException */ public static void main(String[] args) throws IOException { //创建一个服务器Socket(ServerSocket),指定端口8800bin开始监听 ServerSocket serverSocket=new ServerSocket(8800); //使用accept()方法获得Socket对象 Socket socket = serverSocket.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是服务器,客户的信息是:"+info); } String reply="欢迎你,登陆成功!"; os.write(reply.getBytes()); os.close(); br.close(); is.close();
2)把对象传递,建一个Student类里面有三个属性和set/get方法
/** * 客户端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { Socket socket=new Socket("localhost",8800); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); //序列化对象 ObjectOutputStream oos=new ObjectOutputStream(os); Student student=new Student(); student.setID(0); student.setName("小明"); student.setSEX("男"); oos.writeObject(student); socket.shutdownOutput(); //接收服务器端的响应,即从输入流读取信息 BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是客户端,服务器的信息是:"+info); } br.close(); oos.close(); is.close(); os.close(); }
/** * @param args * 服务端 * @throws IOException * @throws ClassNotFoundException */ public static void main(String[] args) throws IOException, ClassNotFoundException { //创建一个服务器Socket(ServerSocket),指定端口8800bin开始监听 ServerSocket serverSocket=new ServerSocket(8800); //使用accept()方法获得Socket对象 Socket socket = serverSocket.accept(); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); ObjectInputStream ois=new ObjectInputStream(is); Student student =(Student) ois.readObject(); if (student!=null) { System.out.println("我是服务器端,客户端的信息是"+student.getID()+student .getName()+student.getSEX()); } String str="欢迎使用!"; os.write(str.getBytes()); os.close(); ois.close(); is.close(); } }
3)实现多个客户端用户登陆
/** * 客户端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { Socket socket=new Socket("localhost",8800); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); //序列化对象g ObjectOutputStream oos=new ObjectOutputStream(os); Student student2=new Student(); student2.setID(2); student2.setName("小明"); student2.setSEX("男"); oos.writeObject(student2); socket.shutdownOutput(); //接收服务器端的响应,即从输入流读取信息 BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是客户端,服务器的信息是:"+info); } br.close(); oos.close(); is.close(); os.close(); }
//创建多个服务器Socket(ServerSocket),指定端口8800bin开始监听 ServerSocket serverSocket=new ServerSocket(8800);
public class LoginThread extends Thread{ Socket socket=null; //每启动一个线程,链接响应的Socket public LoginThread(Socket socket){ this.socket=socket; } //启动线程,即响应客户请求 public void run(){ try { InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); ObjectInputStream ois=new ObjectInputStream(is); Student student; try { student = (Student) ois.readObject(); if (student!=null) { System.out.println("我是服务器端,客户端的信息是"+student.getID()+student .getName()+student.getSEX()); } String str="欢迎使用!"; os.write(str.getBytes()); os.close(); ois.close(); is.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } }
/** * 客户端 * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { Socket socket=new Socket("localhost",8800); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); //序列化对象 ObjectOutputStream oos=new ObjectOutputStream(os); Student student=new Student(); student.setID(0); student.setName("小明"); student.setSEX("男"); oos.writeObject(student); socket.shutdownOutput(); //接收服务器端的响应,即从输入流读取信息 BufferedReader br=new BufferedReader(new InputStreamReader(is)); String info=null; while ((info=br.readLine())!=null) { System.out.println("我是客户端,服务器的信息是:"+info); } br.close(); oos.close(); is.close(); os.close(); }
Socket socket=null; while (true) { socket=serverSocket.accept(); LoginThread loginThread=new LoginThread(socket); loginThread.start(); }