I have made a simple java chat program that will have 1 server and 1 client. It works fine on windows, it can chat with each other within LAN.
But when I try it on Mac computer, it doesn't work. In the server program, if it works properly, it should freeze when I press the start button, and wait for client to join. But in Mac, it does nothing when I press start button. The program doesn't freeze, it's like clicking on a non-coded button. The client in Mac cannot join the server too (server hosted in Windows).
On Windows: Start server --> freeze(wait for client) --> Client joined --> able to chat
On Mac: Start server --> not freeze, like click on normal button --> Client clicked join --> nothing happens
In the Start Server button:
private void startsvbtnActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
server = new ServerSocket(7430);
client = server.accept();
System.out.println("Client request accepted: "+client.getOutputStream());
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
ReceiveMessage serverThread = new ReceiveMessage(dis,textarea);
serverThread.start();
} catch (IOException ex) {
System.out.println("No client available");
}
}
You're checking only IOException, as per doc there could be other exceptions. Catch all and print stacktrace.
http://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html#accept--
catch(Exception e){
e.printStackTrace();
}
Because your button doesn't freeze I would say that the problem is an exception is being thrown, hence your ActionListener returning virtually immediately.
Check your log file to see if your log message System.out.println("No client available"); is there. Also at the very least modify this code to output the stack trace, as I would bet that you cannot bind to the port specified here:
server = new ServerSocket(7430);
Related
I am porting an Android application to iOS platform. This is a app that uses TCP socket programming to communicate with an external ESP8266 device. To debug code I wrote a code in Java using IntelliJ IDE to act as server and Xcode simulator is acting as the server.
try {
ServerSocket serverSocket=new ServerSocket(1234);
System.out.println("Server Started");
System.out.println(serverSocket.getInetAddress());
Socket socket=serverSocket.accept();
System.out.println("Client Accepted");
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
System.out.println("Got: " +bufferedReader.readLine());
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
this is the code for Server using Java.
switch client.connect(timeout: 10) {
case .success:
toggleSwitch.isEnabled=true
connectBtn.isEnabled=false
upArrow.isEnabled=true
downArrow.isEnabled=true
default:
let alert = UIAlertController(title: "Connection Failed.", message: "SST Device is not available", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .`default`, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
self.present(alert, animated: true, completion: nil)
}
Above is the part where iOS device connect with the server.
resistanceValueInt-=1
let x=client.send(string:"\(-1)")
this is where the client sends data to the server. resistanceValueInt is starting at 0 when the app loads. And increment in each button press.
My question is connection with server established immediately. Though the increments and decrements to the values using buttons won't show up immediately. Simulator needs to quit in order to show the result in the Server Console.
Why simulator needs to be closed in order to deliver the data? How should I fix it?
in the server side i used bufferedReader.readLine() to read from the client.
Though in the swift code client writes data to stream by client.send("\(+1)"). this do not attach new line character to the stream. So that Java server do not know data is fully came through or not because there is no new line character.
So client.send("\(+1)\n") is used. Now Server knows data is came through correctly. So that it prints the required output in the console.
The title basically says it all but just but to reiterate:
I need my Java program to kill itself (dispose w/e) if a specific external program launches.
My java program uses a global hook that listens for mouse input, when I remote into a clients computer the mouse listener/GUI that my program creates can cause all kinds of issues with the mouse if used I'm while connected. To handle this I need my program to automatically "turn off" when the screen-connect application we use launches.
I am already using a global hook to capture mouse input, is there something similar I could use for system events maybe?
Depending on the version of windows respectively Java you are using there are various libraries you could be using to simply regularly "scan" the operating system for a list of running processes.
If something shows up that you don't like, simply react using System.exit for example.
In your first program, just create a new Thread and let it run. Put this code in the run method:
try {
ServerSocket socket = new ServerSocket(1111); // the paramater is a port and you can choose any port you want
socket.accept();
socket.close();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
This will wait for a connection and once the connection is made, it will close itself. That is because the accept method that waits for a connection blocks until there is a connection made. That's why you put it in another thread.
After it recieves the connection it will unblock so the code will continue and exit the program! And it MUST be in another friend so it doesn't block your application!
In the second application, in the beginning just make a connection!
To make a connection use this code:
try (Socket socket = new Socket("localhost" ,1111);){
socket.close();
} catch (IOException e) {
}
This will connect it and once it does, the first program will close. Now just continue on with this program! If you don't understand the sockets the best, then check out this link. It is awesomely explained:
https://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html
I am writing an application that streams data that clients can then listen to and receive. However I am running into an issue with closing a socket when a client is no longer listening.
What I do is create a ServerSocket, when then waits for a connection and once it is connected, I start streaming the data. However, once the client is no longer connected, I am stuck in a loop of streaming and cannot tell if anyone is listening. Is there a way around this?
try {
serverSocket = new ServerSocket(STREAM_PORT);
Socket clientSocket = serverSocket.accept();
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
pw.println("some data");
}
} catch (SocketException e) {
// Never occurs when client disconnects
} catch (IOException e) {
// Never occurs when client disconnects
}
I have tried using socket.isClosed(), but it always returns false. Am I approaching this from the wrong angle, or is there a way to do it. I would ideally not want the client to have to send the server a "end" command.
EDIT: Edited to reflect what current code I am running after #Rod_Algonquin suggestion
As you are using PrintWriter, which swallows I/O exceptions, you need to call checkError() after each write to see if an error has occurred.
I couldn't find a similar post here, so here it is:
When I start the server and a couple of clients, I want the users on all clients to be able to input data to be processed. Also, I want the server to be able to exit the listening loop once all client sessions are terminated.
Server code listening loop:
//Console console = System.console();
// or
//InputStreamReader str = new InputStreamReader(System.in);
//BufferedReader uinp = new BufferedReader(str);
// ...
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
while (listening) {
new SocketServerThread(serverSocket.accept()).start();
//String serverInput = console.readLine("> ");
//if ( serverInput.equalsIgnoreCase("exit") ) { listening = false; }
// or
//System.out.print("> ");
//if ( (uinp.readLine()).equalsIgnoreCase("exit") ) { listening = false; }
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
Here's the thing: 1) If I code the server to be able to receive input and process it (like an "exit" string to tell the server to exit the listening loop), it seems to me that the second client's thread won't run, which I find quite peculiar. 2) If I don't code the server to be able to exit the listening loop, how can the server exit the loop at the user's will?
As you can see, I've tried (a) Console and (b) InputStreamReader and BufferedReader, but none of them worked.
So, my question is this: how can I have the server quit the listening loop at the user's will and allow the second client's thread run at the same time?
I think you have confused the purpose of client server architecture.
A server is a centralized system to which clients can connect and request one or more services. So what you're saying is that you want to stop the centralized system i-e the server and still let other clients keep running? This is not possible. Because a client can not exist without a server (It won't be called a client if it doesn't has a server in the first place)
So if you quit server's listening loop, no client can connect to it. Hence no client thread can be executed.
If you want to that all server-client sessions are terminated at the will of the clients' users. U can do a workaround.
Keep a count variable at the server end. Increment it with each new client coming and decrement on the client leaving. Also, keep a boolean variable like toClose initialised with false at server end again. Now let's say client 'A' tells server to close (like "exit" command as u have already done), you change the value of toClose to true.
Now at the end of each client's session, you check something like this:
if (toClose == true && count <=0 )
exitListeningLoop()
For some reason when I connect one Java client to my Cpp server it works perfectly. But when another Java applet tries to connect in addition to the first, it does but it stops receiving data from the server. Some of the other attempts would completly freeze the applet. I have searched everywhere on the 'net but found nothing. I would appreciate any help on the subject like advice, links or source. My only other option (since Flash is out of the question) is to use Active X, but then I would lose all my crossplatform-ability :(. My source (minus debugging code and so on) follows.
public void init() {
try {
socket = new Socket("localhost",4000);
} catch (UnknownHostException e) {
System.out.println("Unknown host");
} catch (IOException e) {
System.out.println("IO Exception");
return;
}
BufferedReader fromServer = null;
PrintWriter toServer = null;
fromServer = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
toServer =
new PrintWriter(socket.getOutputStream(), true);
toServer.flush();
It sounds like your Cpp server can't handle multiple connections. Can you verify that you can accept more than one incoming network connection?
I found the solution! You can't run multiple applets in one browser and one machine. It will work if you load them in seperate browsers (like IE and FF) or load it on a seperate machine (like remotely). I hope this helps.