主题:大家帮帮忙,看看为什么总是这样······
之前也出想过问题,就是连接以后,不能进行输出输入了(不能互相通信,但是连接上了)。大神们帮忙看看是什么问题,小弟谢过啦!!!!!
import java.io.*;
import java.net.*;
/**
* 服务端类
* */
public class ServerStart {
public static void main(String[] args){
ServerSocket server = null;
try{
//1.建立连接
server = new ServerSocket(9000);
while(true){
Socket client = server.accept();
System.out.println("建立连接");
//2.获取流对象
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
//3.传输数据
while(true){
String flag = (String)in.readObject();
System.out.println("客户端说:"+flag);
if(flag == null){
break;
}
String str = ins.readLine();
out.writeObject(str);
out.flush();
}
//4.关闭流
client.close();
in.close();
out.close();
}
}catch(Exception e){
e.getStackTrace();
}
}
}
import java.io.*;
import java.net.Socket;
/**
* 客户端类
* */
public class ClientStart {
public void isNameExist(){
Socket client = null;
try{
//1.建立连接
client = new Socket("127.0.0.1",9000);
//2.获取流对象
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
//3.传输数据
while(true){
String str = ins.readLine();
out.writeObject(str);
out.flush();
if(str == ""||str.equals("exit")){
break;
}
String flag = (String)in.readObject();
System.out.println("服务器说:"+flag);
}
}catch(Exception e ){
e.getStackTrace();
}finally{
//4.关闭流
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args){
ClientStart c = new ClientStart();
c.isNameExist();
}
}