Java网络编程入门
1.进程之间的通信
运行EchoPlayer就启动一个进程,就启动了JVM进程,该进程(EchoPlayer类)执行main方法 ,echo负责生成响应结果,若想要将响应结果移动到一个远程主机上,那么此进程是无法完成的,这种情况需要创建两个程序:客户程序EchoClient和服务器程序EchoServer
客户程序EchoClient负责与用户交互,从本地获取标准输入和标准输出,和远程服务器程序EchoServer通信,发送用户输入的信息,接收返回的信息并将结果输出到标准输出。
服务器程序EchoServer负责接收客户程序EchoClient发送的信息,然后把响应结果发送给客户程序EchoClient。
1 public class EchoPlayer { 2 3 //负责生成相应结果 4 public String echo(String msg){ 5 return "echo : "+msg; 6 } 7 8 public void talk() throws IOException{ 9 System.out.println("please input String :"); 10 BufferedReader bt =new BufferedReader(new InputStreamReader(System.in)); 11 String msg=null; 12 while((msg=bt.readLine())!=null){ 13 if(msg.equals("bye")){ 14 System.out.println(echo(msg)); 15 break; 16 }else{ 17 System.out.println(echo(msg)); 18 System.out.println("please input String :"); 19 } 20 } 21 } 22 public static void main(String[] args) throws IOException { 23 // TODO Auto-generated method stub 24 new EchoPlayer().talk(); 25 } 26 27 }
2.用Java编写客户/服务器程序
Java网络程序都建立在TCP/IP协议的基础上,致力于实现应用层。传输层向应用层提供了套接字Socket接口,Socket封装了下层数据传输细节,应用层的程序通过Socket来建立于远程主机的连接,以及进行数据传输。
Java中有3种套接字类:Socket、ServerSocket、DatagrameSocket,其中前两种为TCP协议基础上的,后一种为UDP协议基础上
服务器代码:
1 public class EchoServer { 2 3 private int port = 8000; 4 private ServerSocket serverSocket; 5 6 //利用构造器初始化服务器 7 public EchoServer() throws IOException{ 8 serverSocket = new ServerSocket(port); 9 System.out.println("服务器启动"); 10 } 11 12 //响应输出信息 13 public String echo(String msg){ 14 return "Server echo : "+msg; 15 } 16 17 private PrintWriter getWriter(Socket socket) throws IOException{ 18 OutputStream socketOut = socket.getOutputStream(); 19 return new PrintWriter(socketOut,true); 20 } 21 22 private BufferedReader getReader(Socket socket)throws IOException{ 23 InputStream socketIn = socket.getInputStream(); 24 return new BufferedReader(new InputStreamReader(socketIn)); 25 } 26 27 public void service(){ 28 while(true){ 29 Socket socket=null; 30 try { 31 //等待客户连接 32 socket = serverSocket.accept(); 33 System.out.println("new connection accepted "+socket.getInetAddress()+":"+socket.getPort()); 34 BufferedReader br= getReader(socket); 35 PrintWriter pw = getWriter(socket); 36 String msg = null; 37 while((msg=br.readLine())!=null){ 38 System.out.println(msg); 39 pw.println(echo(msg)); 40 if(msg.equals("bye")){ 41 break; 42 } 43 } 44 45 46 } catch (IOException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 } 52 53 public static void main(String[] args) throws IOException { 54 new EchoServer().service(); 55 } 56 }
客户端代码
1 public class EchoClient { 2 3 private int port=8000; 4 private String host = "localhost"; 5 private Socket socket; 6 7 public EchoClient() throws IOException{ 8 socket = new Socket(host, port); 9 } 10 11 private PrintWriter getWriter(Socket socket) throws IOException{ 12 OutputStream socketOut = socket.getOutputStream(); 13 return new PrintWriter(socketOut, true); 14 } 15 16 private BufferedReader getReader(Socket socket) throws IOException{ 17 InputStream socketIn = socket.getInputStream(); 18 return new BufferedReader(new InputStreamReader(socketIn)); 19 } 20 21 public void talk(){ 22 try { 23 BufferedReader br = getReader(socket); 24 PrintWriter pw = getWriter(socket); 25 BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in)); 26 String msg=null; 27 while((msg = localReader.readLine())!=null){ 28 pw.println(msg); 29 System.out.println(br.readLine()); 30 if(msg.equals("bye")){ 31 break; 32 } 33 } 34 } catch (IOException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 public static void main(String[] args) throws IOException { 40 // TODO Auto-generated method stub 41 new EchoClient().talk(); 42 } 43 44 }