【发布时间】:2016-11-20 12:43:57
【问题描述】:
我正在尝试做一个简单的 TCPIP 客户端和服务器(为了测试,现在两者都在同一个服务器中)程序。服务器读取请求并发送回对请求的确认。 我遇到了两个问题
据我了解 BufferedReader.read() 应该读取字节 按字节计算,当没有更多数据可供读取时返回 -1。我愿意 看不到读数发生
BufferedReader.read() 永远不会完成读取循环。
为了解决1,当客户端发送请求时,我附加了一个空字符。现在我看到读取正在发生,但如果读取循环仍然等待更多数据,它不会出现。 (客户端程序中的第 96 行)。
为了解决 2,对于 read(),我停止检查 -1,并开始检查 > 0。 (服务器程序中的第 75 行)。
有人能解释一下为什么会有这种行为吗?
服务器程序
import java.net.*;
import java.io.*;
import java.util.concurrent.TimeUnit;
class TCPServer
{
ServerSocket serverSocket;
Socket clientSocket;
DataInputStream is;
DataOutputStream os;
BufferedReader br;
int Port;
TCPServer( int Port)
{
this.Port = Port;
try
{
serverSocket = new ServerSocket( Port );
}
catch( Exception E )
{
System.out.println( "Exception Socket : " + E );
}
System.out.println( "Server socket created" );
try
{
clientSocket = serverSocket.accept();
}
catch( Exception E )
{
System.out.println( "Exception accept : " + E );
}
System.out.println( "Client socket created and binded" );
}
public void initInOutStream()
{
try
{
is = new DataInputStream( clientSocket.getInputStream() );
br = new BufferedReader( new InputStreamReader( is ) );
}
catch( Exception E )
{
System.out.println( "Exception InputStream : " + E );
}
System.out.println( "input stream stream created" );
try
{
os = new DataOutputStream( clientSocket.getOutputStream() );
}
catch( Exception E )
{
System.out.println( "Exception OutputStream : " + E );
}
System.out.println( "output stream stream created" );
}
public void processTCPServer()
{
String clientRequest = null;
String ServerResponse = null;
byte[] clientRequestArray = {};
try
{
int intvalofchar = 0;
initInOutStream();
while( true )
{
StringBuilder sb = new StringBuilder( 512 );
System.out.println( "Waiting for request " );
while( ( intvalofchar = br.read() ) > 0 )
//while( ( intvalofchar = br.read() ) != -1 )
sb.append( (char) intvalofchar );
clientRequest = sb.toString();
System.out.println( "Client Request : " + clientRequest );
ServerResponse = "Ackn for [" + clientRequest + "]\0";
try
{
os.writeBytes( ServerResponse );
os.flush();
System.out.println( "Sent" );
}
catch( SocketException E )
{
System.out.println( "Exception " + E );
}
}
}
catch( Exception E )
{
System.out.println( "Exception readLine : " + E );
}
}
public static void main( String []args )
{
TCPServer obj = new TCPServer( 9999 );
obj.processTCPServer();
}
}
客户端程序
import java.net.*;
import java.io.*;
class TCPClient
{
Socket clientSocket;
DataInputStream is;
DataOutputStream os;
TCPClient( String IP, int Port)
{
try
{
clientSocket = new Socket( IP, Port );
}
catch( Exception E )
{
System.out.println( "Exception socket: " + E );
}
try
{
is = new DataInputStream( clientSocket.getInputStream() );
}
catch( Exception E )
{
System.out.println( "Exception InputStream : " + E );
}
try
{
os = new DataOutputStream( clientSocket.getOutputStream() );
}
catch( Exception E )
{
System.out.println( "Exception OutputStream : " + E );
}
}
public void processTCPClient()
{
String clientRequest = null;
String serverResponse = null;
try
{
while( true )
{
clientRequest = getData();
System.out.println( "Send [" + clientRequest + "]");
try
{
os.writeBytes( clientRequest );
}
catch( Exception E )
{
System.out.println( "Exception writeBytes : " + E );
}
System.out.println( "Data sent " + clientRequest );
System.out.println( "Waiting for response " );
try
{
BufferedReader brs = new BufferedReader( new InputStreamReader( is ) );
int intvalofchar = 0;
StringBuilder sb = new StringBuilder( 512 );
while( ( intvalofchar = brs.read() ) > 0 )
sb.append( (char) intvalofchar );
serverResponse = sb.toString();
System.out.println( "Server Response : " + serverResponse );
}
catch( Exception E )
{
System.out.println( "Exception readLine : " + E );
}
}
}
catch( Exception E )
{
System.out.println( "Exception readLine : " + E );
}
}
public String getData()
{
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
String request = null;
while( true )
{
System.out.println( "Enter : " );
try
{
request = br.readLine();
}
catch( Exception E )
{
System.out.println( "Exception readLine " + E );
}
return request+"\0";
//return request;
}
}
public static void main( String[] args)
{
TCPClient obj = new TCPClient( "localhost", 9999 );
obj.processTCPClient();
obj.getData();
}
}
【问题讨论】:
-
这里没有任何内容支持标题中关于在输入中要求 null 的标题声明。
标签: java sockets network-programming bufferedreader