《Java_Socket网络编程大全》由会员分享,可在线阅读,更多相关《Java_Socket网络编程大全(70页珍藏版)》请在金锄头文库上搜索。
1、武汉软件工程职业学院理论课程教案(首页)(第 页)11.简单服务器端 /* import .*; import java.io.*; */ ServerSocket server=null; try server=new ServerSocket(%1); catch(Exception e) System.out.println(“不能监听:“+e.toString(); Socket socket=null; try socket=server.accept(); BufferedReader %3=new BufferedReader(new InputStreamReader(sock
2、et.getInputStream(); PrintWriter %4=new PrintWriter(socket.getOutputStream(); String %2=%3.readLine(); %4.println(“); %4.flush(); %4.close(); %3.close(); catch(IOException e) System.out.println(“出错:“+e.toString(); finally try if(socket!=null) socket.close(); server.close(); catch(IOException e) e.pr
3、intStackTrace(); 2.简单客户端 /* import .*; import java.io.*; */ Socket socket=null; try (第 页)2socket=new Socket(%1,%2); PrintWriter %3=new PrintWriter(socket.getOutputStream(); BufferedReader %4 = new BufferedReader(new InputStreamReader(socket.getInputStream(); %3.println(“); %3.flush(); String %5=%4.r
4、eadLine(); %6 %3.close(); %4.close(); catch(Exception e) e.printStackTrace(); finally try socket.close(); catch(IOException e) e.printStackTrace(); 3.获得本机IP /import .*; String strIP = null; try strIP =InetAddress.getLocalHost().getHostAddress().toString(); catch(UnknownHostException e) e.printStackT
5、race(); /* %1=InetAddress.getLocalHost().getHostAddress(); Enumeration netInterfaces = null; try netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements() NetworkInterface ni = netInterfaces.nextElement(); System.out.println(“DisplayName:“ + ni.getDisplayName();
6、 System.out.println(“Name:“ + ni.getName(); Enumeration ips = ni.getInetAddresses(); while (ips.hasMoreElements() (第 页)3System.out.println(“IP:“ + ips.nextElement().getHostAddress(); catch (Exception e) e.printStackTrace(); */4.端对端通信 /import .*; byte buf=new byte1024; DatagramSocket ds=new DatagramS
7、ocket(%1); DatagramPacket ip=new DatagramPacket(buf,buf.length); while(true) ds.receive(ip); InetAddress target=InetAddress.getByName(%2); DatagramSocket ds=new DatagramSocket(%3); String str=%4; byte buf=str.getBytes(); DatagramPacket op=new DatagramPacket(buf,buf.length,target,%5); ds.send(op); ds
8、.close();5.点对点通信 /* import java.io.*; import .*; */ public class %6 extends Thread Override public void run() ServerSocket server = null; try server = new ServerSocket(5000); catch (Exception e) System.out.println(“不能监听:“ + e.toString(); Socket socket = null; try socket = server.accept(); BufferedRe
9、ader req = new BufferedReader(new InputStreamReader((第 页)4socket.getInputStream(); PrintWriter os = new PrintWriter(socket.getOutputStream(); Debug.p(req.readLine(); os.println(“Server“); os.flush(); os.close(); req.close(); catch (IOException e) System.out.println(“出错:“ + e.toString(); finally try
10、if (socket != null) socket.close(); server.close(); catch (IOException e) e.printStackTrace(); Thread t = new %6(); t.start(); String strIP = null; try strIP = InetAddress.getLocalHost().getHostAddress().toString(); catch (UnknownHostException e) e.printStackTrace(); Socket socket = null; try socket
11、 = new Socket(strIP, 4000); PrintWriter pw = new PrintWriter(socket.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(socket .getInputStream(); pw.println(“Client“); pw.flush(); Debug.p(br.readLine(); pw.close(); br.close(); catch (Exception e) e.printStackTrace();(第 页)
12、5 finally try socket.close(); catch (IOException e) e.printStackTrace(); 6.UDP对时服务器端 /* import java.io.*; import .*; import java.util.*; */ public class UDPServer extends Thread Override public void run() String strIP = null; try strIP = InetAddress.getLocalHost().toString(); catch (UnknownHostExcep
13、tion e) e.printStackTrace(); DatagramSocket ds; try ds = new DatagramSocket(5000); byte buf = new byte1024; DatagramPacket dpt = new DatagramPacket(buf, buf.length); ds.receive(dpt); String Read_str = new String(buf, 0, dpt.getLength(); if (Read_str.equals(“TimeNow“) Date t = new Date(); byte sendDa
14、ta = String.valueOf(“cmd date “ +(t.getYear()+1900)+“-“+(t.getMonth()+1)+“-“+t.getDate()+“ DatagramPacket dp = new DatagramPacket(sendData, sendData.length, InetAddress.getLocalHost(), 2000); ds.send(dp); ds.close(); catch (SocketException e) e.printStackTrace(); catch (IOException e) (第 页)6e.printS
15、tackTrace(); Thread t = new UDPServer(); t.start();7.UDP对时客户端 /* import java.io.*; import .*; */ String strIP = null; try strIP = InetAddress.getLocalHost().getHostAddress().toString(); catch (UnknownHostException e) e.printStackTrace(); byte buf = new byte1024; DatagramSocket ds; try ds = new DatagramSocket(2000); DatagramPacket ip = new DatagramPacket(buf, buf.length); InetAddress target = InetAddress.getByName(strIP); St