Good evening, i got this server here,
httpServer
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class httpServer {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/test", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
//NimServer nimserver = new NimServer(32778);
//nimserver.serve();
}
static class MyHandler implements HttpHandler {
AtomicInteger atomicInteger = new AtomicInteger(0);
int theValue = atomicInteger.get();
#Override
public void handle(final HttpExchange t) throws IOException {
final String response;
final String requestMethod = t.getRequestMethod();
if ("GET".equals(requestMethod)) {
response = String.format("Besuche: %d%n", atomicInteger.addAndGet(1));
}
else if ("POST".equals(requestMethod)) {
atomicInteger.set(0);
response = "Reset to 0";
}
else {
throw new IOException("Unsupported method");
}
t.sendResponseHeaders(200, response.length());
final OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
and this Client here
httpClient
import java.net.*;
import java.io.*;
public class httpClient {
static int clientno = 0;
public static void main(String[] args) throws Exception {
System.out.println(clientno);
URL test = new URL("http://localhost:8000/test");
HttpURLConnection connect = (HttpURLConnection) test.openConnection();
connect.setRequestMethod("POST");
BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Now i want to send the int clientno from the client to the server and the server should count it up like ++ and send it back to the client. I read alot about Dataoutputstream but there are always sockets included. Is it possible to do it without sockets?
On the server side you in the handler for "POST" you have to invoke t.getRequestBody() - this will give you client input. Either using BufferedReader (if text based - Integer.valueOf(stream.readLine())) or DataInputStream (if binary based - stream.readInt()) you can read the number. Similarly by t.getResponseBody() you get the output to client and write the number in similar way.
For the client the HttpURLConnection has getInputStream() and getOutputStream() to which you need to write clientNumber similarly how you do in server.
Related
I've been working on simple http server in java. When I try to send some http request via google
browser "line" variable should have request string but it has junk characters.
Do you have any idea what is causing the problem?
Code is from this tutorial: https://www.youtube.com/watch?v=FqufxoA4m70&t=2061s&ab_channel=BloomInstituteofTechnology
Here is part of code where you can find this problem.
package com.company;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Main
{
public static void main(String[] args) throws Exception
{
try( ServerSocket socketServer = new ServerSocket(8080))
{
System.out.println("Server created");
while (true)
{
try (Socket client = socketServer.accept())
{
System.out.println("Client connected: " + client.toString());
InputStreamReader isr = new InputStreamReader(client.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuilder request = new StringBuilder();
String line;
line = br.readLine();
while(!line.isBlank())
{
request.append(line);
line = br.readLine();
}
}
}
}
}
I don't know what you're actually doing with your browser. What are you typing in the URL bar? If you are trying to use https instead of http it will not work. Maybe "google browser" is tricking you and doing stuff behind your back... not exactly sending simple HTTP gets.
This works. It's not perfect but at least all IO streams are properly closed and a dummy response is sent to the client (which otherwise will remain hanging).
Try it by typing in your browser http://localhost:8080/some-random-url.
I cannot try with chrome as I don't use it, but this works using Firefox. As a rule, i would test stuff like this with curl and not with a browser - you have more control while testing simple stuff.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws Exception {
try (ServerSocket socketServer = new ServerSocket(8080)) {
System.out.println("Server created");
while (true) {
try (Socket client = socketServer.accept()) {
System.out.println("Client connected: " + client.toString());
try (final InputStream is = client.getInputStream();
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
final OutputStream os = client.getOutputStream()) {
final StringBuilder request = new StringBuilder();
String line;
while (!(line = br.readLine()).isBlank()) {
request.append(line).append("\n");
}
System.out.println("+++++++++++++++++++++++ REQUEST ");
System.out.println(request);
String response = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Connection: closed\r\n" +
"\r\n" +
"Hello there!\r\n";
os.write(response.getBytes(StandardCharsets.UTF_8));
os.flush();
System.out.println("----------------------- RESPONSE ");
System.out.println(response);
}
}
}
}
}
}
Random remark: don't implement an HTTP server on your own. Just do it to understand how it works.
I'm trying to connect two simple Java sockets but whatever port number I type I get the same error : Address already in use: JVM_Bind
Now I found I way around the problem by using using 0 as an argument to the ServerSocket constructor and then calling the getLocalPort method to get the first available port and then pass it to my client class in the Socket constructor as an argument.
So, in NetBeans IDE, I first run the server, get the available port from the console, copy the number and manually enter it to the Socket constructor as the second argument after "localhost" and run the client.
Now the expected output would be "Connected" as the server has accepted the client, but instead, I get the available port number incremented by 1.
Why is this happening? It seems that when I click run in my client.java file I start the server again instead of the client.
sever.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String[] args) throws IOException {
ServerSocket s1 = new ServerSocket(58801);/I manually add the available port number here
System.out.println(s1.getLocalPort());
Socket ss = s1.accept();
System.out.println("Client connected");
}
}
client.java :
import java.io.IOException;
import java.net.Socket;
public class client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 58801); // I here manually add the available port number
}
}
A somewhat belated answer, after the OP already figured it out:
One possible cause is running the server twice by mistake in the IDE. The first run grabs the port, the second run of the server will find that port already in use and generate the error.
An example of Server client socket:
Server class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class GreetServer {
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String greeting = in.readLine();
if ("hello server".equals(greeting)) {
out.println("hello client");
} else {
out.println("unrecognised greeting");
}
}
public void stop() throws IOException {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
}
public static void main(String[] args) throws IOException {
GreetServer server = new GreetServer();
server.start(6666);
}
}
Client class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class GreetClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void startConnection(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public String sendMessage(String msg) throws IOException {
out.println(msg);
String resp = in.readLine();
return resp;
}
public void stopConnection() throws IOException {
in.close();
out.close();
clientSocket.close();
}
public static void main(String[] args) throws IOException {
GreetClient client = new GreetClient();
client.startConnection("127.0.0.1", 6666);
String response = client.sendMessage("hello server");
System.out.println("Response from server: " + response);
}
}
Example response:
Response from server: hello client
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
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.