黑马程序员_网络编程之TCP和UDP

本文提供了一个简单的TCP和UDP网络编程实例,包括服务端和客户端的代码实现。通过这些示例,读者可以了解如何使用Java进行基本的网络通信编程。

------- android培训java培训、期待与您交流! ----------

TCP服务端:

import java.io.DataInputStream;   
import java.io.IOException;   
import java.net.ServerSocket;   
import java.net.Socket;   
/**  
 * 网络编程TCP Server  
 * @author www.lookhan.com  
 *  
 */  
public class ServerTcp {   
  
    public static void main(String[] args) {   
  
        ServerSocket ss = null;   
        Socket s = null;   
        try {   
            ss = new ServerSocket(6666);   
            while(true){   
                //accept()方法是阻塞式的,即如果没有接受到东西,就一直阻塞在这里。    
                s = ss.accept();   
                System.out.println("a client connect!");   
                DataInputStream dis = new DataInputStream(s.getInputStream());   
                System.out.println(dis.readUTF());   
            }   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                s.close();   
                ss.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
  
    }   
  
}  

也有s.getOutputStream()表示向客户端写东西。
客户端 :

import java.io.DataOutputStream;   
import java.io.IOException;   
import java.io.OutputStream;   
import java.net.Socket;   
import java.net.UnknownHostException;   
/**  
 * 网络编程TCP Client  
 * @author www.lookhan.com  
 *  
 */  
public class ClientTcp {   
  
    public static void main(String[] args) {   
  
           
        Socket s = null;   
        OutputStream os = null;   
        DataOutputStream dos = null;   
        try {   
            s = new Socket("127.0.0.1", 6666);   
            os = s.getOutputStream();   
            dos = new DataOutputStream(os);   
            dos.writeUTF("hello server!");   
        } catch (UnknownHostException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally{   
            try {   
                dos.close();   
                os.close();   
                s.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
  
    }   
  
}  

UDP:
   服务端 :

  
import java.io.ByteArrayInputStream;   
import java.io.DataInputStream;   
import java.io.IOException;   
import java.net.DatagramPacket;   
import java.net.DatagramSocket;   
import java.net.SocketException;   
/**  
 * 网络编程UDP Server  
 * @author www.lookhan.com  
 *  
 */  
public class ServerUdp {   
  
    public static void main(String[] args) {   
  
        DatagramPacket dp = null;   
        DatagramSocket ds = null;   
        ByteArrayInputStream bais = null;   
        DataInputStream dis = null;   
        try {   
            byte buf[] = new byte[1024];   
            //DatagramPacket是一个网络上的包,在客户端是要加地址的。    
            dp = new DatagramPacket(buf, buf.length);   
            //服务器端监听着5678端口。    
            ds = new DatagramSocket(5678);   
            while(true){   
                //如果接受到东西,就将其转给dp(即上面的包),随之传给包里面的字节数组空间。    
                ds.receive(dp);   
                bais = new ByteArrayInputStream(buf);   
                dis = new DataInputStream(bais);   
                System.out.println(dis.readLong());   
            }   
        } catch (SocketException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                dis.close();   
                bais.close();   
                ds.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
  
    }   
  
}  

客户端 :

import java.io.ByteArrayOutputStream;   
import java.io.DataOutputStream;   
import java.io.IOException;   
import java.net.DatagramPacket;   
import java.net.DatagramSocket;   
import java.net.InetSocketAddress;   
import java.net.SocketException;   
/**  
 * 网络编程UDP Client  
 * @author www.lookhan.com  
 *  
 */  
public class ClientUdp {   
  
    public static void main(String[] args) {   
  
        ByteArrayOutputStream baos = null;   
        DataOutputStream dos = null;   
        DatagramSocket ds = null;   
        try {   
            long n = 10000L;   
            baos = new ByteArrayOutputStream();   
            dos = new DataOutputStream(baos);   
            dos.writeLong(n);   
            byte[] buf = baos.toByteArray();   
            //定义一个包,参数有三个,第一个是字节数据,第二个是数据的长度,    
            //第三个是SocketAddress(下面是其子类),里面有包走向的地址和端口    
            DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678));   
            //定义本地Socket,并指定接受包的端口(注意下面的是Socket,和上面的不同)   
            ds = new DatagramSocket(9999);   
            //从本地的Socket(Socket)发送包(Packet)。    
            ds.send(dp);   
            ds.close();   
        } catch (SocketException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                ds.close();   
                dos.close();   
                baos.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
  
    }   
  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值