I can't figure out what went wrong ! I'm trying to run a simple sockets comunication. I'm using Eclipse Mars 2 and by the way, it cashes and quits in debug mode and when I try to run Junit tests ! I don't know why !
#
# 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.
#
I get the error message above on the server, and a java.util.NoSuchElementException on the client ! precisely when I try to get the result from the server ( temp = scanner2.nextInt(); in Client.java )
Server class
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);
}
}
Client class
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);
}
}