一:基础的网络编程
1.InetAddress
1 import java.net.InetAddress; 2 public class Test128 { 3 public static void main(String[] args) throws Exception { 4 //inetMethod(); 5 inetMethod2(); 6 } 7 public static void inetMethod()throws Exception{ 8 //local的问题 9 InetAddress i=InetAddress.getLocalHost(); 10 String address=i.getHostAddress(); 11 String name=i.getHostName(); 12 System.out.println("address="+address); 13 System.out.println("name="+name); 14 } 15 public static void inetMethod2()throws Exception{ 16 //根据域名来处理 17 InetAddress ia=InetAddress.getByName("www.baidu.com"); 18 String address=ia.getHostAddress(); 19 String name=ia.getHostName(); 20 System.out.println("address="+address); 21 System.out.println("name="+name); 22 } 23 24 }
二:UDP
1.接收端
1 import java.net.DatagramPacket; 2 import java.net.DatagramSocket; 3 4 public class Test130 { 5 6 public static void main(String[] args) throws Exception { 7 reviceShow(); 8 9 } 10 public static void reviceShow()throws Exception{ 11 DatagramSocket ds=new DatagramSocket(10004); 12 byte[] buf=new byte[1024]; 13 DatagramPacket dp=new DatagramPacket(buf,buf.length); 14 ds.receive(dp); 15 String ip=dp.getAddress().getHostAddress(); 16 String data=new String(dp.getData(),0,dp.getLength()); 17 int port=dp.getPort(); 18 System.out.println("ip="+ip); 19 System.out.println("data="+data); 20 System.out.println("port="+port); 21 } 22 23 }
2.发送端
1 import java.net.DatagramPacket; 2 import java.net.DatagramSocket; 3 import java.net.InetAddress; 4 5 /** 6 * 发送端 7 * @author Administrator 8 * 9 */ 10 public class Test129 { 11 public static void main(String[] args) throws Exception { 12 sendShow(); 13 } 14 public static void sendShow()throws Exception{ 15 DatagramSocket ds=new DatagramSocket(); 16 byte[] buf="info send".getBytes(); 17 DatagramPacket dp=new DatagramPacket(buf,buf.length, 18 InetAddress.getByName("10.66.130.139"),10004); 19 ds.send(dp); 20 ds.close(); 21 } 22 }
三:TCP
1.通信,交换图片(但是一次)
先是服务端:
1 import java.io.FileOutputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.OutputStream; 5 import java.net.ServerSocket; 6 import java.net.Socket; 7 8 /** 9 * 服务端 10 * @author Administrator 11 * 12 */ 13 public class Test123 { 14 15 public static void main(String[] args) throws Exception { 16 ServerSocket ss=new ServerSocket(10009); 17 Socket s=ss.accept(); 18 InputStream in=s.getInputStream(); 19 FileOutputStream fos=new FileOutputStream("shsj.jpg"); 20 byte[] buf=new byte[1024]; 21 int len=0; 22 while((len=in.read(buf))!=-1){ 23 fos.write(buf, 0, len); 24 } 25 OutputStream out=s.getOutputStream(); 26 out.write("成功".getBytes()); 27 fos.close(); 28 s.close(); 29 ss.close(); 30 } 31 32 }
再是客户端:
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.OutputStream; 5 import java.net.Socket; 6 import java.net.UnknownHostException; 7 8 /** 9 * 发送端 10 * @author Administrator 11 * 12 */ 13 public class Test122 { 14 public static void main(String[] args) throws Exception { 15 Socket s=new Socket("10.66.113.25",10009); 16 FileInputStream fis=new FileInputStream("u.jpg"); 17 OutputStream out=s.getOutputStream(); 18 byte[] buf=new byte[1024]; 19 int len=0; 20 while((len=fis.read(buf))!=-1){ 21 out.write(buf, 0, len); 22 } 23 //important 24 s.shutdownOutput(); 25 InputStream ins=s.getInputStream(); 26 byte[] bufin=new byte[1024]; 27 int num=ins.read(bufin); 28 System.out.println(new String(bufin,0,num)); 29 fis.close(); 30 ins.close(); 31 s.close(); 32 } 33 34 }
2.多线程服务端
先写客户端
注意添加一些限制条件,同时,这里是添加的参数是文件路径
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.Socket; 7 import java.net.UnknownHostException; 8 9 public class Test124 { 10 11 public static void main(String[] args) throws Exception { 12 if(args.length!=1){ 13 System.out.println("choose a pic"); 14 return; 15 } 16 File file=new File(args[0]); 17 if(!(file.exists()&&file.isFile())){ 18 System.out.println("file have problem"); 19 return; 20 } 21 if(!file.getName().endsWith(".jpg")){ 22 System.out.println("file type is error"); 23 return; 24 } 25 if(file.length()>1024*1024*5){ 26 System.out.println("file > 5M"); 27 return; 28 } 29 Socket s=new Socket("10.66.113.25",10019); 30 FileInputStream fis=new FileInputStream(file); 31 OutputStream out=s.getOutputStream(); 32 byte[] buf=new byte[1024]; 33 int len=0; 34 while((len=fis.read(buf))!=-1){ 35 out.write(buf, 0, len); 36 } 37 s.shutdownOutput(); 38 InputStream in=s.getInputStream(); 39 byte[] bufin=new byte[1024]; 40 int num=in.read(bufin); 41 String str=new String(bufin,0,num); 42 fis.close(); 43 s.close(); 44 } 45 }
再写服务端
1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 /** 10 * 服务端 11 * @author Administrator 12 * 13 */ 14 public class Test125 { 15 16 public static void main(String[] args) throws Exception { 17 ServerSocket ss=new ServerSocket(10019); 18 while(true){ 19 Socket s=ss.accept(); 20 new Thread(new ServerThread(s)).start(); 21 } 22 } 23 } 24 /** 25 * 需要一个多线程的类 26 * 下面类是用在服务端的类 27 */ 28 class ServerThread implements Runnable{ 29 private Socket s; 30 public ServerThread(Socket s){ 31 this.s=s; 32 } 33 public void run(){ 34 int count=1; 35 String ip=s.getInetAddress().getHostAddress(); 36 try{ 37 System.out.println(ip+"connect"); 38 InputStream ins=s.getInputStream(); 39 File dir=new File("d:\pic"); 40 File file=new File(dir,ip+"("+count+").jpg"); 41 while(file.exists()){ 42 file=new File(dir,ip+"("+(count++)+").jpg"); 43 } 44 FileOutputStream fos=new FileOutputStream(file); 45 byte[] buf=new byte[1024]; 46 int len=0; 47 while((len=ins.read(buf))!=-1){ 48 fos.write(buf, 0, len); 49 } 50 OutputStream out=s.getOutputStream(); 51 out.write("success".getBytes()); 52 fos.close(); 53 s.close(); 54 55 }catch(Exception e){ 56 new RuntimeException(); 57 } 58 } 59 }
3.客户端并发登录
服务端
1 import java.io.BufferedReader; 2 import java.io.FileReader; 3 import java.io.InputStreamReader; 4 import java.io.PrintWriter; 5 import java.net.ServerSocket; 6 import java.net.Socket; 7 8 public class Test127 { 9 10 public static void main(String[] args) throws Exception{ 11 ServerSocket ss=new ServerSocket(10089); 12 while(true){ 13 Socket s=ss.accept(); 14 new Thread(new LoginThread(s)).start(); 15 } 16 } 17 } 18 class LoginThread implements Runnable{ 19 private Socket s; 20 LoginThread(Socket s){ 21 this.s=s; 22 } 23 public void run(){ 24 String ip=s.getInetAddress().getHostAddress(); 25 System.out.println("ip="+ip+" connect"); 26 try{ 27 for(int i=0;i<3;i++){ 28 BufferedReader bufin=new BufferedReader(new InputStreamReader( 29 s.getInputStream())); 30 String str=bufin.readLine(); 31 if(str==null) 32 break; 33 BufferedReader buf=new BufferedReader(new FileReader("user.txt")); 34 PrintWriter out=new PrintWriter(s.getOutputStream(),true); 35 String line=null; 36 boolean flag=false; 37 while((line=buf.readLine())!=null){ 38 if(line.equals(str)){ 39 flag=true; 40 break; 41 } 42 } 43 if(flag){ 44 System.out.println(str+"已登录"); 45 out.println(str+"欢迎登录"); 46 break; 47 }else{ 48 System.out.println(str+"尝试登录"); 49 out.println(str+"用户不存在"); 50 } 51 } 52 }catch(Exception e){ 53 throw new RuntimeException(ip+"failed"); 54 } 55 } 56 }
客户端
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.io.PrintWriter; 5 import java.net.Socket; 6 import java.net.UnknownHostException; 7 8 /** 9 * client 10 * @author Administrator 11 * 12 */ 13 public class Test126 { 14 15 public static void main(String[] args) throws Exception { 16 Socket s=new Socket("10.66.113.25",10089); 17 //键盘录入 18 BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in)); 19 //输出到服务端 20 PrintWriter out=new PrintWriter(s.getOutputStream(),true); 21 //读取返回内容 22 BufferedReader bufin=new BufferedReader( 23 new InputStreamReader(s.getInputStream())); 24 //次数 25 for(int x=0;x<3;x++){ 26 String line=bufr.readLine(); 27 if(line==null) 28 break; 29 out.println(line); 30 String info=bufin.readLine(); 31 System.out.println("info="+info); 32 if("info".contains("欢迎")) 33 break; 34 } 35 bufr.close(); 36 s.close(); 37 38 } 39 40 41 }