J2SE学习笔记(7)初谈通信

通讯麻烦的东西,弄些开胃的,下面的帖子还有料。

public   static   void  main(String[] args)  {
        
    InetAddress add
= InetAddress.getLocalHost();
    System.out.println(add.getHostName());
//主机名
     System.out.println(add.getHostAddress());//主机IP地址
     
//机器名、域名、ip都可以
    InetAddress add1=   InetAddress.getByName("127.0.0.1");
    System.out.println(add1.getHostName());

    
//编码
    String s=null;
    s 
= URLEncoder.encode("中国 china ","gb2312");
    System.out.println(s);
    System.out.println(URLDecoder.decode(s,
"gb2312"));
}

 
// SOCKET套接字
//   与主机地址和端口地址相关联
//   客户端和服务器通过套接字建立连接和进行通信

// SOCKET分类

// TCP套接字
//  较可靠的双向流协议
//  发送任意数量的数据
//  提供消息确认、错误检测和错误恢复等服务
// UDP 套接字
//  比较不可靠


// tcp
public   class  Server  {
    
    
public static void main(String[] args) {
        
//注册自己时表示对客户端连接感兴趣
        ServerSocket s = null;
        Socket client 
= null;
        
//创建服务端Socket   建立服务器和客户端之间的连接
        try {
             s 
= new ServerSocket(4700);
            
//监听等待连接
             System.out.println("等待连接");
             
//accept( ) 方法用于等待客户端触发通信
             client = s.accept();
             
//获取socket 输入输出
             InputStream in = client.getInputStream();
             OutputStream out 
= client.getOutputStream();
             
//向客户发送信息
             out.write(100);
             
             System.out.println(
"请输入:");
             InputStream sin 
= System.in;
             
             
//对输出进行封装
             PrintWriter pw = new PrintWriter(out);
             
//对键盘输入进行封装
             BufferedReader br = new BufferedReader(new InputStreamReader(sin));
             
             String msg 
= null;
             
while (!(msg = br.readLine()).equals("exit")){
                 
                 pw.println(msg);
                 pw.flush();
             }

             
             in.read();
             
            System.out.println(client);
            System.out.println(
"wait for connect....");
        }
 catch (IOException e) {
            System.out.println(
"绑定端口出错");
            e.printStackTrace();
        }
finally{
            
//关闭socket
            if(client!= null)
                
try {
                    client.close();
                }
 catch (IOException e) {
                    e.printStackTrace();
                }

            
if(s!=null)
                
try {
                    s.close();
                }
 catch (IOException e) {
                    e.printStackTrace();
                }

            
        }

        
    }


}

// SOCKET通信实现过程-TCP
// 服务器端socket绑定于特定端口,服务器侦听socket等待连接请求
// 客户端向服务器和特定端口提交连接请求  
// 服务器接受连接,产生一新的socket,绑定到另一端口,由此socket来处理和客户端的交互,服务器继续侦听原socket来接受其他客户端的连接请求
// 连接成功后客户端也产生一socket,并通过它来与服务器端通讯(注意:客户端socket并不与特定端口绑定)
// 服务器端和客户端就通过读取和写入各自的socket来进行通讯。 
public   class  Client  {

    
public static void main(String[] args) {
        
// TODO 自动生成方法存根
        Socket client=null;
        
try {
            client
=new Socket("127.0.0.1",1860);
            
            InputStream in
=client.getInputStream();
            OutputStream out
=client.getOutputStream();
            
            PrintWriter pw
=new PrintWriter(out);
            BufferedReader br
=new BufferedReader(new InputStreamReader(System.in));
            BufferedReader buf
=new BufferedReader(new InputStreamReader(in));
            String msg
=null;
        
while(!((msg=br.readLine()).equals("exit"))){
                pw.println(msg);
                pw.flush();
            }
            
            while((msg = br.readLine())!=null){
                
                System.out.println("server say:"+msg);
            }

            
            
if(((msg=br.readLine()).equals("A")))
            
{
                
while(!((msg=br.readLine()).equals("exit"))){
                    pw.println(msg);
                    pw.flush();
                }

            }

            
else
            
{
                
while((msg = br.readLine())!=null){
                    
                    System.out.println(
"server say:"+msg);
                }

            }

            
            
            System.out.println(client);
        }
 catch (UnknownHostException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }
finally{
            
if(client!=null)
                
try {
                    client.close();
                }
 catch (IOException e) {
                    e.printStackTrace();
                }

        }

    }


}

// SOCKET通信实现过程-UDP
public   class  Pointer1  {    
    
public static void main(String[] args) {        
        
//创建数据报socket
        try {
            DatagramSocket ds 
= new DatagramSocket(8099);
            
byte buf[] = new byte[1024];
            String s 
= "hello pointer2 ,i'm pointer1";
            buf 
= s.getBytes();
            
//构造数据报文
            DatagramPacket packet = new DatagramPacket(
                    buf,
                    buf.length,
                    InetAddress.getLocalHost(),
                    
1860
            );
            ds.send(packet);
            ds.close();
        }
 catch (SocketException e) {
            e.printStackTrace();
        }
 catch (UnknownHostException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

    }

}


public   class  Pointer2  {
public static void main(String[] args) {
        
//创建数据报socket
        try {
            DatagramSocket ds 
= new DatagramSocket(9001);
            
//创建接收报文容器
            byte buf [] = new byte[1024];
            DatagramPacket packet 
= new DatagramPacket(buf,buf.length);
            System.out.println(
"等待数据");
            ds.receive(packet);
            String s 
= new String(packet.getData());
            System.out.println(s);
            ds.close();
        }
 catch (SocketException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }
    
    }

}

你可能感兴趣的:(J2SE学习笔记(7)初谈通信)