What I'm trying to do
A client/server application:
The client opens a GUI with a button "Read".
Clicking it it will open a new frame with some articles to read. At this stage a short String
.
Abstract:
- After you start both server and client , the Client will see the frame with a button.
- As a response, I want the server to send me specific/different things depending on what Operation I click.
What I tried
At this current stage of development I'm stuck with this error message:
Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error
This is the server-class:
public class StartServer {
public static void main(String[] argv) throws Exception {
@
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("Server started");
while (true) {
List<String> receive = new ArrayList<String>();
// Wait for client to connect
Socket aNewClientSocket = serverSocket.accept();
System.out.println("Server-Client connexion established!!");
ObjectInputStream toReceiveFromClient = new ObjectInputStream(aNewClientSocket.getInputStream());
receive = (List<String>) toReceiveFromClient.readObject();
System.out.println(aNewClientSocket.getLocalPort());
System.out.println(aNewClientSocket.getInetAddress());
switch (receive.get(0)) {
case "Reader button click":
List<Object> toSend = new ArrayList<Object>();
Article a=new Article();
a.setTitle("New Title");
toSend.add(a);
ObjectOutputStream objectWayToSend = new ObjectOutputStream(aNewClientSocket.getOutputStream());
objectWayToSend.writeObject(toSend);
objectWayToSend.flush();
default:
throw new IllegalArgumentException("Bad request from client!!");
}
The clients relevant method:
public void sendToServer(List<String> toSend) throws UnknownHostException, IOException {
try {
Socket socket = new Socket("localhost", 9999);
ObjectOutputStream objectWayToSend = new ObjectOutputStream(socket.getOutputStream());
objectWayToSend.writeObject(toSend);
objectWayToSend.flush();
objectWayToSend.close();
}
catch(Exception e) {
JOptionPane.showMessageDialog(frame, "The server is offline or connection can't be establish!");
}
}
@SuppressWarnings("unchecked")
public List<Object> receiveFromServer() throws UnknownHostException, IOException {
List<Object> receive = new ArrayList<Object>();
try {
Socket socket = new Socket("localhost", 9999);
ObjectInputStream objectToGet = new ObjectInputStream(socket.getInputStream());
receive=(List<Object>) objectToGet.readObject();
}
catch(Exception e) {
JOptionPane.showMessageDialog(frame, "The server is offline or connection can't be establish!");
}
return receive;
The referenced button in the GUI
btnCiteste = new JButton("Read");
btnCiteste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
List<String> toSend = new ArrayList<String>();
toSend.add("Read");
try {
sendToServer(toSend);
List<Object> received= receiveFromServer();
//new ArticoleReaderFrame ();
} catch (IOException e) {
e.printStackTrace();
}
}
});