将客户端写入服务器Java

时间:2014-03-06 10:34:48

标签: java sockets io

我需要帮助。我正在制作一个样本队列系统。 当我尝试写入数据并将其存储到服务器时,服务器返回异常。 我也是插座上的新手,所以我会感谢任何能回复我问题的人的帮助。

这是我的学生代码:

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

3 个答案:

答案 0 :(得分:1)

您需要通过在执行完成之前添加“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) {

                }
            }
        }
    }
}

答案 1 :(得分:0)

您需要在catch块中打印出超过“Whoops”的内容。你有什么例外?这有助于您了解出了什么问题。

答案 2 :(得分:0)

您正在阅读两次,但只发送一次。关于异常的原因,我认为这是因为客户端在没有关闭连接的情况下终止。将socket close语句添加到客户端的代码中,异常将消失。您将通过

null添加到收藏家中