as a part of my homework I have to build two classes one listener and one that is making the requests to the server (the server is already written by my teacher and I don't know how code looks like). In theory: The server that I'm connected to should reply with the exact same requests I sent to him. But in practice I get nothing back.
public class Listenerthread extends Thread {
Socket s;
Scanner answerServer;
public Listenerthread(Socket socket) {
this.s = socket;
this.answerServer = new Scanner(new BufferedReader(new InputStreamReader(s.getInputStream())));
}
public void run() {
System.out.println("Listening to the responses from the server......");
while (true) {
if (answerServer.hasNext()) {
System.out.println(answerServer.nextLine());
}
}
}
}
public class Mainthread {
public static void main(String[] args) throws ParseException {
Socket s = new Socket("someServer", 9999);
Listenerthread server = new Listenerthread(s);
server.start();
if (s.isConnected()) {
System.out.println("Connected");
}
String req = "Heyyy mate"; // server should sent me this back
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
pw.println(req);
pw.flush();
//pw.close();
//s.close();
}
}
Update: A comment that OP left on the question after I started writing this answer shows that OP does not actually need to handle the server side of the communications. I will leave this answer for now in case it could still be useful.
When you have a client/server model, you should use a ServerSocket on the server side, as you alluded to in your question but then do not appear to have done in your code.
ServerSocket
Here is a ServerSocket example that may clear that up.
ServerSocket serverSocket;
public void serverSocketTest()
{
serverSocket = new ServerSocket(9999);
// each of the below methods will happen on separate threads
new Thread(this::serverSideAcceptConnectionFromClient).start();
new Thread(this::clientSideConnectToServer).start();
}
public void clientSideConnectToServer()
{
try {
System.out.println("Client is trying to connect to server...");
Socket connectionToServer = new Socket("localhost", 9999);
OutputStream thisGoesToTheServer = connectionToServer.getOutputStream();
InputStream thisIsDataComingFromServer = connectionToServer.getInputStream();
System.out.println("Client successfully connected to server.");
} catch(IOException ex) {
System.out.println("Connection to server failed. (" + ex + ")");
}
}
public void serverSideAcceptConnectionFromClient()
{
try {
System.out.println("Server is listening for potential clients...");
Socket connectionFromClient = serverSocket.accept();
InputStream thisIsDataComingFromClient = connectionFromClient.getInputStream();
OutputStream thisIsGoesBackOutToClient = connectionFromClient.getOutputStream();
System.out.println("Server accepted a client");
} catch(IOException ex) {
System.out.println("Error while listening for clients. (" + ex + ")");
}
}
Related
I have created this code snippet in both a single threaded version and multithreaded for a client/server setup I have going. I have tested both (recording the avg turn around time) and have gotten EXTREMELY similar results within margin of error when running multiple simple server commands at once. have I implememnted my client handler wrong?
This is my first time trying to implement a multithreaded server and from my understanding it just a matter of putting in a client handler being
`
class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
`
below is the snippet of the whole server code.
`
public class Server {
public static void main(String[] args) {
if (args.length < 1) return;
int port = Integer.parseInt(args[0]);
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server is listening on port " + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
new ServerThread(socket).start();
}
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
String text;
do {
text = reader.readLine(); // reads text from client
Process p = Runtime.getRuntime().exec(text);
BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
String outputLine;
while ((outputLine = stdout.readLine()) != null) { // while serverMsg is not empty keep printing
writer.println(outputLine);
}
stdout.close();
writer.println("ENDCMD");
// Text here should just write back directly what the server is reading...?
}
while (!text.toLowerCase().equals("exit"));
socket.close();
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
`
I have tested both (recording the avg turn around time) and have gotten EXTREMELY similar results within margin of error when running multiple simple server commands at once. have I implememnted my client handler wrong?
If you are not making a new connection for each command that you send, then this would be expected. Since each connection runs on one thread, a multi-threaded approach, as you have shown, would have the same speed as if you didn't make a new thread for each connection. The difference is that, without multi-threading, you can only have one connection at a time.
Guys I want to convert my server implementation into multi thread so that it can handle multiple requests. Basically the server is connected with an android application and it is recieving an image from android application. I want to add a thread so that it can handle multiple requests and the thread should start when the request is recieved. Kindly help me out.
This is the Server Code.
public static void main(String[] args) throws UnknownHostException, IOException, MatlabInvocationException, MatlabConnectionException {
while (true) {
try {
serverSocket = new ServerSocket(4001); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
System.out.println("Server started. Listening to the port 4001");
clientSocket = serverSocket.accept();
DataInputStream inputFromClient = new DataInputStream(clientSocket.getInputStream());
int count = inputFromClient.readInt();
int available = inputFromClient.available();
System.out.println("Length of Image in Bytes:" + count);
System.out.println("available:" + available);
image = new byte[count];
inputFromClient.readFully(image);
System.out.println(image.length);
System.out.println(image);
final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image));
ImageIO.write(bufferedImage, "jpg", new File("image.jpg"));
System.out.println("Image has been wriiten in the directory.");
MatlabProxyFactory mpf = new MatlabProxyFactory();
MatlabProxy proxy = mpf.getProxy();
proxy.eval("conclusion=DetectColorL");
Object[] obj = proxy.returningEval("conclusion", 1);
String Message = obj[0].toString();
DataOutputStream outTo = new DataOutputStream(clientSocket.getOutputStream());
outTo.writeUTF(Message.toString());
System.out.println(Message);
proxy.disconnect();
serverSocket.close();
To make it multithreaded you want to be able to have multiple clients connected at the same time, to handle multiple requests instead of one at a time.
To do so, your server will have to permanently accept new clients.
public static void main(String[] args) {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(4001); // Server socket
System.out.println("Server started. Listening to the port 4001");
while (true) {
// Always accept new clients
Socket clientSocket = serverSocket.accept();
new RequestHandlingClass(clientSocket).start(); // Make a new thread and call it's run procedure
}
} catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
Now our server accepts multiple clients we have to implement the RequestHandlingClass class. You want that class to listen for client requests and handle them.
public class RequestHandlingClass() extends Thread {
Socket clientSocket;
DataInputStream inputFromClient;
RequestHandlingClass(Socket clientSocket) {
this.clientSocket = clientSocket;
this.inputFromClient = new DataInputStream(clientSocket.getInputStream());
// ...
}
public void run() {
// Handle client requests
}
}
Based on your question I suppose you want to execute the "image handling" code in the run method.
I'm learning java. I'm trying to make a simple client/server chat system. What I have so far is a program where the server accepts multiple client connections by giving them each a seperate thread.
My problem now, is that I can't figure out how to get an input from one client, and then have it be sent amongst all of the clients, thus essentially have a very very simple chat mechanic. How would I go about accomplishing this? What would be the simpler way?
My code so far is here;
class Client {
public static void main(String argv[]) throws Exception {
String sentMessage; //variable for input
String receivedMessage; //variable for output
String status;
boolean running;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("127.0.0.1", 5622); //name of computer to connect with and port number to use
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println("Client Side\n");
running = true;
while(running)
{
sentMessage = inFromUser.readLine(); //user inputs text to variable 'xInput'
outToServer.writeBytes(sentMessage + '\n'); //the variable is sent to the server
status = inFromServer.readLine();
System.out.println("FROM SERVER: " + status); //display to user
}
clientSocket.close();
}
}
The server code.
class Server {
public static void main(String argv[]) throws Exception {
String clientMessage;
boolean listening = true;
int portNumber = 5622;
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
while (listening) {
new ServerThread(serverSocket.accept()).start();
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
}
}
The thread that handles the client connections.
public class ServerThread extends Thread {
private Socket socket = null;
public ServerThread(Socket socket) {
super("ServerThread");
this.socket = socket;
}
public void run () {
int msgCnt = 0;
try (
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
) {
//something needs to go here
} catch (IOException e) {
e.printStackTrace();
}
}
If you are looking for a simple client-server communication samples then please have a look at below posts where I have described it step by step.
Multiple clients access the server concurrently
Java Server with Multiclient communication.
I'm trying to create a Java program as a proxy to view packets from an incoming source to debug. To do this, I have created a simple Java server application and have edited my host file on the device. Everything works fine as of now, (even my Relay class file) but I am trying to make it into a full fledged proxy. How could I incorporate elements to send data to the server, and send the response back to the client? Sort of like a Man-In-The-Middle type of thing.
import java.net.*;
import java.io.*;
import org.ini4j.Ini;
public class RelayMultiClient extends Thread {
private Socket socket = null;
Socket relay = null;
public RelayMultiClient(Socket socket) {
super("RelayMultiClient");
this.socket = socket;
}
public void run() {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
if(Relay.max_clients == Relay.connected_clients) {
//There are too many clients on the server.
System.out.println("Connection refused from " + socket.getRemoteSocketAddress() + ": Too many clients connected!");
out.close();
in.close();
socket.close();
}
else {
Ini ini = new Ini(new File("settings.ini"));
Relay.connected_clients++;
System.out.println("Connection from client " + socket.getRemoteSocketAddress() + " established. Clients Connected: " + Relay.connected_clients);
while (in.readLine() != null) {
//Send data to the server
//Receive data from server and send back to client
}
System.out.println("Connection from client " + socket.getRemoteSocketAddress() + " lost.");
Relay.connected_clients--;
out.close();
in.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks,
Chris
P.S: I'm not attempting to get HTTP data, I am trying to get data from a game I have created. I don't know if this type of data requires any extra handling.
How could I incorporate elements to send data to the server, and send the response back to the client?
Try the following example as basic proxy:
public class Proxy {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1230); // proxy port
Socket socket = serverSocket.accept();
Socket relay = new Socket("localhost", 1234); // server address
new ProxyThread(relay.getInputStream(), socket.getOutputStream()).start();
new ProxyThread(socket.getInputStream(), relay.getOutputStream()).start();
}
}
class ProxyThread extends Thread {
private InputStream inputStream;
private OutputStream outputStream;
ProxyThread(InputStream inputStream, OutputStream outputStream) {
this.inputStream = inputStream;
this.outputStream = outputStream;
}
public void run() {
try {
int i;
while ((i = inputStream.read()) != -1) {
outputStream.write(i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
It lacks proper exception handling, only demonstrating the basic idea.
I'm having some trouble simulating a connection to my Server Socket, accept seems to continue blocking as it doesn't "see" the connection.
Here's some simplified code
#Test
public void testPDMServerThread() {
try {
ServerSocket serverSocket = new ServerSocket(0);
int port = serverSocket.getPort();
Socket clientSocket = new Socket("localhost", port);
PrintWriter clientRequest = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader serverResponse = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
serverThread = new ProducerMonitorServerThread(serverSocket.accept());
clientRequest.write("Hi!");
serverThread.start();
System.out.println("Server says: " + serverResponse.readLine());
assertEquals("RUNNABLE", serverThread.getState().toString());
} catch (IOException e) {
}
}
And here's the thread where the server should respond
public class ProducerMonitorServerThread extends Thread {
private Socket socket;
public ProducerMonitorServerThread(Socket socket) {
super("PDM");
this.socket = socket;
}
#Override
public void run() {
try {
PrintWriter serverResponse = new PrintWriter(socket.getOutputStream(), true);
BufferedReader clientRequest = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String serverInput, clientOutput;
while((serverInput = clientRequest.readLine()) != null) {
clientOutput = "Bye!";
System.out.println("Client says: " +serverInput);
serverResponse.write(clientOutput);
}
serverResponse.close();
clientRequest.close();
socket.close();
} catch (IOException e) {
}
}
}
It never seems to get past this line which is why I think accept is not seeing the connection
serverThread = new ProducerMonitorServerThread(testServer.accept());
I'm sure there's something fundamental that I'm just not seeing.
First of all, you should not ignore exceptions like you're doing.
The problem is not with accept. The problem is that the server uses readLine(), and the client never sends any EOL character, and never closes the socket. So the server is blocked waiting for an EOL to appear in the reader. The same is true for the client: it uses readLine() and the server never sends any EOL.
Use a debugger. It will help you find the cause of such problems.