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
JavaBot2mo 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
dan1st2mo ago
Are you actually stopping the server process?
Yarden
YardenOP2mo ago
I'm doing this Also something similar in the client
No description
dan1st
dan1st2mo ago
Are you also closing the ServerSocket? Can you show the full class in a code block (NOT a screenshot)?
Yarden
YardenOP2mo ago
I do socket.close() so I think so no? yes i will send you
No description
Yarden
YardenOP2mo ago
It doesn't let me it says it's too long
dan1st
dan1st2mo ago
If you don't close serverSocket, the server will still be bound skip the whole client handling code
Yarden
YardenOP2mo ago
No description
dan1st
dan1st2mo ago
likely still too much text
john
john2mo 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.
john
john2mo 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
dan1st2mo ago
Ok so why are you starting more server processes? If a client disconnects, another client can connect
Yarden
YardenOP2mo ago
What do you mean? How to avoid this?
dan1st
dan1st2mo ago
Stop the server before starting a new one? Why do you want to avoid that? Just don't start more servers?
Yarden
YardenOP2mo 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
dan1st2mo ago
so the server should only handle a single client and then stop?
Yarden
YardenOP2mo ago
Yep once the client says "bye"
dan1st
dan1st2mo ago
you already almost have that then you don't need the loop and also make sure you close serverSocket at the end
Yarden
YardenOP2mo 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
dan1st2mo ago
I am talking about the outer loop currently, your server is capable of handling multiple clients
Yarden
YardenOP2mo 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
dan1st2mo ago
that's exactly what your old version did stop the servers that are still running
dan1st
dan1st2mo ago
In the console view in Eclipse, you have these button
No description
dan1st
dan1st2mo 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
YardenOP2mo ago
doesn't work 🫠 The port is still broken
dan1st
dan1st2mo ago
Show me the Eclipse console view
Yarden
YardenOP2mo ago
No description
dan1st
dan1st2mo ago
there's the double-X button as I said
No description
dan1st
dan1st2mo ago
you need to click that and then the other button should turn red
Yarden
YardenOP2mo ago
I did also I'll show you
Yarden
YardenOP2mo ago
No description
Yarden
YardenOP2mo ago
And then i'm pressing f11 and i get an error
dan1st
dan1st2mo 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
YardenOP2mo ago
You are just a god Thank you so much ❤️
JavaBot
JavaBot2mo 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
YardenOP2mo 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
YardenOP2mo ago
Or maybe am I suppose to open a new help post?
dan1st
dan1st2mo 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
YardenOP2mo 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
dan1st2mo ago
What do you mean with newLine()?
Yarden
YardenOP2mo ago
No description
dan1st
dan1st2mo ago
Is that BufferedWriter going to System.out?
Yarden
YardenOP2mo 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
dan1st2mo ago
How did you create bufferedWriter? and what prints it to the console? Are you using a BufferedReader on the other side?
Yarden
YardenOP2mo 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
dan1st2mo ago
Can you show where this is received and printed to the console?
Yarden
YardenOP2mo 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
dan1st2mo ago
client
john
john2mo 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
dan1st2mo ago
hm, that's weird
Yarden
YardenOP2mo ago
Right? But it seems like everything is working pretyy much alright So thank you very much ❤️
JavaBot
JavaBot2mo 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
JavaBot2mo ago
Post Closed
This post has been closed by <@218019068219883520>.
Want results from more Discord servers?
Add your server