socket通信程序中的问题
下面是两段程序,服务器端的和客户端的,现在的问题是:
当服务器端给客户端发数据之后,客户端好像接受不到,如果把//code for test之间的代码去掉,就可以实现客户端到服务器端的数据传送,难道逆向就不能了吗?
服务器段程序:
import java.io.*;
import java.net.*;
public class tcpServer {
public static void main(String[] args)
{
int port=1500;
ServerSocket server_socket;
BufferedReader input;
//code for test
BufferedReader input1;
PrintWriter output;
String lineToSend;
//code for test
try
{
server_socket=new ServerSocket(port);
System.out.println( "server listening on port: "+server_socket.getLocalPort());
while(true)
{
Socket socket=server_socket.accept();
System.out.println( "New connection at "+socket.getInetAddress()+ ": "+socket.getPort());
input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//code for test
input1=new BufferedReader(new InputStreamReader(System.in));
output=new PrintWriter(socket.getOutputStream());
//code for test
try
{
while(true)
{
//code for test
lineToSend=input1.readLine();
System.out.println(lineToSend);
if(lineToSend.equals( "end "))
{
System.out.println( "game over ");
break;
}
System.out.println( "begin ");
output.println(lineToSend);
System.out.println( "end ");
//code for test
String message=input.readLine();
if(message==null)
{
System.out.println( "message equals null ");
break;
}
System.out.println(message);
}
}
catch(
IOException e)
{
System.out.println(e);
}
try
{
socket.close();
System.out.println( "socket closed by client ");
}
catch(IOException e)
{
System.out.println(e);
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
客户端程序:
import java.io.*;
import java.net.*;
public class tcpClient
{
public static void main(String[] args)
{
int port=1500;
String server= "localhost ";
Socket socket=null;
String lineToSend;
BufferedReader input;
PrintWriter output;
int ERROR=1;
//code for test
BufferedReader input1;
//code for test
//connect to the server
try
{
socket=new Socket(server,port);
System.out.println( "connected with server "+socket.getInetAddress()+ ": "+socket.getPort());
}
catch(
UnknownHostException e)
{
System.out.println(e);
System.exit(ERROR);
}
catch(IOException e)
{
System.out.println(e);
System.exit(ERROR);
}
try
{