网络编程

C/S的编程工具:

    1:基于TCP协议;socket协议,可靠的服务

    2:基于UDP协议:不可靠的,短信功能

如果程序要使TCP协议,使用java的的两个包支持:

    -java.net.*:主要提供网络支持

      --ServerSocket类:服务器端程序

      --Socket类:客户端程序

    -java.io.*传递信息流

Code
public class TCPServer
{
    
public static void main(String args[]) throws Exception
    {
        
// 使用ServerSocket
        ServerSocket server = null ;
        
// 每一个用户在程序中就是一个Socket
        Socket client = null ;
        server 
= new ServerSocket(8888) ;
        
// 等待客户端连接
        client = server.accept() ;
        
// 向客户端打印信息:HELLO MLDN
        PrintWriter out = null ;
        
// 准备向客户端打印信息
        out = new PrintWriter(client.getOutputStream()) ;
        out.println(
"HELLO WORLD") ;
        out.close() ;
        client.close() ;
        server.close() ;
    }
};
我们可以用telnet命令行来调用windows的链接,用o localhost 8888来执行

客户端 测试:客户端就两个功能,建立socket,接受输入的命令,输入流--网络上传输程序用的是字节流,我们可以用BufferedReader来操作

Code
public class TCPClient
{
    
public static void main(String args[]) throws Exception
    {
        
// 表示一个客户端的Socket
        Socket client = null ;
        
// 表示一个客户端的输入信息
        BufferedReader buf = null ;
        client 
= new Socket("localhost",8888) ;
        buf 
= new BufferedReader(new InputStreamReader(client.getInputStream())) ;
        System.out.println(buf.readLine()) ;
        buf.close() ;
        client.close() ;
    }
};
JDK中也有专门来实现udp的类

  -DatagramSocket

  -DatagramPacket

Code
public class UDPServer
{
    
public static void main(String args[]) throws Exception
    {
        DatagramSocket ds 
= null ;
        DatagramPacket dp 
= null ;
        
// 要保证UDP有一个运行的端口
        ds = new DatagramSocket(5000) ;
        String s 
= "HELLO WORLD" ;
        dp 
= new DatagramPacket(s.getBytes(),0,s.length(),InetAddress.getByName("localhost"),8888) ;
        ds.send(dp) ;
        ds.close() ;
    }
};
Code
public class UDPClient
{
    
public static void main(String args[]) throws Exception
    {
        DatagramSocket ds 
= null ;
        DatagramPacket dp 
= null ;
        
// 手机发信息和接收信息是有大小限制的
        byte b[] = new byte[1024] ;
        
// 在8888端口上一直等待信息到来
        ds = new DatagramSocket(8888) ;
        dp 
= new DatagramPacket(b,b.length) ;
        ds.receive(dp) ;
        
// 从数据包中取出数据
        
// System.out.println(dp.getLength()) ;
        String str = new String(dp.getData(),0,dp.getLength()) ;
        System.out.println(
"接收到的数据为:"+str) ;
        ds.close() ;
    }
};
Code
public class UDPServerInput
{
    
public static void main(String args[]) throws Exception
    {
        DatagramSocket ds 
= null ;
        DatagramPacket dp 
= null ;
        
// 要保证UDP有一个运行的端口
        ds = new DatagramSocket(5000) ;
        
// String s = "HELLO MLDN" ;
        
// BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)) ;
        System.out.print("请输入要发送的信息:") ;
        
// s = buf.readLine() ;
        byte b[] = new byte[1024] ;
        
int len = System.in.read(b) ;

        System.out.println(
new String(b,0,len)) ;

        dp 
= new DatagramPacket(b,len,InetAddress.getByName("localhost"),8888) ;
        
        ds.send(dp) ;
        ds.close() ;
    }
};

原文地址:https://www.cnblogs.com/zhxiaomiao/p/1413910.html