簡體   English   中英

從套接字Java讀取數據

[英]Reading data from a socket java

因此,我正在嘗試從套接字讀取數據。 我已經建立了連接,並向正在偵聽此類消息的另一個套接字發送了一條消息,但似乎什么也沒收到。 客戶代碼:

import java.net.*;
import java.io.*;
import java.util.*;
public class TCPClient {
 /*Conecting to server.
 */
 public static void Client(String[] args){
  int port = 47361;
  Scanner in = new Scanner(System.in);
  System.err.print("Starting up...");
  Socket CS1 = null; //Declaring CS1 as a socket
  DataOutputStream DOS1 = null; //Declaring an outputput data stream "DOS1"
  DataInputStream DIS1 = null; //Setting the values to null
  System.err.println("All created.");
  System.out.println("---------");
  System.out.println(" OPTIONS");
  System.out.println("---------");
  System.out.println("0. Connect.");
  System.out.println("1. Change port.");
  System.out.println("2. Cancel start-up.");
  System.out.println("");
  System.out.println("Choose an option.");
  try {
   short ans = in.nextShort();
   if (ans == 2){
    System.err.println("System.exit(0)");
    System.exit(0);
   }
   if (ans == 1){
     System.out.print("Enter the new port: ");
     port = in.nextInt();
     ans = 0;
   }
   if (ans == 0){
    System.out.print("Enter the IP: ");
    CS1 = new Socket(in.next(), port); //Creating an instane of the "CS1" socket
    System.err.print("Request sent on port " + port + ".");
    DOS1 = new DataOutputStream(CS1.getOutputStream());
    DIS1 = new DataInputStream(CS1.getInputStream()); //creating output and input streams
    System.err.println("Instances created!");
    if (DIS1 != null){
     System.err.println("DIS1 is connected!");
    }
    else {
     System.err.println("DIS1 is null!");
    }
    if (DOS1 != null) {
     System.err.println("DOS1 is connected!");
     System.out.print("Enter input: ");
     String FirstMessage = in.nextLine();
     DOS1.writeBytes(FirstMessage);
     String SecondMessage = DIS1.readLine();
     System.out.println(SecondMessage);
     if (SecondMessage.equals(FirstMessage)) {
     }
     else {
      System.out.println("Please check the code, FirstMessage != SecondMessage");
      }
    }
    else {
     System.err.println("DOS1 is null!");
    }
    if (CS1 != null) {
     System.err.println("CS1 is connected!");
    }
    else {
     System.err.println("CS1 is null!");
    }
    DIS1.close();
    DOS1.close();
    CS1.close(); //closing everything
   }
  }
  catch (IOException e){
   System.err.println("IOException: " + e.getMessage());
  }
 }
}

服務器代碼:

import java.net.*;
import java.io.*;
import java.util.*;
public class TCPServer {
 /*Connecting to client
  */
 public static void Server (String[] args) {
  System.err.print("Starting up...");
  ServerSocket SS1 = null;
  DataOutputStream DOS1 = null;
  DataInputStream DIS1 = null;//Setting the values to null
  System.err.println("All created.");
  Scanner in = new Scanner(System.in);
  try {
   SS1 = new ServerSocket(47361); //setting the socket SS1 to port 5000 and creating an instance
   System.err.print("Listening on port 47361...");
   Socket CS1 = SS1.accept(); //accepting the connection request
   DOS1 = new DataOutputStream(CS1.getOutputStream());
   DIS1 = new DataInputStream(CS1.getInputStream());//creating output and input streams
   System.err.println("Instances created!");
   if (DIS1 != null){
    System.err.println("DIS1 is connected!");
    System.err.println(DIS1);
   }
   else {
    System.err.println("DIS1 is null!");
   }
   if (DOS1 != null) {
    System.err.println("DOS1 is connected!");
    System.err.println(DOS1);
   }
   else {
    System.err.println("DOS1 is null!");
   }
   if (SS1 != null) {
    System.err.println("SS1 is connected!");
    System.err.println(SS1);
   }
   else {
    System.err.println("SS1 is null!");
   }
   String FirstMessage = null;
   FirstMessage = DIS1.readLine();
   System.out.println(FirstMessage);
   String SecondMessage = FirstMessage;
   DOS1.writeBytes(SecondMessage);
   DIS1.close();
   DOS1.close();
   SS1.close(); //closing everything
  }
  catch (IOException error){
   System.err.println("IOException " + error.getMessage());
  }
catch (java.util.InputMismatchException error2){
   System.err.println("IOException " + error2.getMessage());
  }
 }
}

到目前為止,它幾乎是一個EchoServer,沒有Buffered。 我看着調試器,它卡在FirstMessage = DIS1.readLine(); 服務器中的行和String SecondMessage = DIS1.readLine(); 在客戶端。 而且,即使輸入了所有可能的輸入,服務器和客戶端也都在等待輸入。 為什么會發生這種情況? 我該如何做呢?

旁注:我知道I / O流或套接字永遠不會等於null。 另外,編譯器警告我類java.io.DataInputStream已被棄用。 您建議使用哪些其他課程?

另一個注意事項:我是IO新手,請不要為此而殺我哈哈:)

謝謝!

常見問題。 您正在閱讀的行,但您不是在寫行。 寫入時在消息中添加行終止符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM