亲宝软件园·资讯

展开

java的TCP和UDP编程 浅谈java的TCP和UDP编程(附实例讲解)

人气:0
想了解浅谈java的TCP和UDP编程(附实例讲解)的相关内容吗,在本文为您仔细讲解java的TCP和UDP编程的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,TCP和UDP编程,下面大家一起来学习吧。

TCP

客户端:

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MyClient {

  public static void main(String[] args) throws Exception{
    
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    
    BufferedReader input = null;
    // 请求指定ip和端口号的服务器
    socket = new Socket("127.0.0.1",3333);
    
    while(true){

      in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      
      out = new PrintWriter(socket.getOutputStream(), true);
      // 接收控制台的输入
      input = new BufferedReader(new InputStreamReader(System.in));
      // out.println("this is client info!");
      String info = input.readLine();
      
      out.println(info);
      
      String str = in.readLine();
      
      System.out.println("客户端显示--》服务器的信息:" + str);
    }
    //in.close();
    //out.close();
  }

}

 服务端:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServices {

  public static void main(String[] args) throws Exception{
    ServerSocket ss = null;
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    BufferedReader input = null;
    
    // 监听3333端口
    ss = new ServerSocket(3333);
    // 等待接收客户端的请求
    socket = ss.accept();
    while(true){
      // 获取连接对象的输入流
      in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      // 获取客户端的输入信息
      String str = in.readLine();
      
      input = new BufferedReader(new InputStreamReader(System.in));
      
      System.out.println("服务器显示-->客户端输入数据:" + str);
      
      out = new PrintWriter(socket.getOutputStream(),true);
      // 将数据输出到客户端
      // out.println("hehe");
      String info = input.readLine();
      
      out.println(info);
    }
//    in.close();
//    out.flush();
//    out.close();
  }

}

UDP

客户端:

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPClient {

  public static void main(String[] args) throws Exception {
    
    DatagramSocket ds = null;
    DatagramPacket dp = null;
    byte[] buf = new byte[1024];
    
    ds = new DatagramSocket(3333);
    dp = new DatagramPacket(buf, 1024);
    
    // 接收数据,放入数据报
    ds.receive(dp);
    // 从数据报中取出数据
    String info = new String(dp.getData(),0, dp.getLength());
    System.out.println("接收到的信息是:" + info);
  }

}

服务端:

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

public class UDPServer {

  public static void main(String[] args) throws Exception {
    DatagramSocket ds = null;
    DatagramPacket dp = null;
    
    // 构建发送对象
    ds = new DatagramSocket();
    // 将数据打包-->打成数据报
    String info = "hello world!";
    
    dp = new DatagramPacket(info.getBytes(), info.length(), InetAddress.getByName("localhost"),3333);
    
    // 发出数据报
    ds.send(dp);
    
  }

}

以上这篇浅谈java的TCP和UDP编程(附实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论