Witam w ramach nauki programowania sieciowe w Javie chcialem napisac prostego czata do komunikacji dwoch komputerow. Mam dwie proste aplikacje wygladajace podobnie z tym ze jedna udostepnia port i oczekuje na przylaczenie klienta.. druga to klient ktory podaje IP serwera i nr udostepnionego portu..., po palaczeniu w oknie dialogowym serwera pokazuje sie napisz "wszystko ok" informujacy o polaczeniu.
Mam problem z implementacja opcji wymiany pomiedzy informacji.
Ze wzgledu ze jestem poczatkujacy w tym temacie prosze o wyrozumialosc i pomoc :-)
Wklejam kod aplikacji serwera i klienta:
Serwer:
import javax.swing.JFrame;
public class ChatSerwer {
public static void main (String[] args)
{
ChatSerwerFrame frame = new ChatSerwerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//-----------------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatSerwerFrame extends JFrame
{
public ChatSerwerFrame()
{
setSize(500,500);
setTitle("Server");
panel = new JPanel();
panel.setLayout(null);
label_port = new JLabel("Port :");
label_port.setBounds(10, 10, 50, 25);
panel.add(label_port);
text_port = new JTextField(15);
text_port.setBounds(60, 10, 110, 25);
text_port.setText("8189");
panel.add(text_port);
button_port = new JButton("Set");
button_port.setBounds(200, 10, 60, 25);
button_port.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
port = Integer.parseInt(text_port.getText());
text_port.setEditable(false);
}
});
panel.add(button_port);
label_message = new JLabel("Text:");
label_message.setBounds(10, 410, 50, 25);
panel.add(label_message);
text_message = new JTextField(15);
text_message.setBounds(40, 410, 330, 25);
panel.add(text_message);
button_message = new JButton("Send");
button_message.setBounds(400, 410, 70, 25);
panel.add(button_message);
button_message.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
/*
* utworzenie gniazda Servera i oczekiwanie na połączonie.
*/
ServerSocket ss = new ServerSocket(port);
Socket incoming = ss.accept();
try
{
textArea.setText("Wszystko OK");
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream,true);
String line = text_message.getText();
textArea.setText(textArea.getText() + "\n" + line);
out.print("Wszystko OK");
}
finally
{
incoming.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
textArea = new JTextArea(200,200);
JScrollPane scroll = new JScrollPane(textArea);
scroll.setBounds(10, 43, 450, 350);
panel.add(scroll);
add(panel);
}
private JPanel panel;
private JTextArea textArea;
private JButton button_message;
private JButton button_port;
private JLabel label_message;
private JLabel label_port;
private JTextField text_message;
private JTextField text_port;
private JScrollPane scroll;
private int port;
}
Klient:
import javax.swing.JFrame;
public class Chat {
public static void main (String[] args)
{
ChatFrame frame = new ChatFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//**********************************************************************************
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatFrame extends JFrame
{
public ChatFrame()
{
setSize(500,500);
setTitle("Klient");
panel = new JPanel();
panel.setLayout(null);
label_ip = new JLabel("IP serwera :");
label_ip.setBounds(10, 10, 70, 25);
panel.add(label_ip);
text_ip = new JTextField(15);
text_ip.setBounds(90, 10, 110, 25);
text_ip.setText("127.0.0.1");
panel.add(text_ip);
button_ip = new JButton("Set");
button_ip.setBounds(210, 10, 60, 25);
button_ip.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
server_ip = text_ip.getText();
}
});
panel.add(button_ip);
label_message = new JLabel("Text:");
label_message.setBounds(10, 410, 50, 25);
panel.add(label_message);
text_message = new JTextField(15);
text_message.setBounds(40, 410, 330, 25);
panel.add(text_message);
button_message = new JButton("Send");
button_message.setBounds(400, 410, 70, 25);
panel.add(button_message);
button_message.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
Socket s = new Socket(server_ip,8189);
try
{
InputStream inStream = s.getInputStream();
Scanner in = new Scanner(inStream);
String line = text_message.getText();
textArea.setText(textArea.getText() + "\n" + line);
}
finally
{
s.close();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
textArea = new JTextArea(200,200);
JScrollPane scroll = new JScrollPane(textArea);
scroll.setBounds(10, 43, 450, 350);
panel.add(scroll);
add(panel);
}
private JPanel panel;
private JTextArea textArea;
private JButton button_message;
private JButton button_ip;
private JLabel label_message;
private JLabel label_ip;
private JTextField text_message;
private JTextField text_ip;
private JScrollPane scroll;
String server_ip;
}
aby nawiazac polaczenie:
opalamy program serwera klikamy Set potem na dole Send
nastepnie odpalamy program klienta i klitamy rowniez Set, Send
problem jest w tym aby komunikaty wpisywanie w polu text pojawiamy sie obu konsolkach programu u klienta i na serwerze :)