
i am not been able to find the error in the program server:
import java.io.*; import java.net.*;
public class Server { public static void main(String[] args) throws Exception { String clientSentence; String sentence;
ServerSocket welcomeSocket = new ServerSocket(6787);
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
BufferedReader serverInput = new BufferedReader(new InputStreamReader(System.in));
sentence = serverInput.readLine();
outToClient.writeBytes(sentence + '\n');
while((clientSentence = inFromClient.readLine()) != null) {
if(clientSentence.equals("exit"))
break;
System.out.println("FROM CLIENT: " + clientSentence);
if (serverInput != null) {
sentence = serverInput.readLine();
outToClient.writeBytes(sentence + '\n');
if (sentence.equals("exit"))
break;
}
}
welcomeSocket.close();
connectionSocket.close();
inFromClient.close();
outToClient.close();
serverInput.close();
}
}
client:
import java.io.*;
import java.net.*;
public class Client { public static void main(String[] args) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6787); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while((modifiedSentence = inFromServer.readLine()) != null){
if (modifiedSentence.equals("exit"))
break;
System.out.println("FROM SERVER: " + modifiedSentence);
sentence = inFromUser.readLine();
if (inFromUser != null) {
outToServer.writeBytes("Typed:"+sentence + '\n');
if(sentence.equals("exit"))
break;
}
}
clientSocket.close();
inFromUser.close();
outToServer.close();
inFromServer.close();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.