服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|

服务器之家 - 编程语言 - JAVA教程 - 浅谈java的TCP和UDP编程(附实例讲解)

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

2020-11-02 17:34脚本之家 JAVA教程

下面小编就为大家带来一篇浅谈java的TCP和UDP编程(附实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

TCP

客户端:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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();
  }
 
}

 服务端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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

客户端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
  }
 
}

服务端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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编程(附实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐