I am trying to make a simple server/client application to get my head around using sockets. I can successfully get single line communication between server and client, for example client sends message to server, server acknowledges and sends single message back.
The problem is when I try to read the incoming replies from the server back to the client when there are multiple lines in the BufferedReader.
Examples...
Server and client giving one line to each other.
import java.io.*;
import java.net.*;
import java.awt.*;
class Server {
public static void main(String args[])throws IOException
{
Server myServ = new Server();
myServ.run();
}//end main
public void run() throws IOException
{
ServerSocket serSKT = new ServerSocket(729);
Socket socket = serSKT.accept();
BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter PW = new PrintWriter(socket.getOutputStream());
String fromClient = BR.readLine();
System.out.println(fromClient);
if(fromClient !=null)
{PW.println("Server giving response to client");PW.flush();}
}}//end class server
Client:
import java.io.*;
import java.net.*;
class Client
{
public static void main(String args[])throws IOException
{
Client myCli = new Client();
myCli.run();
}
public void run() throws IOException
{
int port = 729;
Socket mySkt = new Socket("localhost",port);
PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true);
BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream()));
myPW.println("Message from client to server");
String temp=myBR.readLine();
System.out.println(temp);
}}
So the above works fine..now when I try to use for loops to print 10 lines of text to the server and then 10 lines back from the server to the client, I run in to problems. For example..Here is the server and client..
import java.io.*;
import java.net.*;
import java.awt.*;
class Server {
public static void main(String args[])throws IOException
{
Server myServ = new Server();
myServ.run();
}//end main
public void run() throws IOException
{
ServerSocket serSKT = new ServerSocket(729);
Socket socket = serSKT.accept();
BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter PW = new PrintWriter(socket.getOutputStream());
String fromClient = null;
while((fromClient = BR.readLine())!=null)
{
System.out.println(fromClient);//this is being printed successfully
}
if(fromClient !=null)
{
for(int i=0;i<10;i++){
PW.println("Server giving response to client"+i);}
PW.flush();}
}}//end class server
Client:
import java.io.*;
import java.net.*;
class Client
{
public static void main(String args[])throws IOException
{
Client myCli = new Client();
myCli.run();
}
public void run() throws IOException
{
int port = 729;
Socket mySkt = new Socket("localhost",port);
PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true);
BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream()));
for(int i =0;i<10;i++)
myPW.println("Message from client to server"+i);
String temp=null;
while((temp=myBR.readLine())!=null){
System.out.println(temp);}
}}
The above will print the message 10 times to the server and the server will display what the client has sent it successfully:
while((fromClient = BR.readLine())!=null)
{
System.out.println(fromClient);//this is being printed successfully
}
What I do not understand is why the client will not get the message "Server giving response to client" and print it to the console:
String temp=null;
while((temp=myBR.readLine())!=null){
System.out.println(temp);} //should be printing "Server giving repsonse to client"
Sorry it is long, thanks but I am trying with no success!
****EDIT***
Incase anyone else comes across this problem, This was solved by printing a command to the printwriter in the client class to allow the server to know it has finished. e.g:
for(int i =0;i<10;i++){
myPW.println("Message from client to server"+i);}
myPW.println("Finished");
Now in the server class the text being sent to it should be checked for the "Finished" command, if so, break out of the while loop and start sending back to the client, I believe that the only way BufferedReader will be null is if it is closed, so it is infinitely looping. Thanks for the help everyone.
while((fromClient = BR.readLine())!=null)
{
if(fromClient.equals("Finished"))
{
break;
}
System.out.println(fromClient);
}
It looks like you never exit the while() loop in the server, after the 10th line, the server waits for another line (and another, and another, etc.).
The client has to notify the server that he has finished sending, e.g. by sending a special line.
Then you should exit the while loop on the server and start sending.
I think the issue is with:
while((fromClient = BR.readLine())!=null)
{
System.out.println(fromClient);//this is being printed successfully
}
BR.readLine() will not return null until the stream ends (eg is closed/disconnected). You shouldn't use this as your break condition, it will wait inside readLine() for more data to appear on the stream.
You are waiting for the client to close the stream i.e. when br.readLine() == null before sending anything. After this happens fromClient will be null in which case you send nothing anyway
I suggest you try
for (String s; (s = BR.readLine())!=null;) {
System.out.println(s);
PW.println("Server response to " + s);
}
Related
I'm practising WebSocket in java and wrote two simple programs which one of them is a server that starts listening in port 9090 and receives a string as input from the client then makes a string uppercase and returns back to the client.
The program has no error but for some reason, it does not work. I can't find out what is the problem.
After debuting it seems like the server doesn't receive the inputs string even though both sides connect to each other successfully. When the user inputs his/her string the client sends it to the server but the server doesn't receive it, therefore, both programs go to the wanting stage and the application will not respond.
could you please help me with the issue?
Thanks in advance
server-side code :
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws IOException {
// Start listening at port 9090
ServerSocket s = new ServerSocket(9090);
System.out.println("Started: " + s);
Socket socket = s.accept();
System.out.println("conecction accepted by " + socket);
try {
// Buffer reader to get input and save it as "in"
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()
)
);
// write the output as "out"
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()
)
));
// A Loop for waiting to receive the input from the client
while (true) {
System.out.println("Watting for inpute line ...");
String line = in.readLine();
// print the input string to console to make sure it recive the input
System.out.println("inputed Line: " + line);
// sent back upper case string to client
out.println(line.toUpperCase());
out.flush();
}
}finally {
System.out.println("closing...");
socket.close();
}
}
}
The cline side code:
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
// get the Address for localhost
InetAddress addr = InetAddress.getByName(null);
//Connect to the server on pprt 9090
Socket s = new Socket(addr,9090);
// read and write to server using buffer reader and printwriter
try{
BufferedReader in = new BufferedReader(
new InputStreamReader(
s.getInputStream()
)
);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(s.getOutputStream())
)
);
//Get the Ipute string from user
Scanner input = new Scanner(System.in);
while (true){
System.out.println("Enter your text:");
String line = input.nextLine();
if(line.equals("quit"))
break;
// Sent the input string to the server using 'out'
out.println(line);
// Recive the upper case string from server
String response = in.readLine();
System.out.println("Echo:... " + response);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
System.out.println("closing...");
s.close();
}
}
}
The server side is waiting to read a line from the client, it should work if you flush the PrintWriter on the client side:
// Sent the input string to the server using 'out'
out.println(line);
out.flush();
the first is my client, and second is server side.why the client can't send its second round msg to the server through socket? when you put something in the console, the server will respond through socket, and then send back the msg to client.but when i put something in the console for the second time, the msg cannot be sent to server anymore, please tell me why. thanks
package client;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
static Socket sock ;
static InputStreamReader IR;
public static void main(String[] args) throws IOException,
InterruptedException
{
Client client = new Client();
sock = new Socket("localhost", 1112);
IR = new InputStreamReader(sock.getInputStream());
client.run();
}
public void run() throws IOException, InterruptedException
{
while(true)
{
PrintStream PS = new PrintStream(sock.getOutputStream());
Scanner sc = new Scanner(System.in);
System.out.println("Enter your age:");
int age = sc.nextInt();
PS.println(age);
if(age == 0)
{
break;
}
System.out.println("here");
BufferedReader BR = new BufferedReader(IR);
String MSG = BR.readLine();
System.out.println("client: server has received "+MSG);
Thread.sleep(1000);
}
sock.close();
}
}
package server;
import java.io.*;
import java.net.*;
public class Server {
static ServerSocket serverSocket;
public static void main(String[] args) throws IOException,
InterruptedException {
Server server = new Server();
serverSocket = new ServerSocket(1112);
server.run();
}
public void run() throws IOException, InterruptedException
{
while(true)
{
//ServerSocket serverSocket = new ServerSocket(1112);
Socket socket = serverSocket.accept();
InputStreamReader IR = new
InputStreamReader(socket.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String msg = BR.readLine();
System.out.println("server: I have received "+msg);
Thread.sleep(2000);
if(msg != null)
{
PrintStream PS = new
PrintStream(socket.getOutputStream());
PS.println(msg);
}else
{
break;
}
}
serverSocket.close();
}
}
So when a client socket attempts to connect to a server socket, it gets put on a queue on the server-side. Then, the .accept() method on the server socket takes that request off the queue and "connects" the two sockets.
So, in your code,
the client requests connection,
the server accepts it,
client sends age to server,
server reads it and sends it back,
client sends another age to the server
server never does anything
The problem occurs after step 4 on the server-side. It calls the .accept() method again, but there is no client requesting connection so that .accept() just waits until it gets a socket requesting connection (it's called a blocking method).
You could fix this problem by moving the Socket socket = serverSocket.accept() out of the while(true) loop on the server
I recently programmed a simple Java server, and a client to test the server. The server is supposed to receive messages from the client, and send a random substring of the message back. The problem is this: When I send the message using the client program, the server does not respond. Then, when I kill the client program, the server leaps into action, and attempts to send the data back to the client. The server reads the data correctly but starts processing it only when I stop the client program.
This is the client code:
import java.io.*;
import java.net.*;
class ServerTest{
public static void main(String args[]) throws Exception{
Socket clientSocket = new Socket(myIpAdress, 8001);
//Send the message to the server
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String sendMessage = "randSubstring:StackOverflowIsAwsome";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent: "+sendMessage);
String message = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())).readLine();
System.out.println("Message received from the server : " +message);
clientSocket.close();
}
}
My server code consists of two classes. This one is the listener:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerListener {
public static void main(String args[]) throws Exception {
String clientSentence;
ServerSocket socket = new ServerSocket(8001);
while(true) {
Socket connectionSocket = socket.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
//DataOutputStream output = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = input.readLine();
if(clientSentence.startsWith("randSubstring:")){
Thread connection = new Thread(new ServerConnection(connectionSocket, clientSentence));
connection.start();
}
Thread.sleep(300);
}
}
}
This is the thread that will not start until the client is stopped:
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Random;
public class ServerConnection implements Runnable{
private Socket serverConnection;
private String sentence;
public ServerConnection(Socket connection, String clientSentence){
serverConnection = connection;
sentence = clientSentence;
}
#Override
public void run() {
Random r = new Random();
String substring = sentence.substring(0, r.nextInt(sentence.length()));
try {
OutputStream os = serverConnection.getOutputStream();
OutputStream out = new BufferedOutputStream(os);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
bw.write(substring);
bw.close();
out.close();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I am using a Macintosh with Yosemite. Is this happening because I am trying to run the programs on the same computer, or would the problem occur if the programs were run from different computers? Any help is appreciated.
In the server you do a readLine(..), which means that it will wait for a end-of-line character.
But in your sender code, you just send a string with no line ending.
So either you make sure you also send a end of line char or your server wait's for something else as "delimiter"
You're reading a line but you aren't writing a line. Add a line terminator to the sent message. Otherwise readLine() won't return until the peer closes the connection.
NB The I/O in the try block after the accept should be in the Runnable, not where it is. Don't do I/O in the accept loop.
so I am having an issue with reading input from a client. It works completely fine whenever I am using my if statements without the while statements wrapped around it in the server class. Could anybody point me to why this may be failing?
Server class:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception
{
Server myServer = new Server();
myServer.run();
}
public void run() throws Exception
{
//Initializes the port the serverSocket will be on
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("The Server is waiting for a client on port 9999");
//Accepts the connection for the client socket
Socket socket = serverSocket.accept();
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message = br.readLine();
//Confirms that the message was received
System.out.println(message);
//When this while is here. The match fails and it goes to the else statement.
//Without the while statement it will work and print "Received our hello message."
//when the client says HELLO.
while(message != null)
{
if(message.equals("HELLO"))
{
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("Received our hello message.");
}
else
{
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("Did not receive your hello message");
}
}
}
}
Client class:
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws Exception
{
Client myClient = new Client();
myClient.run();
}
public void run() throws Exception
{
Socket clientSocket = new Socket("localhost", 9999);
//Sends message to the server
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
Scanner scan = new Scanner(System.in);
String cMessage = scan.nextLine();
ps.println(cMessage);
//Reads and displays response from server
InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message = br.readLine();
System.out.println(message);
}
}
You're never modifying message inside the while loop so you have an infinite loop.
Try
while((message = br.readLine()) != null)
You only loops at the Server side, while u forgot to loop at the Client side, I did a quick fix for you, and also help you closed your connections.
Server.java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception
{
Server myServer = new Server();
myServer.run();
}
public void run() throws Exception
{
//Initializes the port the serverSocket will be on
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("The Server is waiting for a client on port 9999");
//Accepts the connection for the client socket
Socket socket = serverSocket.accept();
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String message;
//= br.readLine();
//Confirms that the message was received
//When this while is here. The match fails and it goes to the else statement.
//Without the while statement it will work and print "Received our hello message."
//when the client says HELLO.
PrintStream ps = new PrintStream(socket.getOutputStream());
while((message =br.readLine())!=null)
{
System.out.println(message);
if(message.equals("HELLO"))
{
ps.println("Received our hello message.");
}
if(message.equals("END"))
{
ps.println("Client ended the connection");
break;
}
else
{
ps.println("Did not receive your hello message");
}
}
ps.close();
br.close();
ir.close();
serverSocket.close();
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String[] args) throws Exception
{
Client myClient = new Client();
myClient.run();
}
public void run() throws Exception
{
Socket clientSocket = new Socket("localhost", 9999);
//Sends message to the server
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
Scanner scan = new Scanner(System.in);
String cMessage ="";
InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
BufferedReader br = new BufferedReader(ir);
while(!(cMessage.trim().equals("END"))){
cMessage = scan.nextLine();
ps.println(cMessage);
//Reads and displays response from server
String message = br.readLine().trim();
System.out.println(message);
}
br.close();
ir.close();
scan.close();
ps.close();
clientSocket.close();
}
}
Your code is working just fine for sending one 'HELLO' message.
However, like ^Tyler pointed out, If you want to keep sending messages you need to move 'while((message = br.readLine()) != null)' in the while loop.
Using a loop like you are...
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
sleep(in);
if(!in.ready()){
break;
}
}
There is a cheater way that I figured out.
private static void sleep(BufferedReader in) throws IOException {
long time = System.currentTimeMillis();
while(System.currentTimeMillis()-time < 1000){
if(in.ready()){
break;
}
}
}
This might be sloppy, but if you make a sleep method that waits an amount of time and just keep checking if the BufferedReader is "ready." If it is, you can break out, but then when you come out check again. -- Maybe you could just return a boolean instead of checking twice, but the concept is there.
I'm new to the network communication and I'm trying to build client-server application.
protected void init(){
Server myServer = new Server();
Client myClient = new Client();
}
That's my Client class:
public class Client {
public Client() {
init();
}
private void init() {
Socket echoSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
DataInputStream stdIn = new DataInputStream(System.in);
try {
echoSocket = new Socket("localhost", 1234);
os = new DataOutputStream(echoSocket.getOutputStream());
is = new DataInputStream(echoSocket.getInputStream());
os.writeInt(stdIn.readInt());
echoSocket.getOutputStream().close();
echoSocket.getInputStream().close();
echoSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
And that's server:
public class Server {
public Server() {
init();
}
private void init() {
try {
boolean run = true;
ServerSocket ss = new ServerSocket(1234);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readInt());
s.getInputStream().close();
s.getOutputStream().close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
First of all:
Can I initialize client and server simply like i did? new Server() and new Client()?
Question 2:
Is it important what i initialize at first? client or server?
Question 3:
When i compile this code with client first initialized, i become Connection refused: connect. I know it means that there is no listening socket running on the port you are trying to connect to. That's why server must go first, i think. Is it so? can i fix it using setSoTimeout and how?
Question 4:
When i compile it with server and then client, output is nothing. And i think it has nothing to do with client, because if i try to print "1", for example, it doesn't work either. I think it just waits for the client and does nothing that goes after. How can i fix this? maybe setSoTimeout goes here too?
You can't have both client and server in the same thread.
As you already have observed, the server accepts the connection and tries to read something. It doesn't know that the client is running in the very same thread.
Either make a multi-threaded application, where client and server have their own thread. Or make two prgrams that run independently of each other. The latter would be also the "normal case".
Make two different projects, first run server than client.
Server will write on console "Server started" than run client it will ask your name, type your name press ok . Your name will be sent to server and server will reply saying hello to you.
Here is server code
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Server {
public static void main(String[] args) {
try{
ServerSocket ss= new ServerSocket(2224);
System.out.println("Serever started");
while(true)
{
Socket s=ss.accept();
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
OutputStream os=s.getOutputStream();
PrintWriter pw=new PrintWriter(os);
String name=br.readLine();
String message="Hello "+name+"from server";
pw.println(message);
pw.flush();
}
}
catch(Exception exp)
{
System.out.println("Excepttion occured");
}
}
}
Here is client code
import java.net.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket s=new Socket("localhost",2224);
InputStream is=s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
OutputStream os=s.getOutputStream();
PrintWriter pw=new PrintWriter(os,true);
String message = JOptionPane.showInputDialog("Give your name");
pw.println(message);
pw.flush();
String servermessage = br.readLine();
JOptionPane.showMessageDialog(null, servermessage);
s.close();
}
}