I have a very simple capitalization Java program. Client sends text read from standard input to server which converts that text into capital letters. Program works well but once client is stopped (NetBeans ide used), server is also reset. Server socket should keep listening for new connection from clients regardless of a client being stopped.
public class Client
{
public static void main(String[] args) throws IOException
{
try(Socket s=new Socket("localhost",9090))
{
while(true)
{
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader rd=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader from=new BufferedReader(new InputStreamReader(System.in));
String read=from.readLine();
out.println(read);
String answer;
answer=rd.readLine();
System.out.println(answer);
}
}
}
}
public class Server
{
public static void main(String[] args) throws IOException
{
try(ServerSocket listener = new ServerSocket(9090);
Socket socket = listener.accept();)
{
while (true)
{
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Started...");
String transform=br.readLine();
String newStr=transform.toUpperCase();
out.println(newStr);
}
}
}
}
What happens is a normal behaviour. You server code only includes handling of a single client. The listener.accept() function only accepts latest connection to server. You need to put the listened.accept() in loop and handle all the exceptions that are raised within. The server-side code should look like this:
public class Server
{
public static void main(String[] args) throws IOException
{
try(ServerSocket listener = new ServerSocket(9090);
while (true) {
try {
Socket socket = listener.accept();)
…
} catch (SocketException ex) {
...
}
}
}
}
But keep in mind that this code will only handle single client at a time. No multi-threading in this code.
Your Server is closing the connection and then finishing becuase it is created outside the while loop. I believe this is what you need.
public class Server {
public static void main (final String[] args)
throws IOException {
while (true) {
try (ServerSocket listener = new ServerSocket(9090); Socket socket = listener.accept();) {
while (true) {
final BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Started...");
final String transform = br.readLine();
if (transform == null)
break;
final String newStr = transform.toUpperCase();
out.println(newStr);
}
}
}
}
}
Related
I'm beginning with sockets for my very first time.
I'm trying to set up a server that its only function is to give back to the client a string i send.
The client doesn't send the string, and the server doesn't receive it. Here it goes the code:
Server code:
package servidor;
//All imports needed
public class Servidor {
public static void main (String[] args){
try{
ServerSocket socketServidor = new ServerSocket(2000);
Socket socketDatos = socketServidor.accept();
PrintWriter escribirCliente = new PrintWriter(socketDatos.getOutputStream(),true);
BufferedReader leerCliente = new BufferedReader(new InputStreamReader(socketDatos.getInputStream()));
String linea;
while((linea = leerCliente.readLine())!= null){
System.out.println("hola");
System.out.println("Server: "+ linea);
escribirCliente.println(linea);
if(linea.equals("x")){
break;
}
}
socketDatos.close();
escribirCliente.close();
leerCliente.close();
socketServidor.close();
} catch (IOException e){
System.out.println("Fallo en la conexion.");
}
}
}
Client code:
package cliente;
//All imports needed
public class Cliente {
public static void main(String[] args) throws IOException{
Socket socket = new Socket("localhost", 2000);
BufferedReader leerServidor = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter escribirServidor = new PrintWriter(socket.getOutputStream());
BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
String linea = teclado.readLine();
//System.out.println(""+linea);
while (!(linea.equals("x"))){
System.out.println("en while");
escribirServidor.println(linea);
System.out.println("eco: "+ leerServidor.readLine());
linea = teclado.readLine();
}
escribirServidor.close();
leerServidor.close();
teclado.close();
socket.close();
}
}
The thing is:
In the client, the execution never reaches the "eco" but it does reach the "en while". But in the server, that while is never reached. Not even printing the "hola". I supposed that the issue should be when reading the line, but i've been not able to fix it.
Any ideas?
Thank you in advance!
I basically was trying to give a response back from my server if my client sends me the message "Hello mr server". However it doesn't.
Here is my code :
public class Server {
public static final int PORT = 6666;
public static void main(String[] args) throws IOException {
new Server().runServer();
}
public void runServer() throws IOException {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server up and ready for connections.....");
while (true) {
Socket socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); //for fetching data
String str = br.readLine(); //for fetching data
System.out.println("Client Data:" + str + '\n');
String dataSendingToClient = "gg";
OutputStreamWriter os = new OutputStreamWriter(socket.getOutputStream()); //for sending data
PrintWriter out = new PrintWriter(os); //for sending data
out.println(dataSendingToClient);
os.flush();
if (br.equals("hey mr server")){
OutputStreamWriter os2 = new OutputStreamWriter(socket.getOutputStream()); //for sending data
PrintWriter out2 = new PrintWriter(os); //for sending data
out2.println("hey mr client");
os.flush();
}
}
}
}
You are using br.equals("hey mr server") which will always return false, since br is not a String, but a BufferedReader
Instead you should use your already defined str variable str.equals("hey mr server")
Also note that with your current code, your server will only read one message from the client before moving on to the next connection
If you want your server to read more messages from a single client, you will need to loop until the client sends a disconnect message/the socket closes
while(true)
{
Socket socket = serverSocket.accept();
BufferedReader br = [...];
PrintWriter out = [...];
for(String message = br.readLine(); message != null; message = br.readLine())
{
//do stuff
//message is each line from the client
}
}
If you have multiple clients trying to connect, you will need to multithread the connections
I would recommend a class to enclose the connection details and use Stacks to poll if the client sent data
public static void main(String[] args) //or whatever other method you're running in
{
List<Connection> clients = new ArrayList<Connection>(); //some data structure to hold the clients
//start accepting connections
new Thread(new Runnable()
{
public void run()
{
while(true)
Connection client = new Connection(serverSocket.accept());
}
}).start();
//do something with the clients, read/write/whatever
}
private class Connection extends Runnable
{
private BufferedReader reader;
private PrintWriter writer;
private Queue<String> messages;
public Connection(Socket s)
{
reader = [...];
writer = [...];
messages = new LinkedList<String>();
}
public void run()
{
//just keep reading
while(true)
messages.add(reader.readLine();
}
public String read()
{
messages.poll();
}
public void write(String msg)
{
writer.write(msg);
writer.flush();
}
}
Note: take that as rough pseudocode
I have written server and client part, see code:
//this I run on my firt PC
public class TCPServer {
public static void main(String[] args) throws Exception{
TCPServer server = new TCPServer();
server.run();
}
public void run() throws Exception{
ServerSocket socket = new ServerSocket(6789);
Socket SOCK = socket.accept();
BufferedReader BR = new BufferedReader(new InputStreamReader(SOCK.getInputStream()));
String msg = BR.readLine();
System.out.println(msg);
if(msg != null){
PrintStream PS = new PrintStream(SOCK.getOutputStream());
PS.print("MESSAGE RECEIVED");
}
}
}
//This I run on my notebook
public class TCPClient {
public static void main(String args[]) throws Exception{
TCPClient client = new TCPClient();
client.run();
}
public void run() throws Exception{
Socket clientSocket = new Socket("192.168.88.77",6789);
PrintStream PS = new PrintStream(clientSocket.getOutputStream());
PS.print("Hello from GLADIS VLADLEN");
BufferedReader BR = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String msg = BR.readLine();
System.out.println(msg);
}
}
After running this applications nothing happens. And only after I stop Client app message is received by server part. Why this happens? I try to turn off FireWall but this don't help me.
Both the server and client are reading a line using BufferedReader#readLine(). However the PrintStream is sending characters without the line terminator.
Try calling println instead of print.
I wrote a simple socket program but the sever is not printing out anything.
Client:
public class ChatClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 9001);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner reader = new Scanner(System.in);
while (true) {
String test = reader.next();
out.write(test);
System.out.println(test);
//String line = in.readLine();
}
}
}
Server:
public class ServerApplication {
private static final int PORT = 9001;
private static HashSet<String> names = new HashSet<String>();
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
public static void main(String[] args) throws Exception {
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
System.out.println("Server starts!");
new ChatThread(listener.accept()).start();
}
} finally {
}
}
}
public class ChatThread extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ChatThread(Socket socket){
this.socket = socket;
}
#Override
public void run() {
try {
System.out.println("Thread Starts!");
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while(true){
System.out.println("Getting input!");
String input = in.readLine();
System.out.println("test");
if (input!=null){
//out.print(input);
System.out.println(input);}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any ideas how should I fix this so when I type "Hello" on client, server will print out "Hello" using system.out.println()?
This is highly likely the socket buffers not being flushed and the data is not actually sent to the socket; the server is waiting for more data before sending a chunk.
If we look at the docs for PrintWriter:
... if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked ...
However, you are using print, not println, so the flushing is not automagic.
Try manualy flushing you streams after writing data:
out.write(test);
out.flush();
yep, this was an answer to a 3 year old post... question was edited and popped up on my stream
Testing your server using telnet (telnet localhost 9001) revealed that it's working just fine. I assume that the fact that it can only print a line at a time, rather than individual characters, is intended behavior.
I did find one bug. When terminating the telnet connection, the server would go into an infinite loop. I suggest the following change:
if (input!=null){
System.out.println(input);}
}
else {
break;
}
For the client, I was able to fix it simply by replacing write with println:
out.println(test);
I'm using sockets to chat as part of a large application, however so you can read it easier i have made 2 classes that show the problem. I have a server and a client, the server opens a socket and waits for a client to connect once connected it sends them a welcome message. On the client side they display the welcome message then enter a loop to write to the PrintWriter. On the server side it will now enter a loop that constantly displays text from the bufferedReader however nothing is printing out, not sure if i'm being stupid but think it needs a fresh pair of eyes, Thanks.
public class Server {
public static boolean running = true;
public static PrintWriter out;
public static BufferedReader in;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(1932);
while (true) {
Socket cs = s.accept();
out = new PrintWriter(cs.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(cs.getInputStream()));
out.println("Welcome");
out.flush();
while (running == true) {
String input = in.readLine();
if (input.equals("QUIT")) {
System.out.println("theyve gone :( ");
cs.close();
running = false;
} else {
System.out.println(input);
}
}
}
}
}
public class Client {
public static boolean running = true;
public static PrintWriter out;
public static BufferedReader in;
public static Scanner scan;
public static void main (String [] args) throws IOException{
Socket so = new Socket("localhost", 1932);
out = new PrintWriter(so.getOutputStream());
in = new BufferedReader(new InputStreamReader(so.getInputStream()));
System.out.println("TYPE QUIT TO LEAVE ");
System.out.println(in.readLine());
while(true){
scan = new Scanner(System.in);
String message = scan.next();
out.print(message);
out.flush();
}
}
}
In the server, you're reading the next line:
String input = in.readLine();
So the server blocks until the end of line is reached (or the stream is closed).
And on the client, you never send any end of line:
out.print(message);