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

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java开发实现的Socket双向通信功能示例

Java开发实现的Socket双向通信功能示例

2021-03-17 14:15看看白云 Java教程

这篇文章主要介绍了Java开发实现的Socket双向通信功能,结合实例形式分析了java基于socket实现的服务器端与客户端双向通信相关操作技巧,需要的朋友可以参考下

本文实例讲述了Java开发实现的Socket双向通信功能。分享给大家供大家参考,具体如下:

服务端

?
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.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server_1 {
  public static void main(String[] args) {
    InputStreamReader isr;
    BufferedReader br;
    OutputStreamWriter osw;
    BufferedWriter bw;
    String str;
    Scanner in = new Scanner(System.in);
    try {
      ServerSocket server = new ServerSocket(4444);// 在本机的4444端口开放Server
      Socket socket = server.accept();// 只要产生连接,socket便可以代表所连接的那个物体,同时这个server.accept()只有产生了连接才会进行下一步操作。
      System.out.println(socket.getInetAddress());// 输出连接者的IP。
      System.out.println("建立了一个连接!");
      while (true) {
        isr = new InputStreamReader(socket.getInputStream());
        br = new BufferedReader(isr);
        System.out.println(socket.getInetAddress() + ":" + br.readLine());
        osw = new OutputStreamWriter(socket.getOutputStream());
        bw = new BufferedWriter(osw);
        System.out.print("回复:");
        str = in.nextLine();
        bw.write(str + "\n");
        bw.flush();
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

客服端

?
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Client_1 {
  public static void main(String[] args) {
    InputStreamReader isr;
    BufferedReader br;
    OutputStreamWriter osw;
    BufferedWriter bw;
    String str;
    Scanner in = new Scanner(System.in);
    try {
      Socket socket = new Socket("localhost", 4444);
//     System.out.println(socket.getInetAddress());// 输出连接者的IP。
      System.out.println("成功连接服务器");
      while (true) {
        osw = new OutputStreamWriter(socket.getOutputStream());
        bw = new BufferedWriter(osw);
        System.out.print("回复:");
        str = in.nextLine();
        bw.write(str + "\n");
        bw.flush();
        isr = new InputStreamReader(socket.getInputStream());
        br = new BufferedReader(isr);
        System.out.println(socket.getInetAddress() + ":" + br.readLine());
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/qq_14949289/article/details/49272171

延伸 · 阅读

精彩推荐