Mam problem z komunikacją pomiędzy serwerem a klientem.
Program na razie powinien:
Wysłać wiadomość start przy połączeniu (działa)
Oczekiwać na wiadomość od klienta i po jej otrzymaniu wysłać ją z powrotem (tutaj jest problem)
wszystko wyświetlam w swingu
po wpisaniu wiadomości i naciśnięciu guzika send (wpisuje do PrintWriter(clientSocket.getOutputStream(), true) wiadomość którą użytkownik wpisał w textfieldzie) nic się nie dzieje
jednakże jeśli debuguje program linia po linijce ... wszystko działa jak powinno i wysyła z powrotem do klienta to co wysłał. O co chodzi ? jak to jest możliwe? I jak to naprawić?
Server:
package server.model;
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(1111);
} catch (IOException e) {
System.err.println("Could not listen on port: 1111.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
outputLine = "Start"; //wiadomosc start
out.println(outputLine); //i jej wyslanie do klienta
while ((inputLine = in.readLine()) != null) { //czeka na wiadomosc
out.println(inputLine);
if (inputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
Klient
package server.widok;
import persons.view.View;
import persons.view.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
public class Client {
static String fromUser;
static Window window;
public static void main(String[] args) throws IOException {
window = new Window();
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
boolean listening;
try {
kkSocket = new Socket("localhost", 1111);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}
String fromServer;
window.sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //wyslanie wiadomosci
fromUser = window.textField.getText(); //kasowanie textfielda
window.textField.setText(""); //i focus na niego z powrotem
window.textField.requestFocus();
}
});
boolean list = true;
while(list) //petla glowna
if(in.ready()) //jesli jest cos w in to przechodi dalej
while ((fromServer = in.readLine()) != null) {
window.textArea.append("Server: " + fromServer + "\n");
if (fromServer.equals("Bye.")) {
break;
}
listening = true;
while (listening) {
if (fromUser != null) {
window.textArea.append("Client: " + fromUser + "\n");
out.println(fromUser);
listening = false;
fromUser = null;
}
}
}
out.close();
in.close();
kkSocket.close();
}
}
Widok w swingu:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package persons.view;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* @author Piotrek
*/
public class Window {
public JFrame frame;
public JButton sendButton;
public JTextArea textArea;
public JTextField textField;
public Window(){
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JPanel send = new JPanel();
textField = new JTextField(10);
sendButton = new JButton("Send");
send.add(textField);
send.add(sendButton);
textArea = new JTextArea(10,10);
panel.add(textArea,BorderLayout.CENTER);
panel.add(send,BorderLayout.SOUTH);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}