启动Server后,一次次执行Client。“Just try”消息,有时可以接收到,有时不能。这是为什么啊?谁能告诉我··· 我觉得是缓冲区问题,我想知道具体原因。先谢过了!!!
import java.io.*;
import java.net.*;public class Server {
public static void main(String[] args)throws IOException {
int port = 8033;
ServerSocket server = new ServerSocket(port);
while (true) {
Socket sock = server.accept();
InputStreamReader in = new InputStreamReader(sock.getInputStream());
StringBuffer sb = new StringBuffer(8096); System.out.println("Handling client at " + sock.getRemoteSocketAddress());
boolean bRet = true;
while (bRet) {
if (in.ready()) {
int idx = 0;
while (idx != -1) {
idx = in.read();
sb.append((char)idx);
}
}else {
bRet = false;
}
}
in.close();
// 为什么交换前后两句的顺序,会出现不同的效果?
System.out.println(sb.toString());
}
}
}import java.io.*;
import java.net.*;public class Client {
public static void main(String[] args) {
byte[] data = "just try".getBytes();
try {
Socket sock = new Socket("127.0.0.1", 8033);
OutputStream out = sock.getOutputStream();
out.write(data);
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;public class Server {
public static void main(String[] args)throws IOException {
int port = 8033;
ServerSocket server = new ServerSocket(port);
while (true) {
Socket sock = server.accept();
InputStreamReader in = new InputStreamReader(sock.getInputStream());
StringBuffer sb = new StringBuffer(8096); System.out.println("Handling client at " + sock.getRemoteSocketAddress());
boolean bRet = true;
while (bRet) {
if (in.ready()) {
int idx = 0;
while (idx != -1) {
idx = in.read();
sb.append((char)idx);
}
}else {
bRet = false;
}
}
in.close();
// 为什么交换前后两句的顺序,会出现不同的效果?
System.out.println(sb.toString());
}
}
}import java.io.*;
import java.net.*;public class Client {
public static void main(String[] args) {
byte[] data = "just try".getBytes();
try {
Socket sock = new Socket("127.0.0.1", 8033);
OutputStream out = sock.getOutputStream();
out.write(data);
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
out.flush();
out.close();试过之后还是不行...
HELP HELP HELP
到底是服务端有问题还是客戶端??? 怎么看起来这么简单的问题,没人会呢??
你关闭流的时候,是先关闭 StringBuffer,然后再关闭InputStreamReader,你那个语句等于关闭了InputStreamReader再使用StringBuffer。
这个问题你既然可以接收到,就证明你程序是没有问题的。
我猜最大的原因就是你操作的问题,关闭的时候一定要关闭服务端和客户端,否则下一次使用的时候就有可能出错。
另外out.flush();是必须的。
从结果显示,server确实accept()得到客户端发来的socket。
如果是因为在传输的过程中client socket中的数据丢失了,可以有什么好办法来保证它正确地把数据传过去呢?
您有什么更好的意见吗?我很想学习java服务器端的编程,先谢过了!
http://blog.csdn.net/jia20003/article/category/1270931
文章里说道,对方收不到的原因可能有:发送方没有发送/r/n
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
byte[] data = "just try".getBytes();
try {
Socket sock = new Socket("127.0.0.1", 8033);
OutputStream out = sock.getOutputStream();
out.write(data);
out.write("/r/n".getBytes()); //这样加了之后,还是不可以。应该怎么改?
out.flush();
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
sb.append((char)idx);
你直接一个idx是返回数据的字节的长度,而不是数据,
你更本没有read数据到你的本地StringBuffer中,怎么会有结果啊
InputStreamReaderpublic int read()
throws IOException
Reads a single character.
Overrides: read in class Reader Returns:The character read, or -1 if the end of the stream has been reached Throws: IOException - If an I/O error occurs
Note that returning false does not guarantee that the next read will block.感觉应该是这个函数的bug 吧,或者说,不能用这个函数来作为是否有数据的依据。直接用in.read() 读即可。
public boolean ready() throws IOException
Tells whether this stream is ready to be read. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream. 谁能解释下这个红字的意思?就应该是它没理解清楚
判断此流是否已经准备好用于读取。如果其输入缓冲区不为空,或者可从底层字节流读取字节,则 InputStreamReader 已做好被读取准备。
什么是底层字节流??怎么个情况···
这篇文章说的不错!
ready()是一种非阻塞的访问,所以不等待就去检查是否有数据流。
read() 是一种阻塞方法,直到有数据过来。
So,什么情况下用非阻塞的呢?它的好处何在?
大家一起来讨论啊!!!