In addition to the factors that I collect online, such as one end off, the other end not closed, the server shuts down while the data is being sent, and the client is unable to read from the input stream, what database services are available, and what IIS services need to be shut down.
Personal understanding, if written according to specifications or book examples, errors may have the following, although the root cause and the above are the same, but the specific causes may be different.
The client, send the data when the Terminator, generally end with "\ n", if forget to use may produce this error, or have been waiting for the local server, write their own, plus one on it, but if the server on the network, must add two.
Port occupancy, such as you are in the browser, the result of the program using 80 ports, will inevitably lead to this error.
Server or client file directory, when read and write, can not find the correct directory, not necessarily will tell you is an IO error, and may reflect the socket error, so this also must check.
On the client and the server, the socket is closed, the most likely to produce such errors, the timing is not right, there will be errors, do not close the error, for example, the server does not close the socket, server side will not error, but for the client, if read with Infromstream, When the data is sent out, again ReadLine, there will be errors, normally, on the server side close the socket, at this time the client read to NULL, and will not produce errors.
That's the way it is.
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 =;
//main 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 ());
}
}
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 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 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 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 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's request by user
It'll get a data packet, separate the data and initialize some valubal 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;
}
}