一个关于Java Socket的问题,大家看一下:
我的目的是从client端发出一段字符串,server端接收后原样返回给客户端。
客户端:ConnClient 服务器端ConnSerer(完整源代码附在最后)
===client端===
建了一个out流和in流:
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter Clientout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
向server端输出:clientOut.println("hello"); clientout.close();
等待服务器端返回代码片段:
StringBuffer sbuf = new StringBuffer();
char[] buff = new char[10];
int iLen = in.read(buff);
while (iLen>0) {
sbuf.append(buff,0,iLen);
if (sbuf.toString().trim().equals("hello")){
System.out.println(sbuf.toString());
break;
}
iLen = in.read(buff);
}
===server端===
同样建了一个in流和out流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter Serverout = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
接收client端信息和原样返回信息:
StringBuffer sbuf = new StringBuffer();
char[] buff = new char[10];
int iLen = in.read(buff);
while(iLen>0){
sbuf.append(buff,0,iLen);
if (sbuf.toString().trim().equals("hello")){
System.out.println("Echoing: " + sbuf.toString());
serverout.println(sbuf.toString()); //此处为server端输出流,返回给client端
break;
}
iLen = in.read(buff);
}
问题来了:
1、如果按以上代码运行,server端可以收到client的信息,但client端却收不到返回的信息。为什么?
2、后来我把client端的clientout.close()注释掉后,一切正常,client端能收到server端返回的信息。这又为什么?
难道server端和client端用的是一个out流?为什么client端把out流关掉后(clientout.close()),server端就不能返回信息呢?
以我以前的理解,server端和client端建的两个输出流应该是独立的。不会因为我把client端的out流关掉后,server端out流就不能返回了?不会吧!呵呵!
还有一个问题:server端怎么判断client端数据已经发送完毕?
一开始,我用clientout.print("hello"),运行后,server端显示已经连接成功但一直侦听,没有收到信息,认为client端还没有发送完成。
后来,我改用clientout.println("hello"),运行后,一切正常,服务器端也收到信息(hello)。
从上看出server的地判定就是一个回车符,不知道这样认识对不对?大家指点!
经过我试验,如果client端print后,用clientout.close()也可以使server端接收到信息。但却不能返回信息。也就是我发现上面说述的问题来由。
以上几个问题,请大家和banq大哥多加指点!
源代码如下:
client端:
package com.socket;
import java.net.*;
import java.io.*;
public class ConnClient {
public static void main(String[] args) throws IOException {
InetAddress addr = InetAddress.getByName(null);
System.out.println("addr = " + addr);
Socket socket = new Socket(addr, 8080);
//socket.setSoTimeout(5000);
try {
System.out.println("socket = " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter clientout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
String strSend = "hello";
clientout.println(strSend);
//clientout.println("\r\n");
//clientout.println();
//clientout.close();
StringBuffer sbuf = new StringBuffer();
char[] buff = new char[10];
int iLen = in.read(buff);
while (iLen>0) {
sbuf.append(buff,0,iLen);
if (sbuf.toString().trim().equals(strSend)){
System.out.println(sbuf.toString());
break;
}
iLen = in.read(buff);
}
}catch(Exception e){
e.printStackTrace();
} finally {
System.out.println("closing...");
socket.close();
}
}
}
server端代码:
package com.socket;
import java.io.*;
import java.net.*;
public class ConnServer {
public static final int PORT = 8080;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started: " + s);
try {
Socket socket = s.accept();
try {
System.out.println("Connection accepted: "+ socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter serverout = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
StringBuffer sbuf = new StringBuffer();
char[] buff = new char[10];
iLen = in.read(buff);
while(iLen>0){
sbuf.append(buff,0,iLen);
if(sbuf.toString().trim().equals("hello")){
System.out.println("Echoing: " + sbuf.toString());
serverout.println(sbuf.toString());
}
iLen = in.read(buff);
}
}catch(Exception e) { e.printStackTrace();}
finally {
System.out.println("closing...");
socket.close();
}
} finally {
s.close();
}
}
}