Except for the factors that I collect online, such as closing one end and not closing the other end. When the data is being sent, the Server closes and the Client cannot read the data from the input stream, what database services are there, and what IIS services need to be closed.
In my personal understanding, if you write errors according to the specifications or examples in the book, the root cause may be the same as the above, but the specific cause may be different.
The end character of the client when sending data. Generally, it ends with "\ n". If you forget to use it, this error may occur or you may keep waiting. For the local server, you can write it yourself, you can add one, but you must add two to the servers on the network.
Port usage. For example, if you are using a browser and the result editor uses port 80, this error will inevitably occur.
The file directory of the server or client does not necessarily tell you that the correct directory is not found during read/write, but may be reflected as a Socket error. Therefore, check this.
When the socket is disabled on the client and server, this type of error is most likely to occur. If the closing time is incorrect, an error will occur. If you do not close the socket, for example, the server does not close the socket, the server does not report an error. However, if the client reads data using infromstream, when the data is sent again, it will encounter an error. Normally, the socket is disabled on the server, in this case, the client reads null without generating an error.
That's it.
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;
}
}