Software Connection reset 和Software caused connection abort: recv failed 的一些错误

来源:互联网 发布:安卓电话录音软件 编辑:程序博客网 时间:2024/06/17 00:24
除了那些我在网上搜集的造成的因素,例如一端关闭,另一端没关闭;数据正发送时Server关闭,而Client无法从输入流中读取,还有什么数据库的服务,还有什么IIS的服务需要关闭。

                 个人的理解,如果按照规范或者书上例子编写,错误可能有如下,虽然根本原因和上述是相同的,但具体的导致原因可能不同。

                 客户端,发送数据时的结束符,一般以“\n”结束,如果忘记使用可能会产生该错误,或者一直等待,对于本地的服务器,自己编写的,加一个就可以了,但是如果对网络上的服务器,必须加两个。

                端口的占用,比如你正在开着浏览器,结果编程序使用80端口,必然会导致此错误的产生。

                服务器或者客户端的文件目录,当读写时,找不到正确的目录,并非一定会告诉你是IO错误,而可能反映是Socket错误,所以这个也要务必检查。

                关于在客户端和服务器,socket关闭,最容易产生此类错误,关闭的时机不对,就会出错,不关闭也会出错,例如,服务器端不关闭socket,服务器端不会报错,但是对于客户端来说,如果用infromstream读取,当数据发送完毕时,再次readline,便会出错,正常情况下,在服务器端关闭socket,此时客户端的读取会读取到null,而不会产生错误。

                就是这样的。

import java.io.*;
import java.util.*;
import java.net.*;

public class WebServer {

// the work content of the Server
final static String path = "F:\\work\\SSD8\\EX1-lab";


//the edition of the HTTP
final static String ipEdition = "HTTP/1.1";

//the communication port of the Server
final static int port = 80;

//main method to respond for a client
public static void main(String argv[]){

try{
System.out.println("The Server start!\n");

ServerSocket listenSocket = new ServerSocket(port);

while(true){
   Socket connectionSocket = listenSocket.accept();
   respondToClient(connectionSocket);
}


}catch(SocketException e){
   System.out.println("socket error in Server\n" + e.getMessage());
}catch(IOException e){
   System.out.println("io error in Server\n" + e.getMessage());
}
}


// method to respond cient, including to receice and send the data
public static void respondToClient(Socket connectionSocket) throws IOException,SocketException

{
String requestMessagegetline;
String filename;
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
requestMessagegetline = inFromClient.readLine();
StringTokenizer token = new StringTokenizer(requestMessagegetline," ");
if(token.nextToken().equals("GET")){
   filename = path + token.nextToken();
   File file = new File(filename);
   if(file.exists()){
    if(ipEdition.equals(token.nextToken())){
     int numOfBytes = (int)file.length();
     FileInputStream inFile = new FileInputStream(filename);
     byte[] fileInBytes = new byte[numOfBytes];
     inFile.read(fileInBytes);
   
     System.out.println("ready send the head\n");
     outToClient.writeBytes("HTTP/1.0 200 Document Follow\r\n");
     if(filename.endsWith("html"))
      outToClient.writeBytes("Content-Type: text/html\r");
     if(filename.endsWith(".gif"))
      outToClient.writeBytes("Content-Type: image/gif\r");
     if(filename.endsWith(".jpg"))
      outToClient.writeBytes("Content-Type: image/jpeg\r");
     outToClient.writeBytes("Content-length: " + numOfBytes + "\r\n");
     outToClient.writeBytes("\r\n");
    
     System.out.println("finish send the head\n");
     System.out.println("ready send the filedata\n");
     outToClient.write(fileInBytes,0,numOfBytes);
     System.out.println("finish the data send\n");
    }else{
     outToClient.writeBytes("HTTP/1.0 505 Version Not Supported\r\n");
    }
   }else{
    outToClient.writeBytes("HTTP/1.0 404 Not Found\r\n");
   }
}else{
   outToClient.writeBytes("HTTP/1.0 400 Bad Request\r\n");
}
System.out.println("closed\n");
connectionSocket.close();
}
}


import java.io.*;
import java.net.*;
import java.util.*;


//content:EX1,The client to send message to server to get the head and the data
public class TCPClient {

// the communication port number
final static int portNumber = 5678;

final String pathOfFile = "F:\\work\\SSD8\\EX1-lab\\";

// the data packet of the head from the Server
public String packetHead;


//the data in the packet of the entity
public String fileData;

// name of the Server
public String serverName;

//The IP proticol edition of the user's
public String ipEdition;

//main method to run the program
public static void main(String argv[]){

TCPClient client = new TCPClient();

String userRequest = null;

if(argv[0] != null)client.serverName = argv[0];
else return;

try{

   System.out.println("Enter the request for the Server:");
   BufferedReader inFromUser = new BufferedReader(
     new InputStreamReader(System.in));
   userRequest = inFromUser.readLine();
  
   if(client.connectToServer(userRequest))
    client.writeToFile();
  
   System.out.println("Finish");
  
}catch(SocketException e){
   System.out.println("socket error in Client!" + e.getMessage());
}catch(IOException e){
   System.out.println("io exception in Client!" + e.getMessage());
}
}


//initialize the data of Client
public TCPClient(){
packetHead = "";
fileData = "";
serverName = null;
ipEdition = null;
}

// write the data from the Sever to a specific file
public void writeToFile()throws IOException{
System.out.println("Enter the filename you want to save:");
BufferedReader inFromUser = new BufferedReader(
    new InputStreamReader(System.in));

File file = new File(pathOfFile + inFromUser.readLine());
FileWriter fileWrites = new FileWriter(file);
fileWrites.write(fileData);
fileWrites.close();
}

//create Socket to the Server and send it the request by user
// it will get a data packet, separate the data and initialize some valubal of the Client
public boolean connectToServer(String requestForServer)throws IOException{

Socket clientSocket = new Socket(serverName,portNumber);
if(clientSocket.isConnected()){
   DataOutputStream outToServer = new DataOutputStream(
     clientSocket.getOutputStream());
   BufferedReader inFromServer = new BufferedReader(
     new InputStreamReader(clientSocket.getInputStream()));

   outToServer.writeBytes(requestForServer + "\r\n" + "\r\n");

   System.out.println("connect!");
   System.out.println("From Server :");
  
   String S = inFromServer.readLine();
   while(S != null && !S.startsWith("<") ){
    packetHead += S + "\r";
    S = inFromServer.readLine();
   }
   System.out.println(packetHead);
  
   StringTokenizer token = new StringTokenizer(packetHead," ");
   token.nextToken();
   String conditionKey = token.nextToken();
  
   if(conditionKey.equals("200"))
   {
    while(S != null){
     System.out.println(S);
     fileData += S;
     S = inFromServer.readLine();
    }
    return true;
   }
   System.out.println("client is closed\n");
   clientSocket.close();
}
return false;
}

}

原创粉丝点击