每天多一点之Java网络编程基础
1.什么是socket
所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄。应用程序通常通过"套接字"向网络发出请求或者应答网络请求。
2.如何开发一个Server-Client模型的程序
开发原理:
服务器,使用ServerSocket监听指定的端口,端口可以随意指定(由于1024以下的端口通常属于保留端口,在一些操作系统中不可以随意使用,所以建议使用大于1024的端口),等待客户连接请求,客户连接后,会话产生;在完成会话后,关闭连接。
客户端,使用Socket对网络上某一个服务器的某一个端口发出连接请求,一旦连接成功,打开会话;会话完成后,关闭Socket。客户端不需要指定打开的端口,通常临时的、动态的分配一个1024以上的端口。
3.简单的一对一Server-Client聊天案例
Server:
package com.bxtang.serverandclient; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; /** * * @Description Server * @author HackerD * @version 1.0 * @CreateAt 2013-5-29 */ public class Server { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream inputStream = null; OutputStream outputStream = null; Scanner in = null; String message = null; try { serverSocket = new ServerSocket(8000); socket = serverSocket.accept(); System.out.println("客户端连接成功,可以开始聊天了!"); inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); byte[] b = new byte[1024]; while (true) { int i = inputStream.read(b); System.out.println("客户端对你说:" + new String(b, 0, i)); in = new Scanner(System.in); System.out.print("请输入你要对客户端说的话:"); message = in.next(); outputStream.write(message.getBytes()); outputStream.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { System.out.println("服务端退出了!"); if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }Client:
package com.bxtang.serverandclient; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Scanner; /** * * @Description 客户端 * @author HackerD * @version 1.0 * @CreateAt 2013-5-29 */ public class Client { public static void main(String[] args) { Socket clientSocket = null; OutputStream outputStream = null; InputStream inputStream = null; Scanner in = null; String message = null; try { clientSocket = new Socket("127.0.0.1", 8000); outputStream = clientSocket.getOutputStream(); inputStream = clientSocket.getInputStream(); message = "Hello,Server!This is Client!"; outputStream.write(message.getBytes()); outputStream.flush(); byte[] b = new byte[1024]; while (true) { int i = inputStream.read(b); System.out.println("服务端对你说:" + new String(b, 0, i)); in = new Scanner(System.in); System.out.print("请输入你要对服务端说的话:"); message = in.next(); outputStream.write(message.getBytes()); outputStream.flush(); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { System.out.println("服务器关闭了!"); }finally { System.out.println("客户端退出了!"); if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
4.简单的Swing多人聊天案例:
完善了再发