标签:
InetAddress,UDP, TCP
package hanshi.net; import java.net.*; public class count { public static void main(String[] args)throws Exception { InetAddress ia = InetAddress.getLocalHost(); //获取本地的 print(ia.getHostAddress()); print(ia.getHostName()); InetAddress iad = InetAddress.getByName("www.baidu.com"); //通过名字获取信息,可以是IP名字,也可以是主机名 print(iad.getHostAddress()); print(iad.getHostName()); } static public void print(Object obj){ System.out.println(obj); } } //UDP发送端 package hanshi.net; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.*; public class count { public static void main(String[] args)throws Exception { DatagramSocket ds = new DatagramSocket(110); BufferedReader bufd = new BufferedReader(new InputStreamReader(System.in)); String str = null; while((str = bufd.readLine())!=null){ if("over".equals(str)){ break; } byte[] bt = str.getBytes(); DatagramPacket dp = new DatagramPacket(bt,bt.length,InetAddress.getByName("192.168.109.119"),1000); ds.send(dp); } ds.close(); } static public void print(Object obj){ System.out.println(obj); } } //UDP接收端 package hanshi.net; import java.net.DatagramPacket; import java.net.DatagramSocket; public class count2 { public static void main(String[] args)throws Exception { DatagramSocket ds = new DatagramSocket(1000); //接受端一定要定义一个端口 while(true){ byte[] buf = new byte[1024]; DatagramPacket dg = new DatagramPacket(buf, buf.length); ds.receive(dg); //这里是阻塞方法,没接受到数据就会一直处于等待状态 print(dg.getAddress().getHostAddress()+"***"+dg.getAddress().getHostName()); //通过数据包获取Address来进行操作 print(new String(dg.getData(),0,dg.getLength())); //获取数据流中的数据,要给定数据的长度 print(dg.getPort()); //获取发送端的端口,如果发送端没有指定一个发送端口,这里就会获取到一个系统随机分配的端口 } //接受端口可以是没有关闭的,就想服务器一样 } static public void print(Object obj){ System.out.println(obj); } } //TCP package hanshi.net; import java.io.OutputStream; import java.net.*; //如果Scoke的建立是没有传入参数的,那么建立后是没有连接的,但是可以通过connect来连接。。。 //这个一建立就有一个Scoket流,也就是网络流。。。 //一旦流建立成功就可以拿到输入流和输出流。。。 //服务端没有流对象,服务端是通过accept来获取客户端的流对象来使用的。。。 //服务端不可以关闭自己,但是可以关闭从客户端获取过来的流对象。。。 /*客户端结束后服务端也结束的原因是因为,在s.close()之后的意义就是在末尾加上-1来做结束标志,所以在服务端读取到了就是-1,以为结束了就返回null,所以就跳出循环,结束客户端。。。 简化代码:printWriter既可以接受字符流,也可以接受字节流。。。 */ //理论上在Scokert就要抛出异常,如果连接不上就直接处理了啊。。。 public class count { public static void main(String[] args)throws Exception { InetAddress ia = InetAddress.getLocalHost(); Socket sk = new Socket(ia.getHostAddress(),1000); OutputStream os = sk.getOutputStream(); os.write("chenruibing".getBytes()); //os.close(); 这一个可以不关闭,当关闭了socket后,依附在这个上面的流就会自动关闭 sk.close(); } static public void print(Object obj){ System.out.println(obj); } } package hanshi.net; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class count2 { public static void main(String[] args)throws Exception { ServerSocket ss = new ServerSocket(1000); //都要监听端口 Socket s = ss.accept(); //这个也是阻塞式方法 print(s.getInetAddress().getHostAddress()+"denglu"); InputStream is = s.getInputStream(); byte[] bt = new byte[1024]; int len = is.read(bt); print(new String(bt,0,len)); s.close(); ss.close(); } static public void print(Object obj){ System.out.println(obj); } }
TCP
套接字是两台机器间通信的端点。。。
class anli { public static void main (String[] args)throws Exception { while(true) { Socket s = new Socket("192.168.109.119",12345); OutputStream os = s.getOutputStream(); //os.write("ni hao ".getBytes()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); os.write(line.getBytes()); InputStream in = s.getInputStream(); byte[] bt = new byte[1024]; int len = in.read(bt); System.out.println(new String(bt,0,len)); //br.close(); } //s.close(); } } class rec { public static void main (String[] args)throws Exception { ServerSocket ss = new ServerSocket(12345); while(true) { Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); //System.out.println(ip+".......connect"); InputStream in = s.getInputStream(); byte[] bt = new byte[1024]; int len = in.read(bt); System.out.println(new String(bt,0,len)); OutputStream os = s.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); os.write(line.getBytes()); s.close(); //br.close(); } } }tcp about two
/* 简化代码:printWriter既可以接受字符流,也可以接受字节流。。。 */ class anli { public static void main (String[] args)throws Exception { while(true) { Socket s = new Socket ("192.168.109.119",10086); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedReader bfr = new BufferedReader(new InputStreamReader(s.getInputStream())); //BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream(),true); //使用printWriter的好处 String line = null; while ((line = br.readLine())!=null) { if ("over".equals(line)) { break; } //bfw.write(line); //bfw.newLine(); //bfw.flush(); pw.println(line); //一个有了前面多个代码的功能 String str = bfr.readLine(); System.out.println("Server:::"+str); } br.close();//输入流要关闭。。。 这两个是不能关掉的,要不然就一直报错 s.close(); } } } class rec { public static void main (String[] args)throws Exception { ServerSocket ss = new ServerSocket(10086); while(true) { Socket s = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream(),true); //一定要加上true,要不然的结果就是数据传递不出去。。。 String line = null; while((line = br.readLine())!=null) { pw.println(line.toUpperCase()); //bw.write(line.toUpperCase()); //bw.newLine(); //bw.flush(); } s.close(); ss.close(); } } }
标签:
原文地址:http://my.oschina.net/u/2356176/blog/466638