I'm writing a proxy server using sockets, now it seems that it's "working" more or less but the problem I'm experiencing now is that the images of the URL's are not getting back to the browser, only the text is returned...
This is the code:
//create inputstream to receive the web page from the host
BufferedInputStream inn = new BufferedInputStream(clientURLSocket.getInputStream());
//create outputstream to send the web page to the client
BufferedOutputStream outt = new BufferedOutputStream(clientSocket.getOutputStream());
URL u = new URL("http://"+url);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
byte[] chunk = new byte[1024];
int bytesRead;
InputStream stream = u.openStream();
while ((bytesRead = stream.read(chunk)) > 0) {
outputStream.write(chunk, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
outt.write(outputStream.toByteArray());
outt.flush();
Maybe ByteArrayOutputStream is not good to receive images?
Edit (sorry for the late response):
This is my new code:
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
public class Server {
public void startServer() {
final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);
Runnable serverTask = new Runnable() {
#Override
public void run() {
try {
#SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket clientSocket = serverSocket.accept();
Socket clientURLSocket = serverSocket.accept();
clientProcessingPool.submit(new ClientTask(clientSocket));
clientProcessingPool.submit(new ClientTask(clientURLSocket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread serverThread = new Thread(serverTask);
serverThread.start();
}
private class ClientTask implements Runnable {
private Socket clientSocket;
private Socket clientURLSocket;
private ClientTask(Socket clientSocket) {
this.clientSocket = clientSocket;
this.clientURLSocket = clientSocket;
}
#Override
public void run() {
try {
String url = null;
String curl = null;
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
String buffer;
while ((buffer = in.readLine()) != null) {
//System.out.println(buffer);
if(buffer.contains("GET"))
{
String[] splitText = buffer.split(" ");
curl = splitText[1];
System.out.println(curl);
}
if(buffer.contains("Host"))
{
//parse the host
url = buffer.replace("Host: ", "");
System.out.println(url);
}
if (buffer.isEmpty()) break;
}
//String IP = InetAddress.getByName(url).getHostAddress().toString();
//new socket to send the information over
clientURLSocket = new Socket(url, 80);
//get data from a URL
/* URL host = new URL("http://"+url);
URLConnection urlConnection = host.openConnection();
InputStream input = urlConnection.getInputStream();
int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
input.close();*/
//create inputstream to receive the web page from the host
BufferedInputStream inn = new BufferedInputStream(clientURLSocket.getInputStream());
//create outputstream to send the web page to the client
BufferedOutputStream outt = new BufferedOutputStream(clientSocket.getOutputStream());
URL u = new URL(curl);
HttpURLConnection connection = null;
connection = (HttpURLConnection) u.openConnection();
connection.connect();
//ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
byte[] chunk = new byte[1024];
int bytesRead;
InputStream stream = connection.getInputStream();
while ((bytesRead = stream.read(chunk)) > 0) {
//outputStream.write(chunk, 0, bytesRead);
outt.write(chunk, 0, bytesRead);
outt.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
//outt.write(outputStream.toByteArray());
//outt.flush();
outt.close();
inn.close();
clientURLSocket.close();
/*
out.close();
in.close();
clientSocket.close();
*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Now the problem is that google.com is working fine (it shows all the images and text), but for example youtube.com is not working fine (it also shows the text and images but the web is not being showed completely and it's disordered).
What I'm missing in this code?
By the way, thanks EJP & JB Nizet for your help!
It seems you don't understand how HTTP and HTML work.
When you go to http://google.com with your browser, a first request is sent to get the HTML page. The server response contains the HTML markup, and only that. Then the browser reads and parses this HTML markup and sees that it contains (for example)
<img src="logo.png"/>
So it sends a new HTTP request to the URL http://google.com/logo.png. The server sends a response containing the bytes of the logo image.
If your code only sends a single request to http://google.com, you'll never get the logo.
An HTTP proxy is a lot simpler than what you're doing here.
You are supposed to connect to the URL named in the CONNECT command. Not parse the GET and HOST headers. Once you've processed the CONNECT command, the rest is just copying bytes back and forth.
Related
Socket socket = new Socket("192.168.178.47", 82);
OutputStream out = socket.getOutputStream();
out.write("{ \"phone\": \"23456789\" }".getBytes());
out.flush();
//Server
InputStream in = client.getInputStream();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int i = 0;
while((i = in.read()) >= 0) {
bOut.write(i);
}
String complete = new String(bOut.toByteArray(), "UTF-8");
I had tried to send data via OutputStream to a socket but the data is not flushing. If I add an out.close(); to the end then it works perfectly, but the socket is closed and I cannot accept the response. Does anybody know why? The server is not giving any type of error. I had used Java 1.7!
It is possible that the server is waiting for the end of line. If this is the case add "\n" to the text
I'm not sure of the labelling "//Server" in your question, but I'm assuming the following code is the server code:
InputStream in = client.getInputStream();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int i = 0;
while((i = in.read()) >= 0) {
bOut.write(i);
}
String complete = new String(bOut.toByteArray(), "UTF-8");
This will continue to read, blocking each time, until it gets a value from read() less than zero. That only happens if the stream is closed.
It really looks like you need to establish your own protocol. So instead of looking for "<=0" look for some constant value that signals the end of the message.
Here's a quick demonstration of what I mean (I didn't have time yesterday). I have 3 classes, Message,MyClient (which also is the main class), and MyServer. Notice there isn't anything about sending or receiving a newline. Nothing is setting tcpNoDelay. But it works fine. Some other notes:
This code only sends and receives a single request and response.
It doesn't support sending multiple Message instances. That would require checking for the start of a Message as well as the end.
Message class:
public class Message {
public static final String MSG_START = "<message>";
public static final String MSG_END = "</message>";
private final String content;
public Message(String string){
content = string;
}
#Override
public String toString(){
return MSG_START + content + MSG_END;
}
}
MyServer class
public class MyServer implements Runnable{
public static final int PORT = 55555;
#Override
public void run(){
try {
ServerSocket serverSocket = new ServerSocket(PORT);
Socket socket = serverSocket.accept();
String message = getMessage(socket);
System.out.println("Server got the message: " + message);
sendResponse(socket);
}catch (IOException e){
throw new IllegalStateException(e);
}
}
private void sendResponse(Socket socket) throws IOException{
Message message = new Message("Ack");
System.out.println("Server now sending a response to the client: " + message);
OutputStream out = socket.getOutputStream();
out.write(message.toString().getBytes("UTF-8"));
}
private String getMessage(Socket socket) throws IOException{
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
StringBuilder sb = new StringBuilder(100);
byte[] bytes = new byte[1024<<8];
while(sb.lastIndexOf(Message.MSG_END) == -1){
int bytesRead = in.read(bytes);
sb.append(new String(bytes,0,bytesRead,"UTF-8"));
}
return sb.toString();
}
}
MyClient class
public class MyClient {
public static void main(String[] args){
MyClient client = new MyClient();
Thread server = new Thread(new MyServer());
server.start();
client.performCall();
}
public void performCall(){
try {
Socket socket = new Socket("127.0.0.1",MyServer.PORT);
sendMessage(socket, "Why hello there!");
System.out.println("Client got a response from the server: " + getResponse(socket));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
public String getResponse(Socket socket) throws IOException{
String response;
StringBuilder sb = new StringBuilder(100);
InputStream in = socket.getInputStream();
byte[] bytes = new byte[1024];
while(sb.lastIndexOf(Message.MSG_END) == -1){
int bytesRead = in.read(bytes);
sb.append(new String(bytes,0,bytesRead,"UTF-8"));
}
response = sb.toString();
return response;
}
public void sendMessage(Socket socket, String message) throws IOException{
Message msg = new Message(message);
System.out.println("Client now sending message to server: " + msg);
OutputStream out = socket.getOutputStream();
out.write(msg.toString().getBytes("UTF-8"));
}
}
The output
Client now sending message to server: Why hello there!
Server got the message: Why hello there!
Server now sending a response to the client: Ack
Client got a response from the server: Ack
Process finished with exit code 0
The problem is not that you are not flushing properly, but that the reading code waits for the socket to disconnect before handling the data:
while((i = in.read()) >= 0)
Will loop as long as something can be read from in (the socket's InputStream). The condition will not fail until the other peer disconnects.
Try using
socket.setTcpNoDelay(true);
There is buffering that occurs for performance reasons (read up on Nagle's algorithm).
Looking at your code it seems ok. However you are sending less than the MTU Nagle's algothrim could be holding it back until enough data is present for a full packet or you close the socket.
So - try this:
socket.setTCPNoDelay(true);
http://en.wikipedia.org/wiki/Nagle%27s_algorithm
https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html#setTcpNoDelay-boolean-
I have created a simple Client/Server program where the client takes a file from command line arguments. The client then sends the file to the server, where it is compressed with GZIP and sent back to the client.
The server program when ran first is fine, and produces no errors but after running the client I get the error.
I am getting an error saying the connection is reset, and i've tried numerous different ports so i'm wondering if there is something wrong with my code or time at which i've closed the streams?
Any help would be greatly appreciated!
EDIT - Made changes to both programs.
Client:
import java.io.*;
import java.net.*;
//JZip Client
public class NetZip {
//Declaring private variables.
private Socket socket = null;
private static String fileName = null;
private File file = null;
private File newFile = null;
private DataInputStream fileIn = null;
private DataInputStream dataIn = null;
private DataOutputStream dataOut = null;
private DataOutputStream fileOut = null;
public static void main(String[] args) throws IOException {
try {
fileName = args[0];
}
catch (ArrayIndexOutOfBoundsException error) {
System.out.println("Please Enter a Filename!");
}
NetZip x = new NetZip();
x.toServer();
x.fromServer();
}
public void toServer() throws IOException{
while (true){
//Creating socket
socket = new Socket("localhost", 4567);
file = new File(fileName);
//Creating stream to read from file.
fileIn = new DataInputStream(
new BufferedInputStream(
new FileInputStream(
file)));
//Creating stream to write to socket.
dataOut = new DataOutputStream(
new BufferedOutputStream(
socket.getOutputStream()));
byte[] buffer = new byte[1024];
int len;
//While there is data to be read, write to socket.
while((len = fileIn.read(buffer)) != -1){
try{
System.out.println("Attempting to Write " + file
+ "to server.");
dataOut.write(buffer, 0, len);
}
catch(IOException e){
System.out.println("Cannot Write File!");
}
}
fileIn.close();
dataOut.flush();
dataOut.close();
}
}
//Read data from the serversocket, and write to new .gz file.
public void fromServer() throws IOException{
dataIn = new DataInputStream(
new BufferedInputStream(
socket.getInputStream()));
fileOut = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(
newFile)));
byte[] buffer = new byte[1024];
int len;
while((len = dataIn.read(buffer)) != -1){
try {
System.out.println("Attempting to retrieve file..");
fileOut.write(buffer, 0, len);
newFile = new File(file +".gz");
}
catch (IOException e ){
System.out.println("Cannot Recieve File");
}
}
dataIn.close();
fileOut.flush();
fileOut.close();
socket.close();
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.zip.GZIPOutputStream;
//JZip Server
public class ZipServer {
private ServerSocket serverSock = null;
private Socket socket = null;
private DataOutputStream zipOut = null;
private DataInputStream dataIn = null;
public void zipOut() throws IOException {
//Creating server socket, and accepting from other sockets.
try{
serverSock = new ServerSocket(4567);
socket = serverSock.accept();
}
catch(IOException error){
System.out.println("Error! Cannot create socket on port");
}
//Reading Data from socket
dataIn = new DataInputStream(
new BufferedInputStream(
socket.getInputStream()));
//Creating output stream.
zipOut= new DataOutputStream(
new BufferedOutputStream(
new GZIPOutputStream(
socket.getOutputStream())));
byte[] buffer = new byte[1024];
int len;
//While there is data to be read, write to socket.
while((len = dataIn.read(buffer)) != -1){
System.out.println("Attempting to Compress " + dataIn
+ "and send to client");
zipOut.write(buffer, 0, len);
}
dataIn.close();
zipOut.flush();
zipOut.close();
serverSock.close();
socket.close();
}
public static void main(String[] args) throws IOException{
ZipServer run = new ZipServer();
run.zipOut();
}
}
Error Message:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at java.io.DataInputStream.read(DataInputStream.java:100)
at ZipServer.<init>(ZipServer.java:38)
at ZipServer.main(ZipServer.java:49)
First, the error occurs because the client fails and ends before sending any data, so that
the connection is closed at the time the server wants to read.
The error occurs because you assign the File objects to unused local variables (did your compiler not warn?)
public File file = null;
public File newFile = null;
public static void main(String[] args) throws IOException {
try {
String fileName = args[0];
File file = new File(fileName);
File newFile = new File(file +".gz");
}
catch (ArrayIndexOutOfBoundsException error) {
System.out.println("Please Enter a Filename!");
}
but In your toServer method you use the class variable file as parameter for FileInputStream and this variable is null and this results in an error which ends the program.
Second, if you finished the writing to the outputstream, you should call
socket.shtdownOutput();
Otherwise, the server tries to read until a timeout occurs.
Problem is that server is not able to download apache maven.
So what you can do is just copy the apache maven folder and paste it in the wrapper folder inside the project.
It will manually download the apache maven, and it will definitely work.
I'm experiencing some issues with this code depending on the browser I use, there are URL's displayed correctly in IE but being displayed as plain text in Firefox (for instance www.microsoft.es looks good on IE but not on Firefox).
Don't know what I'm doing wrong here, I think that there's a problem with the headers that I'm using but I'm not sure...
This is the code:
import java.io.*;
import java.net.*;
import java.util.concurrent.*;
public class Server {
public void startServer() {
final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);
Runnable serverTask = new Runnable() {
#Override
public void run() {
try {
#SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket clientSocket = serverSocket.accept();
clientProcessingPool.submit(new ClientTask(clientSocket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread serverThread = new Thread(serverTask);
serverThread.start();
}
private class ClientTask implements Runnable {
private Socket clientSocket;
private ClientTask(Socket clientSocket) {
this.clientSocket = clientSocket;
}
#Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedOutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
String url = null;
int i=0;
String [] headers = new String [100];
String buffer;
while ((buffer = in.readLine()) != null) {
headers[i]=buffer;
i++;
if(buffer.contains("GET"))
{
String[] splitText = buffer.split(" ");
url = splitText[1];
}
if(buffer.contains("POST"))
{
String[] splitText = buffer.split(" ");
url = splitText[1];
}
if(buffer.contains("CONNECT"))
{
String[] splitText = buffer.split(" ");
url = "https://"+splitText[1];
}
if (buffer.isEmpty()) break;
}
URL u = new URL(url);
URLConnection connection = u.openConnection();
for (int x=1;x<i-1;x++){
if (!headers[x].contains("Accept-Encoding:")){
connection.setRequestProperty(headers[x].substring(0, headers[x].indexOf(":")).toString() , headers[x].replace(headers[x].substring(0, headers[x].indexOf(":") +2), "").toString());
}
}
boolean redirect = false;
int status = ((HttpURLConnection) connection).getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect) {
String Url = connection.getHeaderField("Location");
URL urlloc = new URL(Url);
connection = urlloc.openConnection();
for (int x=1;x<i-1;x++){
if (!headers[x].contains("Accept-Encoding:")){
connection.setRequestProperty(headers[x].substring(0, headers[x].indexOf(":")).toString() , headers[x].replace(headers[x].substring(0, headers[x].indexOf(":") +2), "").toString());
}
}
}
byte[] chunk = new byte[1024];
int bytesRead;
InputStream stream;
stream = connection.getInputStream();
while ((bytesRead = stream.read(chunk)) > 0) {
out.write(chunk, 0, bytesRead);
out.flush();
}
out.close();
in.close();
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Any help would be appreciated.
Thanks.
I'm not sure what the problem is, but you should use a tool like Wireshark to examine the actual network traffic between the browser and your proxy, and compare it to the network traffic between the browser and the site when you connect to the site directly.
i have this client/server application, and the client application sometimes completely freezes when i try send object via sockets
public Client client = new Client();
petriNetList = client.query(client.actionLoadPetriNetList, MainWindow.loginUsername);
here is the code of the client
public class Client {
public static Socket kkSocket = null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static BufferedReader stdIn = null;
public static OutputStream outputStream = null ;
public static ObjectOutputStream objectOutputStream = null ;
public static InputStream inputStream = null ;
public static ObjectInputStream objectInputStream = null ;
public String actionSavePetriNet = "SAVE PETRI NET START";
/*
* Save petri net to server
*
* #param action identifies query
* #param petriName name of the petri net
* #param username username
* #param xml content of the petri net
*
* #return int response form server
*/
public int query (String action,String petriName,String username,String xml) throws IOException {
int size ;
int result = 0;
connect();
if (action.equals(actionSavePetriNet)) {
out.println(action); // save petri net
out.println(petriName); //petri net name
out.println(username); //username
System.out.println("(Client:)" + xml);
objectOutputStream.writeObject(xml); //send object over the network
System.out.println("Dostali sme sa sem ?");
result = Integer.parseInt(in.readLine()); //read response from server
}
disconnect();
return result;
}
/*
* connect to server
* TODO: ADD hostname and port as parameter
*/
public static void connect() throws IOException {
try {
kkSocket = new Socket("osiris-PC", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}
stdIn = new BufferedReader(new InputStreamReader(System.in));
outputStream = kkSocket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
inputStream = kkSocket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
}
/*
* Disconnect from server
* close all input/output streams
*
*/
public static void disconnect() throws IOException {
out.close();
in.close();
stdIn.close();
objectOutputStream.close();
outputStream.close();
objectInputStream.close();
inputStream.close();
kkSocket.close();
}
}
and this is the code of server
public class PetriServer {
public static Protocol kkp = new Protocol();
public static InputStream inputStream = null ;
public static ObjectInputStream objectInputStream = null ;
public static OutputStream outputStream = null ;
public static ObjectOutputStream objectOutputStream = null ;
public static void main(String[] args) throws IOException, ClassNotFoundException {
kkp.loadUsers();
while(true) {
ServerSocket serverSocket = null;
inputStream = null ;
objectInputStream = null ;
outputStream = null ;
objectOutputStream = null ;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
inputStream = clientSocket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
outputStream = clientSocket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
inputLine = in.readLine();
String inputLine;
if (inputLine.equals("SAVE PETRI NET START")) {
String petriName;
String username;
String xml ;
String input;
int result;
int size ;
petriName = in.readLine(); //read petri name
System.out.println("(Server): prijali sme poziadavok na ulozenie " + petriName );
username = in.readLine(); //read username
System.out.println("(Server): poziadavok ide od usera: " + username);
while(true) {
//this is the line where is occasionally freezes
xml = (String)objectInputStream.readObject(); //read object over the network
if (!xml.isEmpty()) break;
}
System.out.println("(Server):" + xml);
result = kkp.savePetrinet(username, petriName,xml); //save it to the file
out.println(result);
}
out.close();
in.close();
objectInputStream.close();
inputStream.close();
objectOutputStream.close();
outputStream.close();
clientSocket.close();
serverSocket.close();
}
}
}
Does anybody know what can be problem ?
cheers.
You have the Socket InputStream on the server attached to two different inputs, ObjectInputStream and BufferedReader. InputStreams are not intended to be used this way, and doing so can cause a lot of problems. BufferedReaders, by their nature, are going to be pulling more data off the InputStream than you are actually reading. If they happen to buffer data that makes up an object, then your subsequent attempt to read the object off the ObjectInputStream will block, because the data has already been removed.
You need to pick one method or the other to read data. If you need to be able to read both Strings and Objects off the Socket, then you will have to go to a byte-oriented mode of operation where you read the bytes off into a byte array, then process the byte array yourself to insure that you don't loose any data.
Edit based on #Dunes comment below
Seems your best bet is to stick entirely with ObjectInputStream and make use of its other methods that allow you to read arbitrary primitive types from the stream. And, if you don't mind the deprecated call, it even has a readLine method.
I'm writing a piece of software, and I'm under the restriction of not being able to use socket to connect to a java application using a ServerSocket.
I thought I'd try with an URL connection, since it's possible to define which port to connect to
e.g:
127.0.0.1:62666
I have my server app listening for connections and writing the input out to a jTextArea. When connecting to the server (127.0.0.1:62666) through a browser, it outputs:
GET / HTTP/1.1
GET /favicon.ico HTTP/1.1
I have another app for connecting to the ServerSocket through an URL connection:
try{
URL url = new URL("http://127.0.0.1:62666");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.connect();
PrintWriter writer = new PrintWriter(connection.getOutputStream());
writer.print("Hello");
System.out.println("should have worked");
writer.flush();
writer.close();
}catch(IOException e){
e.printStackTrace();
}
It prints out the "should have worked" message fyi, but it never writes anything to the jTextArea of the server. The code for the server app looks like this:
try{
ServerSocket serverSock = new ServerSocket(62666);
while(doRun){
Socket sock = serverSock.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter writer = new PrintWriter(sock.getOutputStream());
InfoReader.gui.writeToTextArea(reader.readLine() + " From IP: " + sock.getInetAddress() + "\n");
writer.println("Testing123");
writer.close();
reader.close();
}
}catch(IOException e){
e.printStackTrace();
}
Note: when connecting through the browser it displays the text "Testing123".
So I'm wondering how to do this the way I'm trying or perhaps read the URL that the ServerSocket was accessed through, so I could access it through a URL while passing it arguments (in the URL).
Hope this makes sense :)
Thanks,
Mike.
There is one very good example:
public class SimpleHTTPServer {
public static void main(String args[]) throws IOException {
ServerSocket server = new ServerSocket(8080);
while (true) {
try (Socket socket = server.accept()) {
Date today = new Date();
String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
}
}
}
}
Go to http://127.0.0.1:8080/ from browser and you'll get current date.
I can't figure out exactly what's up. There's something funny about that OutputStream. Add a
((HttpURLConnection) connection).getResponseCode();
somewhere after connect() and before close(), and you should see something different, if not what you expect.
Perhaps instead of trying to use HTTP as a hack, you should just go full HTTP. Use HTTP from the client like you already are, and set up an embedded HTTP server on the server. There are several to choose from out there that literally take just a few lines to get running: Grizzly, Simple Framework, or Jetty, for instance.
I think this is what you need to do if you want the client to send a message to the server using a URL connection:
public class Client
{
public Client()
{
try
{
url = new URL("http://127.0.0.1:62666");
URLConnection urlConnection = url.openConnection();
PrintWriter writer = new PrintWriter(urlConnection.getOutputStream());
writer.println("Hello World!");
writer.flush();
writer.close();
}catch(Exception e){e.printStackTrace();}
}
}
Now heres the server:
public class Server implements Runnable
{
public Server()
{
ServerSocket server = new Server(62666);
client = server.accept();
new Thread(this).start();
}
public void run()
{
try
{
String message;
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()))
while((message=reader.readLine())!=null)
{
System.out.println("Message from client: "+message);
}
}catch(Exception e)
{
System.out.println("Client disconnected");
}
}
Socket client;
}
writer.println("Hello");
You're not sending any newline. Also your 'should have worked' trace is in the wrong place. Should be after the flush().
Also you aren't reading the response.
Also the server is only going to display POST ... Or PUT ..., not the line you're sending. So this is never going to work unless you (a) make the server HTTP-conscious or (b) get rid of this insane restriction that you can't use a Socket. Why can't you use a Socket?
EDIT: my version of your code follows:
static class Server implements Runnable
{
public void run()
{
try
{
ServerSocket serverSock = new ServerSocket(62666);
for (;;)
{
Socket sock = serverSock.accept();
System.out.println("From IP: " + sock.getInetAddress());
BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
PrintWriter writer = new PrintWriter(sock.getOutputStream());
String line;
while ((line = reader.readLine()) != null)
{
System.out.println("\t:" + line);
}
writer.println("Testing123");
writer.close();
reader.close();
System.out.println("Server exiting");
serverSock.close();
break;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
static class Client implements Runnable
{
public void run()
{
try
{
URL url = new URL("http://127.0.0.1:62666");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
// connection.setRequestMethod("POST");
connection.connect();
PrintWriter writer = new PrintWriter(connection.getOutputStream());
writer.println("Hello");
writer.flush();
System.out.println("flushed");
int responseCode = connection.getResponseCode();
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
System.out.println("closed");
System.out.println("response code="+responseCode);
String line;
while ((line = reader.readLine()) != null)
System.out.println("client read "+line);
reader.close();
System.out.println("Client exiting");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Thread t = new Thread(new Server());
t.setDaemon(true);
t.start();
new Client().run();
System.out.println("Main exiting");
}