简体   繁体   English

使用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

A Client socket Program (in windows VM) generates integer from 1 to 10 as per below code 客户端套接字程序(在Windows VM中)根据以下代码生成1到10的整数

public class ClientSocket {

    public static void main(String[] args)

    {

try{
    InetAddress inetAddress = InetAddress.getLocalHost();
    String clientIP = inetAddress.getHostAddress();
    System.out.println("Client IP address " + clientIP);

   Integer dataSendingPort ;
   dataSendingPort = 6999 ;

    Socket socket = new Socket("192.168.0.32",dataSendingPort);
    String WelcomeMessage = " hello server from " + clientIP ;


 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

 if(socket.isConnected()){
     System.out.println("connection was successful");
 }
 else{
     System.out.println("Error- connection was not successful");
 }


 for (int x= 0 ; x< 10 ; x++){
     bufferedWriter.write(x);
     bufferedWriter.flush();
 }

    bufferedWriter.close();
}
catch (IOException e){
    System.out.println(e);
}// catch
        finally{

    System.out.println("closing connection");

}

    } // main

} // class

My server socket program is running on Mac OS as Host Machine, whose code is shown below 我的服务器套接字程序在Mac OS上作为主机运行,其代码如下所示

 public class MyServer {

        public static void main(String[] args) throws Exception {


           try {
// get input data by connecting to the socket


                InetAddress inetAddress = InetAddress.getLocalHost();
                String ServerIP = inetAddress.getHostAddress();

               System.out.println("\n server IP address = " + ServerIP);

               Integer ListeningPort ;
               ListeningPort = 6999 ;

               ServerSocket serverSocket = new ServerSocket(ListeningPort);

               System.out.println("server is receiving data on port # "+ ListeningPort +"\n");

               // waiting for connection form client

               Socket socket = serverSocket.accept();


               if(socket.isConnected()){

                   System.out.println("Connection was successful");
               }
               else {
                   System.out.println("connection was not successful");
               }



              BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    Integer s = 0 ;

       while (( s = input.read()) >= 0){

           System.out.println(input.read());
       }
           } //try

            catch (IOException e)
            {
                System.out.println(e);

            } // catch


        } //main
    } //socket class

The issue is that output I am receiving is -1 when I use while loop and receive first value ie 0 without using a loop. 问题是,当我使用while循环并不使用循环而接收第一个值(即0)时,我正在接收的输出为-1。

However, I was able to send a single value from client to server, but how can I send Stream of Values from the client and print it on Server Side. 但是,我能够从客户端向服务器发送单个值,但是如何从客户端发送值流并将其打印在服务器端。

Suggestions are most welcome 欢迎建议

  • -1 means end of stream. -1表示流结束。
  • Closing the input or output stream of a stock closes the socket. 关闭股票的输入或输出流会关闭套接字。
  • socket.isConnected() cannot possibly be false at the point you are testing it. 在测试时, socket.isConnected()不能为假。
  • input.ready() isn't a test for end of stream, or end of message, or end of transmission, or anything useful at all really. input.ready()并不是测试流的末尾,消息的末尾,传输的末尾或任何真正有用的测试。
  • Don't flush inside loops. 不要冲洗内部循环。

暂无
暂无

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

相关问题 套接字编程中“ helloworld”服务器和客户端程序中的异常 - Exception in “helloworld” server and client program in socket programming 在java socket编程中,是否有必要在客户端程序之前先运行服务器程序? - In java socket programming, is it necessary to run server program first before client program? Java套接字将数据从客户端发送到服务器 - Java socket send data from client to server Android Java客户端程序将单独的线程用于套接字IO,找不到使用按钮发送数据的方法 - Android java client program using separate thread for socket IO, Can't find a way to send data with a button 使用 TCP 连接(套接字编程)在 Java 中将 2D 矩阵从客户端发送到服务器 - Send a 2D Matrix from Client to Server in Java using TCP connection (Socket Programming) 为什么不能使用套接字发送正确的数据? JAVA(在本地运行的客户端-服务器程序) - Why can't send the correct data with sockets? JAVA (Client-Server Program running on local) 使用Java中的套接字编程对程序进行排序 - Sorting program using socket programming in java 使用套接字编程的Java中的聊天程序 - chat program in java using socket programming 为什么客户端程序通过Java TCP套接字从服务器获取不正确的数据? - Why client program gets incorrect data from server through Java TCP-socket? 在 Java 中的套接字编程中从服务器向所有客户端发送数据 - Send data from server to all clients in socket programming in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM