2

HI I am having problem in java related to write on the web server from the client. I am using following code please help me where I am wrong

For Server class

import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.util.Scanner;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class Server
{
    public static void main(String args[]){
        try{
            ServerSocket s = new ServerSocket(8080);
            System.out.println("ashish");

            Socket incoming = s.accept();
            System.out.println("the status of the socket="+incoming.isConnected());
            InputStream inStream = incoming.getInputStream();

            InputStreamReader isr=new InputStreamReader(inStream);
            BufferedReader br=new BufferedReader(isr);
            String str=null;
            while((str=br.readLine()).equals("exit")==false){
                System.out.println(str);
            }

            System.out.println("System exit");
        }catch(Exception e){
            e.printStackTrace();
        }    
    }
}

And for the client class which is actually applet have folowing code

import java.applet.Applet;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class testApplet extends Applet
{

    public void init(){}

    public void start(){
        try{
            URL url = new URL("http://localhost:8080"); 
            HttpURLConnection httpCon = (HttpURLConnection)url.openConnection(); 
            httpCon.setDoOutput(true); 
            httpCon.setRequestMethod("PUT");

            PrintWriter out = new PrintWriter( httpCon.getOutputStream()); 
            out.print("Resource content\n");
            out.print("exit"); 
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        } 
    }

    public void stop(){}

    public void destroy(){}
}

Now when I run the server then the server display only the following

PUT / HTTP/1.1
User-Agent: Mozilla/4.0 (Windows Vista 6.1) Java/1.6.0
Host: localhost:8080
Accept: text/html, image/gif. image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

But it did not write the content which I write by the out object of PrintWriter and next goes into wait state

on the other hand the applet does not started just initiated. and when I close server program then applet gives following exception

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at testApplet.start(testApplet.java:19)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Please tell what's wrong with me

Rais Alam
  • 6,970
  • 12
  • 53
  • 84
agarwal_achhnera
  • 2,582
  • 9
  • 55
  • 84
  • I thought it would help to have code formatted, but I didn't go through and indent your source code, if you want to do that. – James Black Jan 30 '11 at 17:04
  • Where does your applet ever try to send data to the server? It seems the client is just waiting for the server to send data to it. – James Black Jan 30 '11 at 17:08
  • I only have time to give it a very quick look, but I don't see that you're calling flush() after your write calls. Also, you are not sending a new line after writing "exit", so the readLine() call in the server will be hung waiting for that. – jhouse Jan 30 '11 at 17:29
  • This question seems to be highly related: http://stackoverflow.com/questions/4844535/why-do-you-have-to-call-urlconnectiongetinputstream-to-be-able-to-write-out-to-u – jhouse Jan 30 '11 at 18:11

1 Answers1

6

Try

httpCon.setChunkedStreamingMode(0);

before writing to outputstream. You should always do this on http connections if content length is not known in advance.

Howard
  • 38,639
  • 9
  • 64
  • 83