I have written a simple client server program. I am able to send text from client to server and vice-versa. But after printing the Server's data on the client, I am not able to print anything on the Client's CLI.
I am not sure what I am doing wrong. I suspect bug in the lines commented "Bug Here". I am unable to find what is wrong in those lines.
Please find my code below.
ATMClient.java
import java.io.*;
import java.net.*;
public class ATMClient
{
public static void main(String args[])
{
try
{
Socket sock = new Socket("localhost", 9010);
sock.setSoTimeout(10000);
System.out.println("Connection established");
String data = null;
InputStreamReader input = new InputStreamReader(sock.getInputStream());
BufferedReader bread = new BufferedReader(input);
BufferedReader brCli = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter text to be sent to Server: ");
String strCli = brCli.readLine();
PrintWriter pwrite = new PrintWriter(sock.getOutputStream(), true);
pwrite.println(strCli);
// Bug Here: The control in not coming out of the while loop
while((data = bread.readLine()) != null)
{
System.out.println(data);
}
// Bug Here: The following line does not get printed.
System.out.print("Enter an Option: ");
pwrite.close();
bread.close();
input.close();
}
catch(IOException ex)
{
System.err.println(ex);
}
}
}
ATMServer.java
import java.io.*;
import java.net.*;
public class ATMServer
{
public static void main(String args[])
{
try
{
ServerSocket server = new ServerSocket(9010);
Socket client = server.accept();
System.out.println("Connection Established");
InputStream input = client.getInputStream();
BufferedReader bufread = new BufferedReader(new InputStreamReader(input));
PrintWriter pwrite = new PrintWriter(client.getOutputStream(), true);
pwrite.println("1. Deposit");
pwrite.println("2. Withdrawal");
pwrite.println("3. Balance");
pwrite.println("4. Exit");
String data = null;
while((data = bufread.readLine()) != null)
{
System.out.println(data);
}
pwrite.close();
bufread.close();
input.close();
server.close();
client.close();
}
catch(Exception ex)
{
System.err.println(ex);
}
}
}
Output:
user1$ java ATMServer
Connection Established
hello
user1$ java ATMClient
Connection established
Enter text to be sent to Server: hello
1. Deposit
2. Withdrawal
3. Balance
4. Exit
Deposit
^C user1$
Can you help me in figuring out how to get an input on Client's console ? Thanks.
As long as the input stream from the remote socket is open,
bread.readLine() will never return null,
and therefore this loop will never end:
while((data = bread.readLine()) != null)
{
System.out.println(data);
}
You need to add some kind of signal, for example the text "ENDMSG". The server should send this when it's done talking to the client, and the client should recognize it as such, and exit from the loop. For example:
while((data = bread.readLine()) != null)
{
if (data.equals("ENDMSG")) {
break;
}
System.out.println(data);
}
Related
I have a Server and Client in my little demo program, where I send some string data from the Client to the Server, and after that resending this data for the Client, which also writes it out to the console. I was confused by PrtintWriter's flush method, which is - according to the JAVA documentation, - flushes the stream. After some researching I'm getting familiar with the concept of autoflash: when the autoFlash parameter is true println, printf, or format methods will flush the output buffer. The only thing what I don't understand here is why should I use the PrintWriter's flush method in the loop and not after the loop. (In my case I use PrintWriter in the Server side.) Autoflash does the same because the println() method is in the loop too. When I use flush after the loop my string data does not appear on the console. Thank you for your guidance and help in advance!
The Client:
public class ClientDemo {
public static void main(String[] args) throws IOException {
final String CLIENTNAME = "<CLIENT>:";
final String SERVERADDRESS = "localhost";
final int PORT = 12312;
try {
Socket socket = new Socket(SERVERADDRESS, PORT);
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(socket.getInputStream());
System.out.println(CLIENTNAME + "Client starts");
List<String> lista = getList();
for(String userInput : lista){
out.println(userInput);
System.out.println("echo: " + scanner.nextLine());
}
} catch(IOException e) {
System.out.println(CLIENTNAME + "Error connecting to the server:" + e.getMessage());
}
}
private static List<String> getList(){
List<String> result = new ArrayList<>();
result.add("egy");
result.add("ketto");
result.add("harom");
result.add("negy");
result.add("ot");
result.add("hat");
result.add("het");
result.add("nyolc");
result.add("kilenc");
result.add("tiz");
return result;
}
}
The Server:
public class ServerDemo {
public static void main(String args[]) throws IOException {
final int PORT = 12312;
final String SERVERNAME ="<SERVER>:";
try {
ServerSocket serverSocket = new ServerSocket(PORT);
Socket socket = serverSocket.accept();
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
Scanner scanner = new Scanner(socket.getInputStream());
System.out.println(SERVERNAME + "Server starts...");
String inputLine;
while(scanner.hasNext()) {
inputLine = scanner.nextLine();
printWriter.println(inputLine);
printWriter.flush();
}
} catch (IOException e) {
System.out.println(SERVERNAME + "Error handleing client...");
}
}
}
You don't have to call flush after writing every line. You are blocking the I/O by doing that. By calling flush you are ensuring that every line you are writing to the socket is actually written and not just buffered (to be written later). Buffering improves the I/O performance. But here it seems that for some reasons, you are not leveraging the advantages the buffering gives. You are blocking until the write is completely done.
I created an simple server/client application,but I could not use this one with two parameters,so I want to add two numbers on server,Two numbers sent to server as parameter.
But When I sent two parameters to server,the client and server begun to wait for anything and nothing happened,as if running both of them without results.
Server
class Server3 public static void main(String[] args)
{
try
(
ServerSocket server=new ServerSocket(12345);
Socket client=server.accept();
PrintWriter output=new PrintWriter(client.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
)
{
int result=0;
String input;
while((input=in.readLine())!=null)
{
System.out.println(input);
result+=Integer.parseInt(input)*3;
//output.println("The result to client is "+result);
//output.flush();
}
output.println("The result to client is "+result);
output.flush();
}
}
}
Client
import java.net.*;
import java.io.*;
import java.util.*;
class Client3
{
public static void main(String[] args) throws Exception
try
(
Socket toServer=new Socket("localhost",12345);
PrintWriter output=new PrintWriter(toServer.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(toServer.getInputStream()));
)
{
String temp,input;
for(int i=0;i<args.length;++i)
{
output.println(args[i]);
output.flush();
}
while((input=in.readLine())!=null);
{
input=in.readLine();
}
System.out.println(input);
}
}
}
Do you hane any idea?
I tried a lot of things?
But never Can I use more numbers,I can use only without while condition.
You have to use a protocol to communicate between client and server. It's just like a way to talk about or you can say some rules to be followed while talking.
Sample code: (Read inline comments)
Here I used DONE as a token to tell the server that client has sent all the numbers and now I am waiting for the result.
Server:
Break the loop once all the numbers are received and sent the result back to client.
public class Server {
public static void main(String[] args) throws IOException {
try (ServerSocket server = new ServerSocket(12345);
Socket client = server.accept();
// flush automatically
PrintWriter output = new PrintWriter(client.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(
client.getInputStream()));) {
int result = 0;
String input;
while ((input = in.readLine()) != null) {
System.out.println(input);
if (input.equals("DONE")) { // Server received token DONE
break; // break the loop
}
result += Integer.parseInt(input) * 3;
}
// sent the result back to client
output.println("The result to client is " + result);
// output.flush();
// no need to call flush here
// because you have already set it to flush automatically
}
}
}
Client:
You don't need to put a while((input=in.readLine())!=null); to wait for received result.
public class Client {
public static void main(String[] a) throws Exception {
try (Socket toServer = new Socket("localhost", 12345);
// flush automatically
PrintWriter output = new PrintWriter(toServer.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(
toServer.getInputStream()));) {
for (int i = 0; i < args.length; ++i) {
output.println(args[i]); // sent all the numbers
// output.flush();
// no need to call flush here
// because you have already set it to flush automatically
}
output.println("DONE"); // Client sent token DONE
String input = in.readLine(); // read the result
System.out.println(input);
}
}
}
output: (server)
1
2
DONE
output: (client)
The result to client is 9
The following is a simple socket-level program. Once a connection is established, the server speaks for as long as he/she wants provided that the message does not end in a period - then the client can speak for as long as he/she wants provided that the conversation does not end in a period - the conversation alternates like this until someone shuts the program down --
I can't get the until there is a period part down ...
Else, I would not have a problem - there would be a one-one interaction
Once one person writes, it stays their turn forever ...
import java.io.*;
import java.net.*;
public class ChatterServer {
final static int SERVER_PORT = 3333;
public static void main(String [] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
System.err.println("Waiting for a client");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection requested from: " + clientSocket.getLocalAddress());
PrintStream toClient = new PrintStream(clientSocket.getOutputStream(), true);
BufferedReader fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
toClient.println("Whatcha want?");
String incoming = fromClient.readLine();
while(incoming != null) {
System.out.println(incoming);
System.out.print("Your turn>");
String myReply="";
//this part does not work
while ( myReply.substring( myReply.length() ) .equals(".") == false){
myReply = keyboard.readLine();
toClient.println(myReply);
}
incoming = fromClient.readLine();
}
}
}
import java.io.*;
import java.net.*;
public class ChatterClient {
final static int SERVER_PORT = 3333;
public static void main(String [] args) throws Exception {
Socket serverSocket = new Socket(args[0], SERVER_PORT);
PrintStream toServer =
new PrintStream(serverSocket.getOutputStream(), true);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String incoming = fromServer.readLine();
while(incoming != null) {
System.out.println(incoming);
System.out.print("Your turn>");
String myReply="";
while ( myReply.substring( myReply.length() ) .equals(".") == false){
myReply = keyboard.readLine();
toServer.println(myReply);
}//end while
incoming = fromServer.readLine();
}//end while
}//end main
}//end ChatterClient class
Better would be to use the endsWith method. It will work just fine, and is cleaner to look at.
while (!myReply.endsWith(".")){...}
While I agree with using String.endsWith the actual problem in the code is that someString.substring(someString.length()) will always be an empty string. You wanted someString.substring(someString.length()-1).
I have implemented a simple chat program in Java . However when I run the code and try to send messages from client I get this as output on Server side
For example:
Client : hi
Server: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=2000]
I get such a response from the client for any message I send..I am basically working on localhost
Can anyone help solve my problem?
My java code :
class Client
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the key value");
int key=Integer.parseInt(br.readLine());
int random=(int)(Math.random()*50);
System.out.println(random);
int response=((int)random)%(key);
System.out.println(key);
System.out.println("response generated is "+response);
System.out.println("Authentication begins");
Socket echoSocket = new Socket("127.0.0.1", 2500);
BufferedReader sin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
PrintStream sout=new PrintStream(echoSocket.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = null;
String s;
DataOutputStream clientout=new DataOutputStream(echoSocket.getOutputStream());
clientout.writeInt(random);
clientout.writeInt(key);
clientout.writeInt(response);
System.out.println("client is"+response);
System.out.println("chat is started");
while (true)
{
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Server : "+s+"\n");
if ( s.equalsIgnoreCase("BYE") )
break;
}
echoSocket.close();
sin.close();
sout.close();
stdin.close();
}
}
class Server
{
public static void main(String args[]) throws IOException
{
int random3=(int)(Math.random()*50);
int response2;
int response3;
int random2;
int key2;
ServerSocket s= new ServerSocket(2500);
Socket echoSocket=s.accept();
DataInputStream clientin=new DataInputStream(echoSocket.getInputStream());
BufferedReader cin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
PrintStream cout=new PrintStream(echoSocket.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s1;
random2=clientin.readInt();
key2=clientin.readInt();
response2=clientin.readInt();
System.out.println(key2);
response3=(random2)%(key2);
System.out.println("server is"+response2);
if(response2==response3)
{
System.out.println("client is authenticated..chat starts");
while (true)
{
s1=cin.readLine();
if (s1.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s+"\n");
System.out.print("Server : ");
s1=stdin.readLine();
cout.println(s1);
}
}
s.close();
echoSocket.close();
cin.close();
cout.close();
stdin.close();
}
}
You get the output because you are using the wrong variables. The variable you should print from the server is s1 and not s.
Variable s refers to the socket, that's why you are getting the socket info instead of the client's response
s1=cin.readLine();
if (s1.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s1+"\n"); // note that this should be s1 and not s
As a good practice, you should name your variable clearly so you and others can always read the code. Having s, s1 and so on will just get you confused later as the code gets larger. It's also a good habit to have and make other engineers who are working with you happier :)
Without showing any code, it's next to impossible to tell. Please post some.
However, the address 0.0.0.0 looks suspicious. If you're working off of localhost, try 127.0.0.1.
I have one problem, I learn how work with socket and I write programm with next code :
import java.net.*;
import java.io.*;
import java.util.*;
public class Client extends Thread{
public static void main(String args[])
{
try
{
Socket s = new Socket(args[0],Integer.parseInt(args[1]));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in,"CP866"));
String str;
str=br.readLine();
str = str+"\n"+s.getInetAddress().getHostAddress()
+":"+s.getLocalPort();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
pw.println(str);
pw.flush();
while(true){
str=null;
if((str=br.readLine())==null)
break;
str = str+"\n"+s.getInetAddress().getHostAddress()
+":"+s.getLocalPort();
System.out.println(str);
pw.print(str);
pw.flush();
}
}
catch(Exception e)
{System.out.println("init error: "+e);}
}
}
Programm of server-side just print obtained message, first message normal print, but next message print in format :, when I cut 25-26 line - message not sent or not obtained.
Why?
You wrote print(str);. So you don't print a newline(\n). Your server-side uses also a BufferedReader (I supose). And he is reading until a newline.
So change to println(str);