socket问题
大家帮我看下这个程序有没有问题,为什么我测试时什么从客户端的控制台输入,服务端的控制台没有输出呢客户端
import *;
import *;
public class ClientSide {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
public ClientSide()
{
try{
socket=new Socket(InetAddress.getByName("localhost"),4700);
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
}
catch(IOException e){
System.out.println("Build Socket Error:"+e);
System.exit(1);
}
}
public void ClientStart()
{
try{
BufferedReader readConsole=new BufferedReader(new InputStreamReader(System.in));
String str;
while((str=readConsole.readLine())!=null){
writer.write(str);
str=reader.readLine();
System.out.println(str);
}
reader.close();
writer.close();
socket.close();
}
catch(IOException e){
System.out.println("Error while client start:"+e);
}
}
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
ClientSide client=new ClientSide();
client.ClientStart();
}
}
服务端
import *;
import *;
public class ServerSide {
private ServerSocket serversocket;
private Socket socket;
public ServerSide(){
try{
serversocket=new ServerSocket(4700);
}
catch(IOException e){
System.out.println("Build ServerSocket Error:"+e);
System.exit(1);
}
}
public void ServerStart() throws IOException{
try{
while(true){
socket=serversocket.accept();
new ServerThread(socket);
}
}
catch(IOException e){
System.out.println("ServerSocket can't accept a socket:"+e);
}
finally{
socket.close();
serversocket.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
ServerSide server=new ServerSide();
server.ServerStart();
}
catch(IOException e){
System.out.println("Can't no close ServerSocket");
}
}
}
class ServerThread extends Thread{
private BufferedReader reader;
private PrintWriter writer;
public ServerThread(Socket soc){
try{
reader=new BufferedReader(new InputStreamReader(soc.getInputStream()));
writer=new PrintWriter(new OutputStreamWriter(soc.getOutputStream()),true);
start();
}
catch(IOException e){
System.out.println("Bulid ServerThread Error:"+e);
System.exit(1);
}
}
public void run() {
try{
String str;
while((str=reader.readLine())!="BYT"){
writer.write(str);
System.out.println(str);
}
reader.close();
writer.close();
}
catch(IOException e){
System.out.println("Read ClientSide Input Error:"+e);
}
}
}