I am currently learning Java and am playing around with socket programming.
My goal is to implement request-response communication like HTTP. I can get one way communication from the client to the server. But when I program the client to listen for a response it causes the server to not print out the request.
I've googled the issue and most posts mention using the flush() method, which I have tried. Anyone have any thoughts?
Here is the client code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
class Client {
public static void main(String args[]) throws IOException {
// Get the port number from the first command line argument
int port_number;
if(args.length == 0) {
port_number = 80;
} else {
port_number = Integer.parseInt(args[0]);
}
try (
// Create a socket
Socket socket = new Socket("localhost", port_number);
// Input reader
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
// Output writer
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
) {
System.out.println("Sending request");
out.print("Foo");
out.flush();
System.out.println("Sending Complete");
// If this next section is commented out then the server prints the message
StringBuilder message = new StringBuilder();
int data;
while((data = in.read()) != -1) {
message.append((char) data);
}
System.out.print(message);
}
}
}
and here is my server code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String args[]) throws IOException {
// Get the port number from the first command line argument
int portNumber = Integer.parseInt(args[0]);
System.out.println("Listening on port " + portNumber);
ServerSocket serverSocket = new ServerSocket(portNumber);
while (true) {
try (
Socket socket = serverSocket.accept();
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
){
StringBuilder message = new StringBuilder();
int data;
while((data = in.read()) != -1) {
message.append((char) data);
}
System.out.println(message);
out.print("Bar");
}
}
}
}
This is because your sever currently loop until read() returns -1 which never happens in your case so he never reaches the part of the code where he is supposed to send Bar, you need a specific marker to indicate the end of your message so for example here you can send data line by line, for this proceed as next:
Client side:
System.out.println("Sending request");
// Send a line to the server
out.println("Foo");
out.flush();
System.out.println("Sending Complete");
// Read a line sent by the server
System.out.print(in.readLine());
Output:
Sending request
Sending Complete
Bar
Server side:
// Print the line sent by the client
System.out.println(in.readLine());
// Send a message to the client
out.println("Bar");
Output:
Listening on port 9999
Foo
Related
I am learning Java and I'm writing an example client - server application.
The sokcket connection is fine, everything works well until the second message from the client app. It does not reach the server. If I start another client it also succeed at the first message, and fails at the second.
Anyone has an idea? Thanks in advance!
Server code:
package networking;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static Socket socket;
public static void main(String[] args) {
try {
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while (true) {
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String incomingMessage = br.readLine();
System.out.println("((( " + incomingMessage);
String returnMessage = incomingMessage;
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage + "\n");
bw.flush();
System.out.println("))) " + returnMessage);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
}
And the client
package networking;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Client {
private static Socket socket;
public static void main(String args[]) {
try {
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = "";
/*
while(!message.equals("q")) {
System.out.print("Message: ");
message = console.readLine();
bw.write(message + "\n");
bw.flush();
System.out.println("))) " + message);
//Get the return message from the server
String incomingMessage = br.readLine();
System.out.println("((( " + incomingMessage);
}
*/
for (int i = 0; i < 10; i++) {
bw.write(i + "\n");
bw.flush();
System.out.println("))) " + i);
String incomingMessage = br.readLine();
System.out.println("((( " + incomingMessage);
}
} catch (Exception exception) {
exception.printStackTrace();
} finally {
//Closing the socket
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Your while is misplaced in your server code, and in fact you need 2 while loops:
one for accepting new clients
one for manage several messages by client
In pseudo code it gives you:
while (true):
accept new client
while (client connected):
read message from client
write back message to client
close client socket
If you want to use threads, then it's the inner while loop task which you have to delegate to a new thread.
Note: accept is blocking until a new client comes. That why you could send only one message by client.
Your server is not set up to handle this. You are reading one line, then discarding the connection for the GC, without closing it. The server, reading one line, then ignores all other lines and starts listening for the next connection.
Also, consider using threads.
Here's my code:
SERVER:
package server;
public class Main {
public static void main(String args[]) {
new EchoServer(9000);
}
}
+
package server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
private ServerSocket server;
public EchoServer(int port) {
try {
server = new ServerSocket(port);
while (true) {
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(in.readLine() + " | MOD");
socket.close();
}
} catch(Exception err) {
err.printStackTrace();
}
}
}
CLIENT:
package client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Main {
public static void main(String args[]) {
try {
while (true) {
Socket socket = new Socket("localhost", 9000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
out.println(input.readLine());
System.out.println(in.readLine());
socket.close();
}
} catch (Exception err) {
System.out.println(err);
}
}
}
It works all as it should, except that I want when the server sends the "message" + " | MOD" to the client, I want the server to send that to all clients, how can I do that?
I am new to Java, but not to coding so please help me if I've done some wrong stuff that can be done easier or better.
Please help.
Thanks alot.
What you can do is save the client sockets in an array, and then use a for loop to send to each socket.
First, declare your clientSocket array; note that 5 is just an arbitrary size used for testing. Also, declare a counter int.
public Socket clientSocket[] = new Socket[5];
public int intLastSocket = 0;
// this should be placed where you're waiting to accept connections
while (true) {
printTCP("Ready to accept welcome socket");
clientSocket[intLastSocket] = welcomeSocket.accept();
intLastSocket++;
}
// on the server, call this to send. s is a reference to the server object
public void sendToAllTCP(TCPServer s, String message) {
for (Socket z : s.clientSocket) {
if (z != null) {
PrintStream outToClient = null;
try {
outToClient = new PrintStream(z.getOutputStream());
outToClient.println(message);
} catch (IOException e) {
TCPServer.printTCP("Caught an IO exception trying "
+ "to send to TCP connections");
e.printStackTrace();
}
}
}
}
IN YOUR CODE:
package com.murplyx.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
private ServerSocket server;
// use the clientSocket Array to save each new connection
public Socket clientSocket[] = new Socket[5];
public EchoServer(int port) {
try {
server = new ServerSocket(port);
// this should be placed where you're waiting to accept connections
while (true) {
System.out.println("Ready to accept welcome socket");
clientSocket[intLastSocket] = server.accept();
intLastSocket++;
//send your message here, note that putting
//it here will send it each time u add a new connection
sendToAllTCP(/*the message you want to send */);
}
} catch(Exception err) {
err.printStackTrace();
}
}
public void sendToAllTCP(String message) {
// this is an enchanced for loop, i don't know if it's in other languages
// but in java it's supposed to let you loop through
//each object in any iterable list
// such as array, arraylist, linkedlist, etc
for (Socket z : clientSocket) {
if (z != null) {
//basically this chunk of code declares output and input streams
//for each socket in your array of saved sockets
PrintStream outToClient = null;
try {
outToClient = new PrintStream(z.getOutputStream());
outToClient.println(message);
} catch (IOException e) {
System.out.println("Caught an IO exception trying "
+ "to send to TCP connections");
e.printStackTrace();
}
}
}
}
}
Depending on when you want to send your message, you can use the console and sys.in to send it. For example, if you read a line from sys.in and it .equals("sendMsg"), then you can call sendToAllTCP(yourmessage)
You should take a look at multiThreaded chat Server. Each client wich connects gets it's own thread.
Here is the perfect answer to your question:
multithread client-server chat, using sockets
Good luck mate!
I need to read a "bdd.txt" file placed on a Virtual Machine on my computer. I made a client/server system in Java. My Server.java is on my VM (Ubuntu) with a "database" (the bdd.txt file in the same folder), and my Client.java is on my Windows 7.
So far I have split my code into 2 different files (Server/Client) and I made the connexion between my Windows 7 and my VMware Player's Ubuntu. When I start my server on my VM, it listens on a port number x, then I go back on my Windows and run my client. It asks to make the connexion and then, back on my VM, I print a message "The connexion is made" and my app is running. So now I can communicate between them. I have just used socket = new Socket("my VM ip address",portNumber); and it works. But now, I have no idea how to adapt my code to reach my bdd.txt file I moved on my VM.
How can I now read the bdd.txt file, to have access to the pin codes ?
Why is my new Client() never called in my program?
Here is Client.java :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
int pinSize = 0;
//set up server communication
Socket clientSocket = new Socket(InetAddress.getLocalHost(),1234);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
Scanner scanner = new Scanner(System.in);
System.out.println("Enter pin : ");
String password = scanner.next();
pinSize = password.length();
//send PIN to server
out.println(password);
if (pinSize != 4) {
System.out.println("Pin must be 4 digits");
} else {
System.out.println("Checking...");
}
out.flush();
//get response from server
String response = in.readLine();
System.out.println(response);
in.close();
out.close();
clientSocket.close();
}
}
Here is Server.java (in the same folder as bdd.txt):
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static ServerSocket server;
public static void main(String[] args) throws Exception {
server = new ServerSocket(1234);
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
//Listen for client requests:
String request;
while ((request = in.readLine()) != null) {
//check PIN, send result
boolean pinCorrect = checkPin(request);
out.println(pinCorrect ? "yes" : "no");
out.flush();
}
out.close();
in.close();
socket.close();
}
/**
* Check if PIN is in bdd.txt
* #throws IOException
*/
private static boolean checkPin(String pin) throws IOException {
boolean result = false;
File file = new File("bdd.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = in.readLine()) != null) {
result |= (line.equals(pin));
}
in.close();
return result;
}
}
There's a lot of irrelevant stuff in you question, it's hard to see how your program works.
Here's what should happen:
Client side:
User inputs a number
Client sends user's number to server
Client receives response from server, and displays it
Server side:
Server listens for client connection
Server receives number from client
Server checks number against file bbd.txt
If number exists in file, return yes else return no
I have written some simple code to show you, excluding UI stuff:
Client.java:
public static void main(String[] args) {
//set up server communication
Socket clientSocket = new Socket("ip.address",1234);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
//send PIN to server
out.println("9.8.7.6");
out.flush;
//get response from server
String response = in.readLine();
System.out.println(response);
in.close();
out.close();
clientSocket.close();
}
Server.java:
public static void main(String[] args) throws Exception {
//Set up client communication
ServerSocket server = new ServerSocket(1234);
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
//Listen for client requests:
String request;
while ((request = in.readLine()) != null) {
//check PIN, send result
boolean pinCorrect = checkPin(request);
out.println(pinCorrect ? "yes" : "no");
out.flush();
}
out.close();
in.close();
socket.close();
}
/**
* Check if PIN is in bdd.txt
*/
private static boolean checkPin(String pin) {
boolean result = false;
File file = new File("bdd.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = in.readLine()) != null) {
result |= (line.equals(pin));
}
in.close();
return result;
}
I'm new to java socket programming, this program allows TCP server to have a multi-thread that can run concurrently. I try to send the txt file from one client(has another client that will sent file at the same time) to the server side and ask server to send "ok" status message back to client side. But it seems that the server can't receive any file from the client and the strange thing is if i delete the receiveFile() method in my client class, the server is able to recieve the file from client. Can somebody help me?
Server.class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class ConcurrentServer {
public static void main(String args[]) throws IOException
{
int portNumber = 20020;
ServerSocket serverSocket = new ServerSocket(portNumber);
while ( true ) {
new ServerConnection(serverSocket.accept()).start();
}
}
}
class ServerConnection extends Thread
{
Socket clientSocket;
ServerConnection (Socket clientSocket) throws SocketException
{
this.clientSocket = clientSocket;
setPriority(NORM_PRIORITY - 1);
}
public void run()
{
try{
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
OutputStream outToClient = clientSocket.getOutputStream();
PrintWriter printOutPut = new PrintWriter(outToClient,true);
while(inFromClient.ready())
{
String request = inFromClient.readLine();
System.out.println(request);
System.out.println("test");
}
printOutPut.write("HTTP/1.1 200 OK\nConnection: close\n\n");
printOutPut.write("<b> Hello sends from Server");
printOutPut.flush();
printOutPut.close();
clientSocket.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Client.class
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SmallFileClient {
static String file="test.txt";
static PrintWriter outToServer;
static Socket socket;
public static void main(String[] args) throws IOException
{
final int PORT=20020;
String serverHostname = new String("127.0.0.1");
socket = new Socket(serverHostname, PORT);
outToServer = new PrintWriter(socket.getOutputStream(),true);
sendFile();
receiveFile();
outToServer.flush();
outToServer.close();
socket.close();
}
//read file and send file to server
public static void sendFile() throws IOException
{
BufferedReader br=new BufferedReader(new FileReader(file));
try
{
String line = br.readLine();
while(line!=null)
{
//send line to server
outToServer.write(line);
line=br.readLine();
}
}catch (Exception e){System.out.println("!!!!");}
br.close();
}
//get reply from server and print it out
public static void receiveFile() throws IOException
{
BufferedReader brComingFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
try
{
String inline = brComingFromServer.readLine();
while(inline!=null)
{
System.out.println(inline);
inline = brComingFromServer.readLine();
}
}catch (Exception e){}
}
}
Get rid of the ready() test. Change it to:
while ((line = in.readLine()) != null)
{
// ...
}
readLine() will block until data is available. At present you are stopping the read loop as soon as there isn't data available to be read without blocking. In other words you are assuming that `!ready()! means end of stream. It doesn't: see the Javadoc.
Objective: send data to server and have server return something back and to print what the server sends back
Problem: If I close the out stream then it will send the data to the server but my input stream won't work and I can't receive what the server tries to give me. If I use flush() to send data to server the server never receives the data. I have been stuck on this for literally 3 hours. How do you do read and Write at the same time.
Client.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
class Client {
public static void main(String args[]) {
String data = "head";
try {
Socket skt = new Socket("server", 5050);
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
out.print(data);
out.close();
System.out.println("Sent data");
while (!in.ready()) {
}
String input = in.readLine();
System.out.println(input);
out.close();
in.close();
skt.close();
} catch (Exception e) {
System.out.print("Whoops! It didn't work!\n" +e.toString());
}
}
}
Server.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.PrintWriter;
class Server {
public static void main(String args[]) {
String data;
String input;
try {
ServerSocket srvr = new ServerSocket(5050);
Socket skt = srvr.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(
skt.getInputStream()));
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
// /////Waits for message from client/////////
while (!in.ready()) {
}
// ///////////////////////////////////////////
input = in.readLine(); // Read the message
System.out.println("Received String input: " + input);
// Send output to client
System.out.println("After output");
if (input.equals("head"))
data = "haha";
else
data = "Wtf did you send me";
Thread.sleep(2000);
out.print(data);
// ///////////////////
System.out.println("Sent data: " + data);
in.close();
out.close();
skt.close();
srvr.close();
} catch (Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
In your client you have:
out.print(data);
out.close();
and in your server you have:
input = in.readLine();
Client send message without new line character - that's why in.ready() is true. What's more - it closes PrintWriter instead of flushing (you are also closing stream at the end of program). If you change that lines to:
out.println(data); // sends message with new line character
out.flush(); // unnecessary
Client sends a message. The same thing is when Server sends message - you use print instead of println but Client reads using readLine that reads until new line character, or to be more precise (BufferedReader|readLine()):
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
One more thing - you are connecting to the server using "server" hostname. For my tests I changed it to "localhost". Maybe there is other mistake?