简体   繁体   English

将客户端写入服务器Java

[英]Writing client to server Java

I need help. 我需要帮助。 I'm making a sample queue system. 我正在制作一个样本队列系统。 When I try to write data and store it to server, the server returns an exception. 当我尝试写入数据并将其存储到服务器时,服务器将返回异常。 I'm also a newbie on sockets so I will appreciate any help from anyone who will reply on my question. 我还是套接字的新手,因此,如果有任何人回答我的问题,我将不胜感激。

Here's my code for Student: 这是我给学生的代码:

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.");
      }
   }
}

and for the server: 对于服务器:

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");
      }
   }
}

You need to properly close the PrintWriter by adding an "out.close()" statement before it finishes it's execution. 您需要通过在完成执行之前添加“ out.close()”语句来正确关闭PrintWriter。 Since you'r enot doing that, the client socket is abruptly closed and you get an exception on the server side. 由于您不这样做,因此客户端套接字突然关闭,并且在服务器端出现异常。

Ideally this close() should be performed inside a finally block so you make sure it's executed. 理想情况下,此close()应该在finally块内执行,以确保执行它。 Also, as pointed out in another answer, you should only read once, store it in a variable, and then do whatever you need with the value. 同样,正如另一个答案中指出的那样,您应该只读取一次,将其存储在变量中,然后对值进行任何所需的操作。

Example: 例:

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!");
        }
    }
}

student: 学生:

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) {

                }
            }
        }
    }
}

You need to print out more than "Whoops" in the catch block. 您需要在catch块中打印出更多的内容而不是“ Whoops”。 What exception do you get? 你有什么例外? This will help you to understand what is going wrong. 这将帮助您了解问题所在。

You're reading two times, but send only once. 您正在阅读两次,但仅发送一次。 Regarding the cause of exception, I think it occurs because the client terminates without closing connection. 关于异常原因,我认为这是因为客户端在不关闭连接的情况下终止的。 Add socket close statement into client's code and exception will disappear. 在客户端代码中添加套接字关闭语句,异常将消失。 You're going to add null into collector though 您将尽管将null添加到收集器

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM