'Client and Echo server connected but not client not sending the message to server
A client connects successfully to the server and gets the message "Echo server started.." from the server but when writing to the server nothing happens. Im trying to create an Echo Client program that will connect to the Echo Server (which I assume is successful) now what's left is the communication between the client and the echo server. Any help with to point out the flaw will be appreciated!
Client:
import java.net.*;
import java.util.*;
import java.io.*;
public class GreetinEchoClient {
public static void main(String [] args) throws IOException
{
System.out.println("Echo client ...");
InetAddress localHost = InetAddress.getLocalHost();
Socket socket = new Socket(localHost, 7777);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("We are connected to echo server");
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("Enter message :");
String input = scanner.nextLine();
if("exit".equalsIgnoreCase(input)) {
break;
}
out.println(input);
String response = br.readLine();
System.out.println("Server response :" + response);
} try {
socket.close();
scanner.close();
}catch (IOException e) {}
}
Echo server:
import java.io.*;
import java.net.*;
public class GreetinEcho {
public static void main(String [] args) {
ServerSocket echoServer = null;
String line;
DataInputStream is;
DataOutputStream os;
Socket clientSocket = null;
try {
echoServer = new ServerSocket(7777);
System.out.println("Echo Server Started....");
}
catch (IOException e) {
System.out.println(e);
}
try {
clientSocket = echoServer.accept();
System.out.println("Connected to the client " + clientSocket.getRemoteSocketAddress());
is = new DataInputStream(clientSocket.getInputStream());
os = new DataOutputStream(clientSocket.getOutputStream());
while(true) {
line = is.readUTF();
System.out.println("On Server :" + line);
os.writeUTF(line);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
