Java, Socket and transfer text - java

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);

Related

How to get input from Console in Java Client Server Program

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);
}

Not working Socket java software

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

Client not able to send messages to server in java chat program

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.

Java networking Not Working

Im trying to solve an exercise on JAVA IO . The problem i get is that the messages are not coming on the correct order. For example this is what happens:
Run server
Run Client
Type Password
Two
Three
Wrong password
One
Wrong password
Trial
Waiting for sentence
T
TRIAL
Your IP address is :/
Your Socket is : 4351
Current date is : 2011/05/18 15:45:13.
So for some reason the messeges are not on right order.
This is the code:
import java.io.*;
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class TCPServer {
public TCPServer() {
}
public static void main(String args[])
throws Exception {
String clientSentence;
String capitalisedSentence;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
ServerSocket welcomeSocket = new ServerSocket(4351);
while (true) {
Socket clientSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(clientSocket.getOutputStream());
boolean correctPass = false;
while (!correctPass) {
if (getPassword(clientSocket,inFromClient,outToClient)) {
correctPass = true;
}
}
outToClient.writeBytes("Waiting for sentence"+"\n");
clientSentence = inFromClient.readLine();
capitalisedSentence = clientSentence.toUpperCase();
outToClient.writeBytes(capitalisedSentence + "\n"
+ "Your IP address is :" + clientSocket.getInetAddress() + "\n"
+ "Your Socket is : " + clientSocket.getLocalPort() + "\n"
+ "Current date is : " + dateFormat.format(date) + "\n");
}
}
private static boolean getPassword(Socket clientSocket,BufferedReader inFromClient,DataOutputStream outToClient) throws Exception {
boolean passed = false;
outToClient.writeBytes("Type password." + "\n");
while (!passed) {
String password = inFromClient.readLine();
if (password.equals("1")) {
passed = true;
} else {
outToClient.writeBytes("Wrong Password" + "\n");
}
}
return true;
}
}
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String args[])
throws Exception {
String sentence;
String modifiedSentence = "";
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 4351);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
do{
while(inFromServer.ready()){
System.out.println(inFromServer.readLine());
}
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + "\n");
}while(true);
// close the socket and the connection
}
}
Note that the messages aren't so much in the wrong order, as that you are one behind on the replies from the server. I think your problem is just that you send a message, and then immediately go back to the top of the loop and check if there is an incoming message. If it is not there immediately, you ask for another input. But what if the server does not reply in between the time when you send the message and go back to the top of the loop? This will happen in a nanosecond.
So you're asking a question, and then if you don't get an immediate reply, you're promptly asking another question. Then you display the reply to the first question and ask the third question. Etc. You need to be a little more patient. It's been a while since I've done socket programming, but I think you really need to wait for a reply, don't just say that if there is no reply, barrel ahead.
the problem is you are using a DataOutputStream for output and a Reader for input -- instead try using a PrintWriter (which offers a println!) for output and the Reader you currently use for reading. It's not a good idea to mix a Stream and a Reader/Writer!

java program Inetaddress

The question I have been asked is too
write java program that reads IP address from input file and writes the corresponding host names in the output file and vice versa.
here is my code:
import java.net.*;
import java.io.*;
public class hw
{
public static void main(String args[])
{
try{
FileReader f= new FileReader("w.txt");
BufferedReader r = new BufferedReader(f);
FileWriter o = new FileWriter("out.txt");
PrintWriter p = new PrintWriter(o);
String line = r.readLine();
String hn=line;
String IP;
InetAddress d=InetAddress.getByName(hn);
while(line !=null)
{
hn=d.getByName(line);
p.println(hn);
IP=d.getHostName();
p.println(IP);
}
r.close();
p.close();
}
catch(FileNotFoundException e )
{System.out.println("file not found");}
catch(IOException e)
{System.out.println("io error "+e.getMessage());}
}//main
}//class
I guess your while loop never terminates. Usually I read in a loop like this:
while ((line = r.readLine()) != null) {
// process line, i.e.
InetAddress ia = InetAddress.getByName(line.trim());
// etc.
}
Also you might consider putting your close statements into the finally block for good form.
kevin corrected your loop error , as for your second question
I suggest you read this tutorial about reading and writing files Using stream io

Categories