Dzień dobry,
Piszę aplikację klient - serwer.
Klient:
public class klient extends JFrame {
private JPanel contentPane;
private static BufferedReader in;
private static PrintWriter out;
private static Socket s;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
klient frame = new klient();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public klient() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNastepny = new JButton("nastepny");
btnNastepny.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
out.println("nastepny");
}
});
btnNastepny.setBounds(53, 28, 89, 23);
contentPane.add(btnNastepny);
JButton btnPoprzedni = new JButton("poprzedni");
btnPoprzedni.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
out.println("poprzedni");
}
});
btnPoprzedni.setBounds(63, 99, 89, 23);
contentPane.add(btnPoprzedni);
JButton btnKoniec = new JButton("koniec");
btnKoniec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
out.println("koniec");
}
});
btnKoniec.setBounds(53, 172, 89, 23);
contentPane.add(btnKoniec);
JButton btnPolacz = new JButton("polacz");
btnPolacz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
polacz();
}
});
btnPolacz.setBounds(312, 85, 89, 23);
contentPane.add(btnPolacz);
}
public void odczyt()
{
try {
while(true)
{
String inputLine = in.readLine();
if (inputLine != null) {
if(inputLine.equals("nast"))
System.out.println("odebralem od serwea nast");
if(inputLine.equals("pop"))
System.out.println("odebralem od serwea pop");
if(inputLine.equals("kon"))
System.out.println("odebralem od serwea kon");
}
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void polacz()
{
try {
s=new Socket("localhost",7777);
// System.out.println("wywoluje sie test");
in =new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream(),true);
odczyt();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Serwer:
public class serwer {
public static void main(String[] args) {
try {
ServerSocket s = new ServerSocket(7777);
Socket przychodzacy=s.accept();
PrintWriter out =
new PrintWriter(przychodzacy.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(przychodzacy.getInputStream()));
String inputLine="";
while(true)
{
inputLine = in.readLine();
if (inputLine != null) {
if(inputLine.equals("nastepny"))
{
System.out.println("odebralem od klienta nastepny");
out.println("nast");
}
if(inputLine.equals("poprzedni"))
{
System.out.println("odebrałem od klienta poprzedni");
out.println("pop");
}
if(inputLine.equals("koniec"))
{
System.out.println("odebrałem od klienta koniec");
out.println("kon");
}
}
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Zastanawia mnie dlaczego przyciskając dowolny przycisk po stronie klienta, nie dociera on do serwera chociaż ten ciągle nasłuchuje. Generalnie zależy mi na tym, aby po naciśnięciu przycisku serwer odpowiednio reagował.