JAVA网络编程UDP

发送端

package bank;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class send {
    public static void main (String[] args){
        DatagramSocket ds=null;
        try {
         ds = new DatagramSocket();
            byte[] b = "你好啊,我是send".getBytes();
            //创建一个数据报,每个数据不能大于64k,都记录着数据信息,发送端的ip,端口号,以及接受的up和端口号
            DatagramPacket pack = new DatagramPacket(b,0,b.length, InetAddress.getByName("127.0.0.1"),9090);
            ds.send(pack);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ds!=null){
                ds.close();
            }
        }

    }

}

接受端

package bank;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class send {
public static void main (String[] args){
    DatagramSocket ds=null;
    try {
     ds = new DatagramSocket();
        byte[] b = "你好啊,我是send".getBytes();
        //创建一个数据报,每个数据不能大于64k,都记录着数据信息,发送端的ip,端口号,以及接受的up和端口号
        DatagramPacket pack = new DatagramPacket(b,0,b.length, InetAddress.getByName("127.0.0.1"),9090);
        ds.send(pack);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ds!=null){
            ds.close();
        }
    }

}

}

客户端向服务端发送文本,服务端将文本转成大写返回给客户端

服务端

package bank;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main (String[] args){
        ServerSocket ss = null;
        Socket s = null;
    //接受来自客户端的信息
    InputStream is = null;
    //返回给客户端
    OutputStream os = null;

    try {
        ss = new ServerSocket(9090);

        s = ss.accept();
        is = s.getInputStream();
        byte[] b = new byte[10];
        int len;
        String str = new String();
        while ((len = is.read(b))!=-1){
            String str1 = new String(b,0,len);
            str+=str1;
        }
        String upperCase = str.toUpperCase();
        os = s.getOutputStream();
        os.write(upperCase.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(os != null){
            try {
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        if(is != null){
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        if(s != null){
            try {
                s.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        if(ss != null){
            try {
                ss.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

}

客户端

	package bank;
	
	
	import java.io.IOException;
	import java.io.InputStream;
	import java.io.OutputStream;
	import java.net.InetAddress;
	import java.net.Socket;
	import java.util.Scanner;
	
	public class Client {
public static void main (String[] args)  {
    Socket socket = null;
    OutputStream os = null;
    Scanner scanner = null;
    //接受来自服务端的数据
    InputStream is = null;

    try {
        //1.创建一个Socket的对象,通过构造器指明服务端的ip地址,以及接受程序的端口号
        socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
        //2.使用getOutputStream()发送数据,方法返回OutputStream的对象
        os=socket.getOutputStream();
        //向服务端发送数据
        System.out.println("请输入多个字符:");

        scanner = new Scanner(System.in);
        String str = scanner.next();
        os.write(str.getBytes());

        socket.shutdownOutput();
        is=socket.getInputStream();
        byte[] b = new byte[10];
        int len;
        while ((len = is.read(b))!=-1){
            String str1 = new String(b,0,len);
            System.out.print(str1);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
      if (is!=null){
          try {
              is.close();
          } catch (IOException e) {
              e.printStackTrace();
          }

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

            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

你可能感兴趣的:(J2SE)