I've created 1 client server, but everytime I'm exiting the server I can't connect anymore

In order to fix this problem I need to change the port. But the problem is that I did it like 8 times, so 8 ports are broken now.. When I'm writing bye, the server shuts down, but it seems like the port is still taken, what to do and how to fix it? Thank you
No description
53 Replies
JavaBot
JavaBot4w ago
This post has been reserved for your question.
Hey @Yarden! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st4w ago
Are you actually stopping the server process?
Yarden
Yarden4w ago
I'm doing this Also something similar in the client
No description
dan1st
dan1st4w ago
Are you also closing the ServerSocket? Can you show the full class in a code block (NOT a screenshot)?
Yarden
Yarden4w ago
I do socket.close() so I think so no? yes i will send you
No description
Yarden
Yarden4w ago
It doesn't let me it says it's too long
dan1st
dan1st4w ago
If you don't close serverSocket, the server will still be bound skip the whole client handling code
Yarden
Yarden4w ago
No description
dan1st
dan1st4w ago
likely still too much text
Akif
Akif4w ago
public class Server {

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;

ServerSocket serverSocket = null;

serverSocket = new ServerSocket(1234);


while(true){

try {

socket = serverSocket.accept();

inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

while(true) {

String msgFromClient = bufferedReader.readLine();

System.out.println("Client: " + msgFromClient);

bufferedWriter.write("MSG Received");
bufferedWriter.newLine();
bufferedWriter.flush();



if (msgFromClient.equalsIgnoreCase("BYE")) {

bufferedWriter.flush();
socket.shutdownOutput();
break;
}
}
public class Server {

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;

ServerSocket serverSocket = null;

serverSocket = new ServerSocket(1234);


while(true){

try {

socket = serverSocket.accept();

inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

while(true) {

String msgFromClient = bufferedReader.readLine();

System.out.println("Client: " + msgFromClient);

bufferedWriter.write("MSG Received");
bufferedWriter.newLine();
bufferedWriter.flush();



if (msgFromClient.equalsIgnoreCase("BYE")) {

bufferedWriter.flush();
socket.shutdownOutput();
break;
}
}
This message has been formatted automatically. You can disable this using /preferences.
Akif
Akif4w ago
socket.close(); inputStreamReader.close(); outputStreamWriter.close(); bufferedReader.close(); bufferedWriter.close(); System.out.println("Socket closed: " + socket.isClosed());
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
This message has been formatted automatically. You can disable this using /preferences.
dan1st
dan1st4w ago
Ok so why are you starting more server processes? If a client disconnects, another client can connect
Yarden
Yarden4w ago
What do you mean? How to avoid this?
dan1st
dan1st4w ago
Stop the server before starting a new one? Why do you want to avoid that? Just don't start more servers?
Yarden
Yarden4w ago
I want this to be only 1 client server, so when the client disconnects I want the server to disconnects. That's why I want to avoid that The next level, the next program, will be for many clients, but I want to do this step by step
dan1st
dan1st4w ago
so the server should only handle a single client and then stop?
Yarden
Yarden4w ago
Yep once the client says "bye"
dan1st
dan1st4w ago
you already almost have that then you don't need the loop and also make sure you close serverSocket at the end
Yarden
Yarden4w ago
The guy in the tutorial said: * The first loop: ensures the server is constantly running. * The second loop: ensures that, once the client is connected, the server is constantly interacting with the client until the client disconnects. Where was he wrong? Maybe I've missed something?
dan1st
dan1st4w ago
I am talking about the outer loop currently, your server is capable of handling multiple clients
Yarden
Yarden4w ago
fixed it thank youuu! There is only 1 issue: When i'm changing back the port to a port I already used before, I still got the error. Is there a way to "free" the problematic ports?
dan1st
dan1st4w ago
that's exactly what your old version did stop the servers that are still running
dan1st
dan1st4w ago
In the console view in Eclipse, you have these button
No description
dan1st
dan1st4w ago
the red button stops the current program, the double-X clears the stopped consoles so red button to stop the currently running program, then double-X to make it not show stopped programs, then the red button again to stop the currently shown program etc
Yarden
Yarden4w ago
doesn't work 🫠 The port is still broken
dan1st
dan1st4w ago
Show me the Eclipse console view
Yarden
Yarden4w ago
No description
dan1st
dan1st4w ago
there's the double-X button as I said
No description
dan1st
dan1st4w ago
you need to click that and then the other button should turn red
Yarden
Yarden4w ago
I did also I'll show you
Yarden
Yarden4w ago
No description
Yarden
Yarden4w ago
And then i'm pressing f11 and i get an error
dan1st
dan1st4w ago
now since the button is red, you can click the red button to stop that process then you click on the double-X again and on the red button again and on the double-X and on the red button until you stopped all servers
Yarden
Yarden4w ago
You are just a god Thank you so much ❤️
JavaBot
JavaBot4w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Yarden
Yarden4w ago
Last last last issue When I'm trying to write a new line, it shows the marker on the existing server's message, but why? I have the line "newLine() It suppose to take 1 line space no?
No description
Yarden
Yarden4w ago
Or maybe am I suppose to open a new help post?
dan1st
dan1st4w ago
? Maybe you are using System.out.print instead of System.out.println keep it open as long as you want to and close it if you think you are finished with it
Yarden
Yarden4w ago
Wait yes totally this So i'm a bit confused, what exactly the use of newLine()? I thought it take a line space
dan1st
dan1st4w ago
What do you mean with newLine()?
Yarden
Yarden4w ago
No description
dan1st
dan1st4w ago
Is that BufferedWriter going to System.out?
Yarden
Yarden4w ago
Hmmm probably not? Isn't it "catch" the message the client writes in the system.out? I mean the BufferedReader And then the writer put it in the server's chat
dan1st
dan1st4w ago
How did you create bufferedWriter? and what prints it to the console? Are you using a BufferedReader on the other side?
Yarden
Yarden4w ago
I have InputStreamReader, the BufferedReader takes the information from the InputStreamReader. The System.out prints in to the console no? I'm using BufferedReader on both client and server sides
dan1st
dan1st4w ago
Can you show where this is received and printed to the console?
Yarden
Yarden4w ago
client or server? socket = serverSocket.accept();
inputStreamReader = new InputStreamReader(socket.getInputStream()); outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
bufferedReader = new BufferedReader(inputStreamReader); bufferedWriter = new BufferedWriter(outputStreamWriter);
while(true) {
String msgFromClient = bufferedReader.readLine();
System.out.println("Client: " + msgFromClient);
bufferedWriter.write("MSG Received"); bufferedWriter.newLine(); bufferedWriter.flush();
dan1st
dan1st4w ago
client
Akif
Akif4w ago
try {
socket = new Socket("localhost", 1234);
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

Scanner scanner = new Scanner(System.in);

while(true){
String msgToSend = scanner.nextLine();
bufferedWriter.write(msgToSend);
bufferedWriter.newLine();

System.out.println("Server: " + bufferedReader.readLine());

if(msgToSend.equalsIgnoreCase("BYE"))
break;

}
try {
socket = new Socket("localhost", 1234);
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());

bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);

Scanner scanner = new Scanner(System.in);

while(true){
String msgToSend = scanner.nextLine();
bufferedWriter.write(msgToSend);
bufferedWriter.newLine();

System.out.println("Server: " + bufferedReader.readLine());

if(msgToSend.equalsIgnoreCase("BYE"))
break;

}
This message has been formatted automatically. You can disable this using /preferences.
dan1st
dan1st4w ago
hm, that's weird
Yarden
Yarden4w ago
Right? But it seems like everything is working pretyy much alright So thank you very much ❤️
JavaBot
JavaBot4w ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot4w ago
Post Closed
This post has been closed by <@218019068219883520>.
Want results from more Discord servers?
Add your server