网络通信

来源:互联网 发布:社交软件的弊端 编辑:程序博客网 时间:2024/04/27 23:36
<span style="font-size:14px;">import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Arrays;public class InetAddressDemo {public static void main(String[] args) throws UnknownHostException {// TODO Auto-generated method stubInetAddress ad=InetAddress.getLocalHost();System.out.println(ad);System.out.println("本机计算机名:"+ad.getHostName());System.out.println("本机IP地址"+ad.getHostAddress());//获得字节数组形式的IP地址byte[] bytes=ad.getAddress();System.out.println("本机的字节数组形式IP地址"+Arrays.toString(bytes));//根据机器名可以获得InteAddrass实例InetAddress ad1=InetAddress.getByName("XZ-201601130953");System.out.println("ad1:"+ad1);//根据IP地址也可以获得InteAddrass实例InetAddress ad2=InetAddress.getByName("183.26.234.128");System.out.println("ad2:"+ad2);byte[] b={(byte) 183,26,(byte) 234,(byte) 128};InetAddress ad3=InetAddress.getByAddress(b);System.out.println("ad3:"+ad3);//百度的IP地址byte[] bb={14,(byte) 215,(byte) 177,37};InetAddress ad4=InetAddress.getByAddress(bb);System.out.println("百度ad4:"+ad4);System.out.println("百度计算机名:"+ad4.getHostName());}}</span>

          


<span style="font-size:14px;">import java.net.MalformedURLException;import java.net.URL;public class UrlDemo {public static void main(String[] args) throws MalformedURLException {// TODO Auto-generated method stub//创建一个URL实例URL baidu=new URL("http://www.baudu.com");//?后面表示参数也就是字符串,#后面表示锚点URL url=new URL(baidu,"?index.html/tn=monline_3_dg#test");System.out.println("协议:"+url.getProtocol());System.out.println("主机:"+url.getHost());System.out.println("端口:"+url.getPort());System.out.println("文件路径:"+url.getPath());System.out.println("文件名:"+url.getFile());System.out.println("相对路径:"+url.getRef());System.out.println("查询字符串:"+url.getQuery());}}</span>


服务端

<span style="font-size:14px;">import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class ServerSocketDemo {public static void main(String[] args) {// TODO Auto-generated method stubtry {ServerSocket serversocket=new ServerSocket(8888);//调用accept()方法开始监听,等待客户端的链接System.out.println("******等待客服端的链接");Socket socket=serversocket.accept();//获取输入流并读取客服端信息InputStream is=socket.getInputStream();//把字节输入流转换成字符输入流InputStreamReader isr=new InputStreamReader(is);//添加缓冲BufferedReader br=new BufferedReader(isr);String info=null;while((info=br.readLine())!=null){System.out.println("我是服务器,客服端对我说"+info);}socket.close();br.close();is.close();isr.close();serversocket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}</span>

客服端

<span style="font-size:14px;">import java.io.*;import java.net.Socket;import java.net.UnknownHostException;public class SocketDemo {public static void main(String[] args) {// TODO Auto-generated method stubtry {//localhost也可以是IP地址Socket socket=new Socket("localhost",8888);//获得输出流,向服务器端发送信息,字节输出流OutputStream os=socket.getOutputStream();//将输入流转为打印流PrintWriter pw=new PrintWriter(os);pw.write("用户名:admin;密码:123");socket.shutdownInput();pw.close();os.close();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}</span>








0 0