int allSize=21*1024;//21k int totalRead=0; int currentRead=0; byte[] bb = new byte[allSize]; //InputStream in while(totalRead<allSize) { currentRead=in.read(bb,totalRead,allSize); totalRead += currentRead; } 有出现无限循环的可能 如果有死循环需要在循环条件中判断in.available(),具体你自己试吧 这是很久以前写的东东了
Basically xiangqian's idea is right. What you need to do is to first find out the size of a message. If the size is fixed, you can just hard-code it. Otherwise, you have to put it in front of each message. Receiver read the int in the message, decide the size, then enter a loop to receive all bytes. UP's code may get into a dead loop because if the connection breaks, there may be a case that you can never get out of loop (in some case, exceptions will be thrown). You can set a timer to avoid dead loop though.Also, checking in.available() is not a good idea, because it returns a size of what available in the buffer, which is 0 in most of case, not bytes you should receive.
public static void main(String args[]) throws IOException { int c; Socket s1; InputStream s1In; DataInputStream dis;
// Open your connection to a server, at port 5432 // localhost used here s1 = new Socket("192.111.111.2",5432);
//set up the time out of read operation = 100s, if occured assume server dead. s1.setSoTimeout(10000);
// Get an input file handle from the socket and // read the input s1In = s1.getInputStream(); dis = new DataInputStream(s1In); while (true){ try { String st = new String (dis.readUTF()); System.out.println(st); }catch (InterruptedIOException ee){ System.out.println("Time out , Maybe Server dead or power down casually"); break; }catch (EOFException ee){ System.out.println("End of Stream"); break; }catch (IOException e){ System.out.println("IOException, May be server closed"); break; } }
// When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); } } import java.net.*; import java.io.*;public class SimpleServer { public static void main(String args[]) { ServerSocket s = null; Socket s1; String sendString = "Hello Net World!"; int slength = sendString.length(); OutputStream s1out; DataOutputStream dos; // Register your service on port 5432 try { s = new ServerSocket(5432); } catch (IOException e) { // ignore } // Run the listen/accept loop forever int count=0; while (true) { try { // Wait here and listen for a connection s1=s.accept(); // Get a communication stream associated with // the socket s1out = s1.getOutputStream(); dos = new DataOutputStream (s1out); // Send your string! // (UTF provides machine independence) while (System.in.available()==0){ dos.writeUTF(sendString); } // clear key board input while (System.in.available()!=0){ System.in.read(); } System.out.println("SocketClosed "+ count++); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } catch (IOException e) { // ignore } } } }
int totalRead=0;
int currentRead=0;
byte[] bb = new byte[allSize];
//InputStream in
while(totalRead<allSize)
{
currentRead=in.read(bb,totalRead,allSize);
totalRead += currentRead;
}
有出现无限循环的可能
如果有死循环需要在循环条件中判断in.available(),具体你自己试吧
这是很久以前写的东东了
当再server端按 enter键正常close掉socket,则在client端分别引起EOFExceptin
按ctrl+c相当于结束server,在client端IOException 以区别这两种情况,
直接关掉运行server的电源会引起client的等待,等待10秒钟后就会因超时退出。
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[])
throws IOException {
int c;
Socket s1;
InputStream s1In;
DataInputStream dis;
// Open your connection to a server, at port 5432
// localhost used here
s1 = new Socket("192.111.111.2",5432);
//set up the time out of read operation = 100s, if occured assume server dead.
s1.setSoTimeout(10000);
// Get an input file handle from the socket and
// read the input
s1In = s1.getInputStream();
dis = new DataInputStream(s1In); while (true){
try {
String st = new String (dis.readUTF());
System.out.println(st);
}catch (InterruptedIOException ee){
System.out.println("Time out , Maybe Server dead or power down casually");
break;
}catch (EOFException ee){
System.out.println("End of Stream");
break;
}catch (IOException e){
System.out.println("IOException, May be server closed");
break;
}
}
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
import java.net.*;
import java.io.*;public class SimpleServer {
public static void main(String args[]) {
ServerSocket s = null;
Socket s1;
String sendString = "Hello Net World!";
int slength = sendString.length();
OutputStream s1out;
DataOutputStream dos; // Register your service on port 5432
try {
s = new ServerSocket(5432);
} catch (IOException e) {
// ignore
}
// Run the listen/accept loop forever
int count=0;
while (true) {
try {
// Wait here and listen for a connection
s1=s.accept(); // Get a communication stream associated with
// the socket
s1out = s1.getOutputStream();
dos = new DataOutputStream (s1out); // Send your string!
// (UTF provides machine independence)
while (System.in.available()==0){
dos.writeUTF(sendString);
}
// clear key board input
while (System.in.available()!=0){
System.in.read();
} System.out.println("SocketClosed "+ count++); // Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close(); } catch (IOException e) {
// ignore
}
}
}
}