Updated Socket handling

Still a WIP
This commit is contained in:
Jesper Saastamoinen 2024-12-13 09:53:58 +01:00
parent c44334f196
commit 47ac4552ab
3 changed files with 71 additions and 14 deletions

View file

@ -19,16 +19,23 @@ class ClientHandler extends Thread {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int bytesRead = in.read(buffer); int bytesRead = in.read(buffer);
if(bytesRead == -1){
System.out.println("Client closed the Connection");
}
String message = new String(buffer, 0, bytesRead); String message = new String(buffer, 0, bytesRead);
System.out.println("Received message from client: " + message); System.out.println("Received message from client: " + message);
String response = "Hello from froj!"; String response = "Hello from froj!";
out.write(response.getBytes()); out.write(response.getBytes());
// Close the socket SocketServer.messageQueue.put(message);
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error handling client connection: " + e.getMessage()); System.out.println("Error handling client connection: " + e.getMessage());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} }
} }
} }

View file

@ -1,34 +1,55 @@
package com.jsaasta.froj.stdlib.socket; package com.jsaasta.froj.stdlib.socket;
import java.io.*; import java.io.*;
import java.net.ConnectException;
import java.net.Socket; import java.net.Socket;
public class SocketClient { public class SocketClient {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
while(true) { while(true) {
Socket socket = new Socket("localhost", 8000);
System.out.println("Connected to server");
// Get input and output streams
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
InputStreamReader input = new InputStreamReader(System.in); InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input); BufferedReader reader = new BufferedReader(input);
// Send message to server
String message = reader.readLine(); String message = reader.readLine();
String response = sendMessage(message);
if (response != null) {
System.out.println("Received response from server: " + response);
}
}
}
public static String sendMessage(String message) throws IOException {
Socket socket = null;
try {
socket = new Socket("localhost", 8000);
System.out.println("Connected to server");
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
out.write(message.getBytes()); out.write(message.getBytes());
// Read response from server
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int bytesRead = in.read(buffer); int bytesRead = in.read(buffer);
String response = new String(buffer, 0, bytesRead); String response = new String(buffer, 0, bytesRead);
System.out.println("Received response from server: " + response);
// Close the socket return response;
socket.close(); } catch (ConnectException e) {
System.out.println("Connection refused. Is the server running?");
return null;
} catch (IOException e) {
System.out.println("Error sending message: " + e.getMessage());
return null;
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.out.println("Error closing socket: " + e.getMessage());
}
}
} }
} }
} }

View file

@ -2,6 +2,7 @@ package com.jsaasta.froj.stdlib.socket;
import com.jsaasta.froj.FrojCallable; import com.jsaasta.froj.FrojCallable;
import com.jsaasta.froj.Interpreter; import com.jsaasta.froj.Interpreter;
import com.jsaasta.froj.stdlib.StdlibRuntimeError;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -9,8 +10,12 @@ import java.io.OutputStream;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.List; import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class SocketServer implements FrojCallable { public class SocketServer implements FrojCallable {
protected static BlockingQueue<String> messageQueue = new LinkedBlockingQueue<>();
private static ServerSocket serverSocket;
@Override @Override
public int arity() { public int arity() {
return 1; return 1;
@ -21,7 +26,7 @@ public class SocketServer implements FrojCallable {
try { try {
Double param = (Double) arguments.get(0); Double param = (Double) arguments.get(0);
int port = param.intValue(); int port = param.intValue();
ServerSocket serverSocket = new ServerSocket(port); serverSocket = new ServerSocket(port);
System.out.println("Server started. Listening for incoming connections..."); System.out.println("Server started. Listening for incoming connections...");
while (true) { while (true) {
@ -30,9 +35,33 @@ public class SocketServer implements FrojCallable {
ClientHandler clientHandler = new ClientHandler(socket); ClientHandler clientHandler = new ClientHandler(socket);
clientHandler.start(); clientHandler.start();
String nextMessage = getNextMessage();
try{
return Double.parseDouble(nextMessage);
} catch (NumberFormatException e) {
return nextMessage;
}
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e.getMessage());
} catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally{
shutdown();
} }
} }
public static String getNextMessage() throws InterruptedException {
return messageQueue.take();
}
public static void shutdown() {
try {
serverSocket.close();
} catch (IOException e) {
System.out.println("Error shutting down server: " + e.getMessage());
}
}
} }