1
HI我在Java中遇到了與從客戶端寫入Web服務器相關的問題。我使用下面的代碼,請幫我在哪裏,我錯了如何在java中的web服務器上編寫
對於服務器類
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();
}
}
}
以及客戶端類實際上是小應用程序,有如下因素代碼
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(){}
}
現在,當我運行服務器然後服務器只顯示以下內容
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
但它沒有寫co我寫PrintWriter 的下一個對象,然後進入等待狀態的內容
另一方面,小程序剛剛啓動時才啓動。當我關閉服務器程序,然後小應用程序提供了以下異常
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)
請告訴什麼是我錯了
我認爲這將有助於代碼格式化,但如果你想這樣做,我沒有經過並縮進你的源代碼。 – 2011-01-30 17:04:39