簡體   English   中英

將客戶端寫入服務器Java

[英]Writing client to server Java

我需要幫助。 我正在制作一個樣本隊列系統。 當我嘗試寫入數據並將其存儲到服務器時,服務器將返回異常。 我還是套接字的新手,因此,如果有任何人回答我的問題,我將不勝感激。

這是我給學生的代碼:

import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Student {
   public static void main(String args[]) {
      Scanner sc = new Scanner (System.in);
  try {
     Socket skt = new Socket("localhost", 1234);
     PrintWriter out = new PrintWriter(skt.getOutputStream(), true);

     System.out.println("Hello!");

     System.out.print("Please enter name: ");
     String name = sc.next();
     System.out.print("Please enter ID Number: ");
     String id = sc.next();
     String student = name +" - "+ id;
     out.print(student);

      } catch(Exception e) {
          System.out.println("Whoops! It didn't work.");
      }
   }
}

對於服務器:

import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
   public static void main(String args[]) {
      Scanner sc = new Scanner (System.in);
      ArrayList<String> collector = new ArrayList<String>();

  try {
     ServerSocket srvr = new ServerSocket(1234);
     Socket skt = srvr.accept();
     BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));

     System.out.print(in.readLine());
     String x = in.readLine();
     collector.add(x);


      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

您需要通過在完成執行之前添加“ out.close()”語句來正確關閉PrintWriter。 由於您不這樣做,因此客戶端套接字突然關閉,並且在服務器端出現異常。

理想情況下,此close()應該在finally塊內執行,以確保執行它。 同樣,正如另一個答案中指出的那樣,您應該只讀取一次,將其存儲在變量中,然后對值進行任何所需的操作。

例:

public class Server {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> collector = new ArrayList<String>();

        try {
            ServerSocket srvr = new ServerSocket(1234);
            Socket skt = srvr.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));

            final String studentInfo = in.readLine();
            System.out.print(studentInfo);
            collector.add(studentInfo);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Whoops! It didn't work!");
        }
    }
}

學生:

public class Student {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = null;
        try {
            Socket skt = new Socket("localhost", 1234);
            out = new PrintWriter(skt.getOutputStream(), true);

            System.out.println("Hello!");

            System.out.print("Please enter name: ");
            String name = sc.next();
            System.out.print("Please enter ID Number: ");
            String id = sc.next();
            String student = name + " - " + id;
            out.print(student);
        } catch (Exception e) {
            System.out.println("Whoops! It didn't work.");
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {

                }
            }
        }
    }
}

您需要在catch塊中打印出更多的內容而不是“ Whoops”。 你有什么例外? 這將幫助您了解問題所在。

您正在閱讀兩次,但僅發送一次。 關於異常原因,我認為這是因為客戶端在不關閉連接的情況下終止的。 在客戶端代碼中添加套接字關閉語句,異常將消失。 您將盡管將null添加到收集器

暫無
暫無

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

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