【发布时间】:2017-04-24 05:58:02
【问题描述】:
我不知道出了什么问题!我正在尝试运行一个简单的套接字通信。我正在使用 Eclipse Mars 2,顺便说一句,当我尝试运行 Junit 测试时,它会在调试模式下兑现并退出!不知道为什么!
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x739ac3e1, pid=2144, tid=0x0000182c
#
# JRE version: Java(TM) SE Runtime Environment (8.0_111-b14) (build 1.8.0_111-b14)
# Java VM: Java HotSpot(TM) Client VM (25.111-b14 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [chtbrkg.dll+0x1c3e1]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\User.ASUS\Documents\eclipse\workspace\Sockets\hs_err_pid2144.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
我在服务器上收到上述错误消息,在客户端上收到 java.util.NoSuchElementException !正是当我尝试从服务器获取结果时( temp =scanner2.nextInt(); 在 Client.java 中)
服务器类
package main;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("Waiting for the client...");
Socket socket = serverSocket.accept();
System.out.println("Client connected");
Scanner scanner = new Scanner(socket.getInputStream());
int number = scanner.nextInt();
int temp = number * 2;
PrintStream printStream = new PrintStream(socket.getOutputStream());
printStream.println(temp);
}
}
客户端类
package main;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
int number, temp = 0;
Scanner scanner = new Scanner(System.in); // to read input from user
Socket socket = new Socket("127.0.0.1", 1234);
Scanner scanner2 = new Scanner(socket.getInputStream()); // used to accept results from the server
System.out.println("Enter any number");
number = scanner.nextInt();
PrintStream printStream = new PrintStream(socket.getOutputStream());
printStream.println(number); // send the number to the server
temp = scanner2.nextInt();
System.out.println(temp);
}
}
【问题讨论】: