简体   繁体   English

使用 TCP 连接(套接字编程)在 Java 中将 2D 矩阵从客户端发送到服务器

[英]Send a 2D Matrix from Client to Server in Java using TCP connection (Socket Programming)

I need to send a 2D matrix from the client to the server-side using the following packages:我需要使用以下包将 2D 矩阵从客户端发送到服务器端:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket; 

I have read a matrix from the user and I need to send to the server to perform certain operations on it.我已经从用户那里读取了一个矩阵,我需要发送到服务器以对其执行某些操作。 How do I send the complete matrix across?如何发送完整的矩阵? I am sending multiple variables and not just a matrix.我正在发送多个变量而不仅仅是一个矩阵。 I am sending integers and a matrix.我正在发送整数和矩阵。

So after trying out something I thought could work, I found a simple solution to this problem.因此,在尝试了一些我认为可行的方法后,我找到了解决此问题的简单方法。

Client-Side code客户端代码

            // Make a new client side connection
            Socket clientSocket = new Socket("localhost", 9000);


            // Create an output stream
            DataOutputStream dataOutput = new DataOutputStream(clientSocket.getOutputStream());

            // Send data to the server

            // Send the number of nodes and the matrix
            dataOutput.writeInt(nodes);
            dataOutput.flush();
            for (int i = 0; i < nodes; i++)
                for (int j = 0; j < nodes; j++)
                    dataOutput.writeInt(adjMatrix[i][j]);
                    dataOutput.flush();

and the code on the server-side which would receive the matrix is as follows.接收矩阵的服务器端代码如下。

    // create a server socket and bind it to the port number 
    ServerSocket serverSocket = new ServerSocket(9000); 
    System.out.println("Server has been started");
    
    while(true){
        
        // Create a new socket to establish a virtual pipe 
        // with the client side (LISTEN)
        Socket socket = serverSocket.accept(); 
        
        // Create a datainput stream object to communicate with the client (Connect)
        DataInputStream input = new DataInputStream(socket.getInputStream()); 

     
        // Collect the nodes and the matrix through the data
        int nodes = input.readInt();
        int adjMatrix[][] = new int[nodes][nodes]; // Create the matrix
        for (int i = 0; i < nodes; i++)
            for (int j = 0; j < nodes; j++)
                adjMatrix[i][j] = input.readInt();
    }

This solution worked for me can be used to parse any type of data stream.这个对我有用的解决方案可用于解析任何类型的数据流。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java中的套接字编程将数据流从客户端程序(在VM中运行)发送到服务器程序(在Host OS上) - Send stream of Data from client program (running in VM) to server program(on Host OS) using socket programming in Java Android客户端/ Java服务器和Mysql数据库Tcp套接字编程 - Android Client /Java Server and Mysql DataBase Tcp Socket Programming 服务器如何知道客户端的端口号,它将使用它使用 Java 编程套接字将响应发送到客户端? - How the server knows the port number of the client in which it will use it to send the responses to the client using java programming socket? Java TCP Socket编程:客户端和服务器在同一台计算机上通信良好,但无法通过局域网相互发送数据 - Java TCP Socket Programming: Client and Server communicate well on the same computer, but fail to send data to each other over LAN 从服务器到客户端的Java TCP连接 - Java TCP Connection From Server through Client TCP套接字连接Java Client &lt;-&gt; C ++服务器中的意外行为 - Unexpected behaviour in TCP socket connection Java Client<-> c++ server Java客户端和C ++服务器通过TCP套接字发送和接收 - Java client and a C++ server send and receive via TCP Socket 服务器无法将消息发送到客户端(Java线程套接字编程) - Server unable to send messages to client (java thread socket programming) 使用套接字将服务器JAVA的图像发送到android客户端 - send image from server JAVA to android client using socket 如何在不使用常规客户端-服务器套接字编程的情况下用Java发送字节? - How to send a byte in java without using the regular client - server socket programming?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM