在我的客户端类中,我没有得到任何错误,但是当代码运行方法setUpStreams()时,它会正确初始化objectoutputstream,并且消息“ Output Stream Setup”出现在我的JFrame上,但是之后什么都没发生,只是卡住了初始化objectinputstream。我要做什么才能使objectinputstream初始化。我已经附上了客户端类和服务器类的代码。谢谢。
public class client5 extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String serverIP;
private Socket connection;
public client5(String host) {
super("Client Program");
serverIP=host;
userText= new JTextField();
userText.setEditable(false);
userText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
userText.setText("");
}
});
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow), BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
startRunning();
}
public void startRunning() {
try{
connectToServer();
setupStreams();
whileChatting();
}
catch(EOFException eofException) {
showMessage("\n Connection Terminated");
}
catch(IOException ioException) {
ioException.printStackTrace();
}
finally {
closeCrap();
}
}
private void connectToServer() throws IOException{
showMessage("Attempting Connection...");
connection = new Socket(InetAddress.getByName(serverIP), 6789);
showMessage("Connected to " + connection.getInetAddress().getHostName());
}
private void setupStreams(){
try {
output = new ObjectOutputStream(connection.getOutputStream());
output.reset();
output.flush();
showMessage("\nOutput Stream Setup");
input = new ObjectInputStream(connection.getInputStream());
showMessage("\nInput Stream Setup");
}
catch(Exception e) {
e.printStackTrace();
}
}
private void whileChatting() throws IOException{
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}
catch(ClassNotFoundException error) {
showMessage("\n Unknown Object Type");
}
}while(!message.equals("SERVER - END"));
}
private void closeCrap(){
showMessage("\n Closing Shit");
ableToType(false);
try {
output.close();
input.close();
connection.close();
}
catch(IOException ioException) {
ioException.printStackTrace();
}
}
private void sendData(String message) {
try {
output.writeObject("CLIENT - " + message);
output.flush();
//showMessage("\n CLIENT -" + message);
}
catch(IOException ioException) {
chatWindow.append("\n Error Sending Message");
}
}
private void showMessage(final String mes) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
chatWindow.append(mes);
}
}
);
}
private void ableToType(final boolean tof) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
userText.setEditable(tof);
}
}
);
}
}
public class server5 {
private ServerSocket server;
private ArrayList<Socket> connections;
public server5(int port) {
try {
server=new ServerSocket(port);
connections=new ArrayList<Socket>();
}
catch(IOException e) {
e.printStackTrace();
}
runServer();
}
public void runServer() {
while(true) {
try {
Socket connection=server.accept();
connections.add(connection);
ServerThread serverThread=new ServerThread(connection,connections);
serverThread.start();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
private class ServerThread extends Thread{
private Socket connection;
private ObjectInputStream is;
private ObjectOutputStream os=null;
private ArrayList<Socket> connections;
public ServerThread(Socket socket, ArrayList<Socket> connections) {
this.connection=socket;
this.connections=connections;
}
public void run() {
setUpStreams();
try {
while(true) {
Object input=is.readObject();
if(input.getClass().equals(String.class)) {
sendData(input);
}
Thread.sleep(2);
}
}
catch(IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
closeStreams();
}
}
private void setUpStreams() {
try {
is = new ObjectInputStream(connection.getInputStream()) ;
}
catch(IOException e) {
e.printStackTrace();
}
}
private void closeStreams() {
try {
connection.close();
os.close();
is.close();
System.exit(0);
}
catch (IOException e) {
e.printStackTrace();
}
}
public void sendData(Object data) {
for(int i=0;i<connections.size();i++) {
try {
os=new ObjectOutputStream(connections.get(i).getOutputStream());
os.writeObject(data);
os.flush();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
}