I'm trying to send a message from a computer and read it from another using java socket. The problem is that the message received by the server is something unencoded represented by two question marks inside a diamond.
To read and write I use the following objects:
PrintWriter out = new PrintWriter(new OutputStreamWriter( socket.getOutputStream(), "UTF-8"), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")));
And the methods:
out.println("OK");
in.readLine();
Use DataInputStream and DataOutputStream to read/write the socket. These have readUTF() and writeUTF() methods which will send Strings properly.
Related
So I'm currently doing a project where we have to accept and process HTTP requests, which I have done successfully.
There's an optional task of also accepting HTTPS requests, which I'm confused as to how I'd go about that, preferably using standard Java.
For regular HTTP, I'm using a regular ServerSocket accept(), then reading from the socket through an InputStreamReader in its InputStream, like so:
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Problem is, when I try with HTTPS, the message I can read on the first line of the HTTPS GET is just garbled characters. Is there a way to process this as well?
I'm having trouble using HttpUrlConnection. I'm working on multiple servers. Some servers send response in gzip encoding and some don't. For gzip encoding, I'm using
inputStream = new GZIPInputStream(connection.getInputStream());
inputStreamReader = new InputStreamReader(inputStream);
And for normal encoding, I'm using
inputStreamReader = new InputStreamReader(connection.getInputStream());
Is it possible to know the encoding of getInputStream so that I know beforehand whether or not to use GZIPInputStream. Or is there a generic input stream reader for both compressed & uncompressed. Thanks.
Get the content encoding from the HttpURLConnection using getContentEncoding().
It it's gzip-encoded, the result of that call should be gzip, and then you know what type of input stream you need to create.
I read other SO questions about this problem and nothing solve my problem so I think this post is not a duplicate.
I want to send a Unicode string from android client socket to a server socket But I receive all Unicode character as question marks from server (I use a c# server on windows). I use WireShark to monitor network and it show all Unicode character as dots. This is my send code:
String msg = "تست سوکت";
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(msg);
out.flush();
Some stackOverFlow question says adding charSetEncoder to OutputStreamWriter constractor can solve this but that's not worked for me and the result was the same:
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream(),
Charset.forName("UTF-8").newEncoder())),
true);
// Or
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream(),
"UTF-8")),
true);
After that I try to send byte array as string, But that send nothing to server:
byte[] byteArray = msg.getBytes(Charset.forName("UTF-8"));
out.println(byteArray.toString() + "\n");
Your Java code is correct.
That Wireshark is not showing unicode characters is assumable a limitation of Wireshark.
Therefore the receiving C# program must be the problem. Are you sure that this program expects a stream of UTF-8 characters?
"Unicode" on Windows and C# usually means UTF-16, not UTF-8.
I am trying to create a proxy server.
I want to read the websites byte by byte so that I can display images and all other stuff. I tried readLine but I can't display images. Do you have any suggestions how I can change my code and send all data with DataOutputStream object to browser ?
try{
Socket s = new Socket(InetAddress.getByName(req.hostname), 80);
String file = parcala(req.url);
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader dis = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter socketOut = new PrintWriter(s.getOutputStream());
socketOut.print("GET "+ req.url + "\n\n");
//socketOut.print("Host: "+req.hostname);
socketOut.flush();
String line;
while ((line = dis.readLine()) != null){
System.out.println(line);
}
}
catch (Exception e){}
}
Edited Part
This is what I should have to do. I can block banned web sites but can't allow other web sites in my program.
In the filter program, you will open a TCP socket at the specified port and wait for connections. If a
request comes (i.e. the client types a URL to access a web site), the application will process it to
decide whether access is allowed or not and then, using the same socket, it will send the reply back
to the client. After the client opened her connection to WebPolice (and her request has been checked
and is allowed), the real web page needs to be shown to the client. Therefore, since the user already gave her request, now it is WebPolice’s turn to forward the request so that the user can get the web page. Thus, WebPolice acts as a client and requests the web page. This means you need to open a connection to the web server (without closing the connection to the user), forward the request over this connection, get the reply and forward it back to the client. You will use threads to handle multiple connections (at the same time and/or at different times).
I don't know what exactly you're trying to do, but crafting an HTTP request and reading its response incorporates somewhat more than you have done here. Readline won't work on binary data anyway.
You can take a look at the URLConnection class (stolen here):
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
Then you can read textual or binary data from the in object.
Read line will treat the line read as a String, so unless you want to mess around with conversions over to bytes, I wouldn't recommend that.
I would just read bytes until you can't read anymore, then write them out to a file, this should allow you to grab the images, keeping file headers intact which can be important when dealing with files other than text.
Hope this helps.
Instead of using BufferedReader you can try to use InputStream.
It has several methods for reading bytes.
http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html
My server code is as follow:
writer = new PrintWriter(s.getOutputStream());
writer.flush();
writer.print("HTTP/1.1 200 OK\r\n");
writer.print("Content-Length: " + len + "\r\n");
writer.print("Content-Type: "+"application/soap+xml;charset=utf-8"+"\r\n\r\n");
writer.print(response);
writer.close();
The variable response is SOAP+XML. I am adding the HTTP headers needed to send it over and it works in some cases but in most cases my client(SOAPUI) just waits for the response which doesn't get produced. When I close the client I don't see any error on the server side.
Any help is appreciated. Thanks.
From Socket.getOutputStream():
"...Closing the returned OutputStream will close the associated socket....".
Closing the PrintWriter will close the OutputStream which in turn will close the socket. Just flush the PrintWriter, don't close it.