Java - Download a file through browser using a Socket - java

i was studying Java Socket and i tried to develop a Socket using port 80 to download a file from browser.
So, i run my main class (source below), it will open a Socket in any port i want to.
Then someone outside will access http://MY_IP:MY_PORT/download/FILE_NAME
I got this all working, however the filesize on client side is 0 bytes (for small files), and slightly lower size for bigger archives (original 600mb, download 540mb+-)
I really checked my code a lot of times, i couldn't find any error, i also changed from java libs to Apache-commons thinking it would help, but no success.
so maybe i think i got something wrong on Response headers.
Can you guys help me please?
Thanks in advance.
Class HTTPDownload:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
class HTTPDownloader {
Socket incoming = null;
ServerSocket server = null;
public HTTPDownloader(){
int port = 11000;
try{
server = new ServerSocket(port);
System.out.println("Creating SocketServer on Port " + port);
}catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Preparing to accept connections...");
while(true){
try{
incoming = server.accept();
System.out.println("connection!");
HTTPDownloaderThread thread1 = new HTTPDownloaderThread(incoming);
thread1.start();
}catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException{
new HTTPDownloader();
}
}
Class HTTPDownloadThread:
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Paths;
class HTTPDownloaderThread extends Thread {
private static final int BUFFER_SIZE = 4096;
private Socket socket;
private byte[] buf = new byte[BUFFER_SIZE];
private OutputStream out;
private InputStream is;
HTTPDownloaderThread(final Socket socket){
this.socket = socket;
}
public void run(){
int numberRead = 0;
try{
out = socket.getOutputStream();
is = socket.getInputStream();
numberRead = is.read(buf, 0, BUFFER_SIZE);
System.out.println("read " + numberRead);
if(numberRead<0)
return;
byte[] readBuf = new byte[numberRead];
System.arraycopy(buf, 0, readBuf, 0, numberRead);
String header = new String(readBuf);
System.out.println(header);
String fileName = header.split("\r\n")[0].split(" ")[1].substring(1);
System.out.println(socket.getInetAddress().getHostAddress()+" asked for file: "+fileName);
File f = new File("C:\\TestFolder\\"+fileName);
out.write("HTTP/1.1 200 OK\r\n".getBytes());
out.write("Accept-Ranges: bytes\r\n".getBytes());
out.write(("Content-Length: "+f.length()+"\r\n").getBytes());
out.write("Content-Type: application/octet-stream\r\n".getBytes());
out.write(("Content-Disposition: attachment; filename=\""+fileName+"\"\r\n").getBytes());
out.write("\r\n".getBytes()); // Added as Joy RĂȘ proposed, make it work!
Files.copy(Paths.get("C:\\TestFolder\\"+fileName) , out);
System.out.println("File upload completed!");
// out.flush();
out.close();
socket.close();
}catch(SocketException e) {
System.out.println(e.getMessage());
}catch(Exception e){
e.printStackTrace();
}
}
}

For one thing, add another "\r\n" between headers and data. Check your HTTP Response; does the Content-Length header report the correct file size for the downloaded file? Do the files show up usable on the client in the same way they were on the server?
Web proxies always helpful in debugging HTTP (or other client-server) applications :)
Also, I'm assuming you are specifying port 11000 on the browser, as that's what you're listening on on the server

The website does not let me to comment but i thought that I should tell about my findings.. By using
Files.copy("path",outStreamObj);
outStreamObj.close();
socketObj.close();
Will cause incomplete or corrupt downloads but if still want to use then outStreamObj and socketObj must not be closed the files transfer is fast with the above code (atleast from my observation). If you try to close it will report Broken Pipe or Connection reset or will not complete the download(freeze it).
Instead using the following code will let you close the outStreamObj as socketObj but file download is slow from the socket probably because of while loop.
Socket socket=serverSocket.accept();
FileInputStream fs=new FileInputStream(path);
OutputStream out = socket.getOutputStream();
//This is the change from the Files.copy()
int reads=0;
while((reads=fs.read())!=-1)
{
out.write(reads);
}
out.close();
socket.close();

Related

Sending Datagram Packets in Java

I am writing a program to send an integer (variable called intToSend) over a network using UDP. I run the program on two machines on the same network, one after the other. I thought that after running them both, the first one to be run would open a message box with the sent integer, but this doesn't happen. Both programs wait for a packet to be received as shown by "Waiting..." being printed to the console. I have the program ask for the destination ip to be input to the console. Then after that, the createSocket method is called, followed by sendData and then receiveData.
Here is the code:
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {
Scanner s = new Scanner(System.in);
PrintStream o = System.out, e = System.err;
InetAddress thisAddr, destAddr;
DatagramSocket socket;
int port = 1729, intToSend = 8;
boolean running = true;
public static void main(String[] args) {
new Main();
}
private Main() {
try {
thisAddr = InetAddress.getLocalHost();
System.out.println("Internal IP: " + thisAddr.getHostAddress().toString());
System.out.println("External IP: " + getIp());
} catch (Exception e) {
e.printStackTrace();
}
try {
destAddr = InetAddress.getByName(getDestIp());
} catch (UnknownHostException e) {
e.printStackTrace();
}
createSocket();
sendData();
receiveData();
}
private void receiveData(){
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
while(true){
System.out.println("Waiting...");
try {
socket.receive(receivePacket);
} catch (IOException e) {
e.printStackTrace();
}
String receivedText = new String(receivePacket.getData());
JOptionPane.showMessageDialog(null, receivedText);
}
}
private void sendData(){
byte[] dataToSend = String.valueOf(intToSend).getBytes();
DatagramPacket packet = new DatagramPacket(dataToSend, dataToSend.length, destAddr, port);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
private void createSocket(){
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
public static String getIp() throws IOException{
URL whatismyip = new URL("http://icanhazip.com");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
return in.readLine();
}
private String getDestIp() {
String temp;
o.println("What is the other user's ip?");
temp = s.nextLine();
return temp;
}
}
This code works for me. If I input the target IP as the local machine's IP then I get the popup. If I input another machine on the network I also get the popup. My guess would be that either one of your machines has a firewall running that is blocking the incoming UDP packet, or your machines have multiple network interfaces and the IP you detect is not the one that is on the same network as the other machine.
In the former case you can disable the firewall (not a good idea if your machines are not behind a router with a firewall or are on a network which you don't have full control over) or open the specific port for incoming and outgoing UDP on both machines.
In the latter case you want to look out for the IPs presented on both machines being on the same subnet (the first three numbers being the same in the case if IPv4) e.g. both starting with 192.168.1. or similar.
When you do get your packet through you will probably get a very long popup window because you allocate a 1024 byte array and put the string at the start of that array but then convert the entire 1024 byte array into a string which may include various stuff off the end of the first N bytes you wrote the int into.
There are various ways to resolve that but this but a simple way to be able to pack a bunch of data into a packet and then read it back reliably is to use ByteArrayInput/OutputStreams and DataInput/OutputStreams:
//Sending side
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeInt(N); //this will write 4 bytes
byte[] packetData = bout.toByteArray();
//Receiving side
byte[] packetBuffer = ...;
ByteArrayInputStream bin = new ByteArrayInputStream(packetBuffer);
DataInputStream din = new DataInputStream(bin);
int N = din.readInt(); //This will read only the first 4 bytes, and use the same marshalling as DataOutputStream to produce a consistent value, even if the integer value is something exceptional like infinity or NaN.

Converting java.net.Socket.getInputStream() to byte[] showing large delay

I have designed a Java Client class that is required to send a byte[] array to a Java Server class via a socket. Here is my code:
ByteArrayClient.java
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ByteArrayClient {
public static void main(String[] args) {
//make POJO__________________________
ByteEncodeSubscriptionReq sr1=ByteEncodeSubscriptionReq.makeRequest(103, "Str1", "Str2");
//Connection details____________________
String serverName = "localhost";
int port = 6060;
try {
//Establish Connection with server_______________________________
System.out.println("ByteArrayClient: Connecting to " + serverName +":" + port+"...");
Socket client = new Socket(serverName, port);//make new socket
System.out.println("ByteArrayClient: connected to " + client.getRemoteSocketAddress());
//Encode POJO to ByteArray________________________________
byte[] SubscripReqByteArray=ByteEncodeSubscriptionReq.encode(sr1);
//encoded correctly to a 44 bit byte array
System.out.println("ByteArrayClient: SubscripTionRequest successfully encoded");
//Send POJO ByteArray to server__________________________
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.write(SubscripReqByteArray);;
System.out.println("ByteArrayClient: POJO sent to server");
//Receive Server response_________________________________
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("ByteArrayClient received: " + in.readUTF());
//close socket____________________________________
client.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("PojoClient: Connection Failed");
}
}
}
...and ByteArrayServer.java
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class ByteArrayServer extends Thread{
private ServerSocket serverSocket;
public ByteArrayServer(int port) throws IOException {
serverSocket = new ServerSocket(port);//create server socket
serverSocket.setSoTimeout(15000);//socket closes after 15 seconds
this.start();
}
public void run() {
while (true) {//server runs infinitely______________
try {
System.out.println("ByteArrayServer: Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket servedClient = serverSocket.accept();//client socket
System.out.println("ByteArrayServer: connected to " + servedClient.getRemoteSocketAddress());
//Receive Client ByteArray___________________________________________
ByteEncodeSubscriptionReq receivedReq=new ByteEncodeSubscriptionReq();//server side POJO
System.out.println("ByteArrayServer: created SubscriptionReq Object");
InputStream PojoStreamHolder = servedClient.getInputStream();
System.out.println("ByteArrayServer: client InputStream received");
byte[] clientByteStream=new byte[44];//same size as Pojo byte requirement
_____/*MY CODE IS STUCK SOMEWHERE HERE*/__________
servedClient.getInputStream().read(clientByteStream);
System.out.println("ByteArrayServer: clientByteStream received: "+clientByteStream[0]+" "+clientByteStream[1]);
receivedReq=ByteEncodeSubscriptionReq.decode(clientByteStream);
//Send confirmation to Client__________________________________________________
DataOutputStream out = new DataOutputStream(servedClient.getOutputStream());
if(receivedReq.getRequestSymbol().trim().length()!=0){
out.writeUTF("ByteArrayServer received Subscription ID="+receivedReq.getSubscriptionID());
System.out.println("ByteArrayServer: new SubscriptionRequest ID="+receivedReq.getSubscriptionID()+" Subscriber_Name="+receivedReq.getSubscriberName());
}else{
out.writeUTF("ByteArrayServer: did not receive Subscription ID");
}
//Close Client socket_________________________________________________________
//server.close();
//serverSocket.close();
} catch (SocketTimeoutException s) {
System.out.println("PojoServer: Socket timed out after " + getTimeElapsedInSeconds(startTime) + " seconds from start");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
// int port = Integer.parseInt(args[0]);//to get port as an Argument
int port = 6060;
try {
Thread t = new ByteArrayServer(port);
startTime = System.currentTimeMillis();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is the Server console output:
ByteArrayServer: Waiting for client on port 6060...
ByteArrayServer: connected to /127.0.0.1:64233
ByteArrayServer: created SubscriptionReq Object
ByteArrayServer: client InputStream received
The issue is that while the Stream is received by the server without errors, it gets stuck near servedClient.getInputStream().read(clientByteStream); method and does not proceed further.
I've also tried
int count=servedClient.getInputStream().read(clientByteStream);
and
DataInputStream in = new DataInputStream(servedClient.getInputStream());
long bStr=in.readLong();
and
ObjectInputStream PojoObjHolder = new ObjectInputStream(PojoStreamHolder);
byte[] clientByteStream2 = (byte[])PojoObjHolder.readObject();
..but they show the same problem as well.
How should I pass the Byte Array between the two classes without extra imports?
The problem was in my ByteArrayClient Class. I had to link the OutputStream with the client socket, rather than creating a new instance of it. So I replaced:
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.write(SubscripReqByteArray);;
with
OutputStream os = client.getOutputStream(out);
os.write(SubscripReqByteArray);;
Thank you for the hint Ekant
DataInputStream.readFully(byte[] b) will finish only when in inputstream bytes till b.length available. So for sure you need to debug if you have all the bytes or not.And the solution is to make those byte available so that the function will finish.
Same for DataInputStream.read(byte[] b) The method is blocked until input data is available.
Please make sure by debugging your app that inputstream have 44 bytes.
Try below to count available bytes and you can read those easily.
// count the available bytes form the input stream
int count = is.available();
// create buffer
byte[] bs = new byte[count];
// read data into buffer
dis.read(bs);

SocketException - 'Socket is closed' even when isConnected() returns true

I'm making and android app that uses socket communication to send an image to a java app running on a computer
Here's what's happening : The desktop is running a server java application, the client android application is run on the device, it transfers an image to the server and that part goes well. After that the server app takes in a line from the console and passes it back to the android app. TILL that message is received by the android app, it is supposed to show a progress dialog box and it gets stuck there. The android app is supposed to read the string passed by the desktop app using readLine() but when I try to open the inputstream over the socket in the android app it gives me the exception.
Following are the codes, first for the desktop server, then for the android client
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
class ProjectServer
{
ServerSocket serSock;
Socket sock;
BufferedReader in;
PrintWriter out;
public static void main(String ar[])
{
try
{
ProjectServer cs=new ProjectServer();
cs.startServer();
}
catch(Exception e)
{
}
}
public void startServer()
{
try
{
serSock=new ServerSocket(8070);
System.out.println("Waiting for client...");
sock=serSock.accept();
System.out.println("Connections done");
//Accept File
System.out.println("Connected");
System.out.println(sock.isConnected()+"1");
//receive code
int filesize=450660;
int bytesRead;
int current=0;
// receive file
byte [] mybytearray = new byte [filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\\Project Server\\Capture.png");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
System.out.println("end-start");
bos.close();
//sock.close();
//receive code ends
//System.out.println(br.readLine());
//Matlab computation
//Send result
System.out.println(sock.isConnected()+"2");
PrintWriter pr=new PrintWriter(sock.getOutputStream(),true);
pr.println((new BufferedReader(new InputStreamReader(System.in))).readLine());
System.out.println(sock.isConnected()+"3");
(new BufferedReader(new InputStreamReader(System.in))).readLine();
System.out.println(sock.isConnected()+"4");
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
}
Android Client:
package com.site.custom;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Act2 extends Activity
{
private ProgressDialog pd;
private String serverIP="58.146.100.187";
private BufferedReader in;
private PrintWriter out;
private String path;
private Socket cliSock;
public void onCreate(Bundle onCreateInstance)
{
super.onCreate(onCreateInstance);
setContentView(R.layout.act2);
this.setTitle("This has started");
path=getIntent().getStringExtra("path");
//Establish Connection
//pd=ProgressDialog.show(this, "Establishing connection", "Finding server",false,true);
try
{
cliSock=new Socket(serverIP,8070);
//pd.dismiss();
//Log.v("MERA MSG","changing text");
((TextView)findViewById(R.id.tview)).setText(path);
}
catch(Exception e)
{
Log.v("MERA MSG",e.toString());
}
//Send file
//Log.v("MERA MSG","changing text1");
ProgressDialog pd=ProgressDialog.show(this, "Sending image", "Image chosen:"+path.substring(path.lastIndexOf("//")+1),false,true);
//Log.v("MERA MSG","changing text2");
try
{
File myFile = new File (path);
System.out.println((int)myFile.length());
byte[] mybytearray = new byte[450560];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = cliSock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
os.close();
bis.close();
//sock.close();
//System.out.println("Completed");
System.out.println(cliSock.isConnected()+"1");
pd.dismiss();
//System.out.println("Done");
System.out.println(cliSock.isConnected()+"2");
//Show dialog box till computation results arrive
pd=ProgressDialog.show(this, "Recognizing...", "(waiting for server reply)",false,true);
System.out.println(cliSock.isConnected()+"3");
in=new BufferedReader(new InputStreamReader(cliSock.getInputStream()));
System.out.println(in.readLine());
pd.dismiss();
}
catch(Exception e)
{
Log.v("MERA MSG",e.toString());
e.printStackTrace();
}
}
}
And btw here are the logs from LogCat:
http://pastebin.com/atHMycTa
You can confirm that the socket is still connected here as it shows true on lines 350 and 349
Look at line 351 for the error.
isConnected() only tells you whether you have ever connected this Socket, which you have, so it returns true. It is not an indication of the state of the connection.

Simple Java HTTP-Proxy with Sockets gets stuck without error message

I'm trying to get a simple multithreaded proxy to work in Java. However I don't manage to get the webpage show up in my browser, after the first GET request and the response from the webpage, the program is just stuck (as you can see from my code, I'm printing everything i get on the stdout for debugging, and there I see the sourcecode of the webpage, however after printing out "After Client Write", nothing happens (no exception, just nothing...)).
import java.net.*;
import java.util.*;
import java.io.*;
public class Proxy
{
public static void main(String[] args)
{
try
{
ServerSocket listensocket = new ServerSocket(Integer.valueOf(args[0]));
while(true)
{
System.out.println("wait");
Socket acceptsocket = listensocket.accept(); // blocking call until it receives a connection
myThread thr = new myThread(acceptsocket);
thr.start();
}
}
catch(IOException e)
{
System.err.println(">>>>" + e.getMessage() );
e.printStackTrace();
}
}
static class myThread extends Thread
{
Socket acceptsocket;
int host, port;
String url;
myThread(Socket acceptsocket)
{
this.acceptsocket=acceptsocket;
}
public void run() {
try
{
System.out.println("hello");
Socket client = acceptsocket;
//client.setSoTimeout(100);
InputStream clientIn = client.getInputStream();
//BufferedInputStream clientIn=new BufferedInputStream(clientis);
OutputStream clientOut = client.getOutputStream();
System.out.println("hello");
String clientRequest = readStuffFromClient(clientIn); // parses the host and what else you need
System.out.print("Client request: -----\n"+clientRequest);
Socket server;
server = new Socket("xxxxxxxxxxxxx" , 80);
InputStream serverIn = server.getInputStream();
//BufferedInputStream serverIn=new BufferedInputStream(serveris);
OutputStream serverOut = server.getOutputStream();
serverOut.write(clientRequest.getBytes());
serverOut.flush();
String serverResponse = readStuffFromClient(serverIn);
System.out.print("Server Response: -----\n"+serverResponse);
clientOut.write(serverResponse.getBytes());
clientOut.flush();
System.out.println("After Client Write");
clientIn.close();
clientOut.close();
serverIn.close();
serverOut.close();
server.close();
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
private String readStuffFromClient(InputStream clientdata)
{
ByteArrayOutputStream response = new ByteArrayOutputStream();
StringBuffer request=new StringBuffer(8192);
int i, httpstart,n=-1 ;
byte[] buffer = new byte[8192];
System.out.println("beforetry");
try
{
while((n = clientdata.read(buffer))!=-1)
{
System.out.println("before");
response.write(buffer,0,n);
//response.flush();
}
request=new StringBuffer(response.toString());
/*System.out.println("new:"+n+" "+ request.toString());
System.out.println("End client data");*/
}
catch (Exception e)
{
System.out.println("here");
System.out.println(e);
e.printStackTrace();
i = -1;
}
System.out.println("End manipulation method");
return request.toString();
}
}
}
(this is a stripped down not working example of my program, from the comments one can see I already tried to use BufferedInputStream). In general, this program is very unresponsive even for the first GET request from the browser. When I only read the clientdata once (not in a loop), I get a little bit further, e.g. get more GET/Response pairs, but at some point the program still gets stuck.
Somehow I think either I've a real trivial error I just don't manage to see, or the program should work, but simply doesn't for no real reason.
Any help is appreciated, thanks in advance!
You need two threads: one to read from the client and write to the server, and one to do the opposite, for each accepted socket. There is a further subtlety: when you read EOS from one direction, shutdown the opposite socket for output, and then if the input socket for that thread is already shutdown for output, close both sockets. In both cases exit the thread that read the EOS.
Try getting first the OutputStream and then the InputStream!
InputStream clientIn = client.getInputStream();
OutputStream clientOut = client.getOutputStream();
change it to:
OutputStream clientOut = client.getOutputStream();
InputStream clientIn = client.getInputStream();
This will make it work:
It will check if there is more data available to read
Still, it's important that you use BufferedIS because I think ByteArrayIS doesn't implement available method.
BufferedInputStream bis = new BufferedInputStream(clientdata);
System.out.println("beforetry");
try {
while(bis.available() > 0){
n = bis.read(buffer);
response.write(buffer, 0, n);
}

why doesn't my client read correctly the outputStream sent by the Server?

It's a simple client-server where Server using a BufferedWriter object would send to the Client receiving in the object BufferedReader.
When I use OutputStream and PrintWriter for the Server and InputStream and Scanner for the Client it works well.
What happens is that the client in Buffered way reads -1 if I'm sending an int and null for String.
I hope my English makes sense. ;P
That's the code:
Server:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8189);
Socket incoming;
incoming = server.accept();
try {
// OutputStream output = incoming.getOutputStream();
// PrintWriter outStream = new PrintWriter(output, true /*autoFlush*/);
// outStream.println("ENTER");
BufferedWriter output = new BufferedWriter(new
OutputStreamWriter(incoming.getOutputStream()));
output.write(3);
System.out.println("\nSent");
} finally {
incoming.close();
}
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
//Client theClient= new Client();
Socket RTSPSocket;
int ServerPort = 8189;
//server name address
String ServerHost = "localhost";
//get Server IP Address
InetAddress ServerIPAddress = InetAddress.getByName(ServerHost);
RTSPSocket = new Socket(ServerIPAddress, ServerPort);
try {
/*
InputStream input = theClient.RTSPSocket.getInputStream();
Scanner in = new Scanner(input);
String line = in.nextLine();
System.out.println(line);
*/
BufferedReader input = new BufferedReader(new
InputStreamReader(RTSPSocket.getInputStream()));
//String line = input.readLine();
//System.out.println("\nRicevuto: " + line);
System.out.println(input.read());
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
try putting the following code after output.write(3);
output.flush();
The BufferedOutputStream is built to only send the data when the buffer is full (I believe the default 1024 bytes). So, to force the data to be sent you need to flush the stream.
You have to flush data to receive them in the client part.
output.write(3);
output.flush();
System.out.println("\nSent");
When you have an OutputStream (or a Writer) you have to flush your data, this way you're 100% sure that what you wanted to be send has been sent.
Most (if not all) the OutputStream subclasses use a "mini buffer" which is flushed only when it's full or you manually call flush(). In your case, it's even more flagrant because you're using a BufferedWriter.
Another thing, when you use streams/writers, you really should close them after you're finished, one of the main thing close() do (most of the time) is flushing the last data remaining in the "mini buffer".
Resources :
Javadoc - OutputStream.flush()

Categories