Reputation: 294
I am making a messaging application that would connect two or more users on a local network and start messaging.It doesnot require an internet connection. Now I don't know Where to make the socket and where serverSocket? Who would be the client and who would be the server? At the time the code that I've written is like this ....
static ServerSocket server;
static Socket client;
public static void main(String[] args) throws IOException {
try{
server=new ServerSocket(65474);
}
catch(IOException e){
System.out.println("Port not Free");
}
while(server.isClose()==false){
client=server.accept();
}
BufferedReader rdr=new BufferedReader(new InputStreamReader(client.getInputStream()));
From here ahead I will get the input from the client. Is this code correct?Would client connect to the server correctly?
Upvotes: 0
Views: 1658
Reputation: 99
You are infinitely accepting clients BUT you will never get to read their data.
You should put it like that:
while(!server.isClosed() {
client=server.accept();
BufferedReader rdr=new BufferedReader(new InputStreamReader(client.getInputStream()));
}
You can remove the ==false and just negate the boolean youre getting. It will be set to true if it returns false and false if it returns true.
Dont forget to read the data and to display it on the screen like:
String receivedText = rdr.readLine();
System.out.println(receivedText);
Upvotes: 2