Witam!
Z pomocą Waszego forum napisałem aplikacje serwera i klienta - prosty czat
Problem w tym, że nie działa do końca tak, jak powinno, gdyż klienci nie komunikują się ze sobą
Jak to rozwiązać?
serwer:
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;
// watek wczytywania
class InThread extends Thread {
Socket BB_socket;
public InThread(Socket BBS) {BB_socket = BBS;}
public void run() {
try {
BufferedReader BR = new BufferedReader(new InputStreamReader(BB_socket.getInputStream()));
PrintStream PS = new PrintStream(BB_socket.getOutputStream());
while (true)
{
String str = BR.readLine();
System.out.println(">> "+str);
PS.println(str);
}
} catch (IOException e) {}
}
}
public class BBServer
{
public static void main(String [] args)
{
BBServer BBS = new BBServer();
try
{
ServerSocket BB_S = new ServerSocket(7777);
Socket BB_socket;
while(true)
{
try
{
while(true) //glowna petla polaczenia
{
System.out.println("Czekam na polaczenie.");
BB_socket = BB_S.accept();
System.out.println("Polaczenie ustanowione!");
// utworz watek klienta
Thread client = new InThread(BB_socket);
client.start();
}
}
catch(SocketException e){System.out.println("klient zerwal polaczenie");System.exit(0);}
catch(IOException e){System.err.println(e);System.exit(0);}
}
}
catch(IOException e){
System.err.println(e);
e.printStackTrace();
System.exit(0);
}
}
}
klient:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;
class BBWindow extends JPanel
{
Socket BB_Socket;
JTextArea JB_output = new JTextArea("output",10,10);
JButton JB_send = new JButton("Wyslij");
JTextField JB_input = new JTextField("input",10);
class MyThread extends Thread {
public void run() {
try {
BufferedReader BR = new BufferedReader(new InputStreamReader(BB_Socket.getInputStream()));
while (true) {
JB_output.append(BR.readLine()+"\n");
}
} catch (IOException ioe) {JB_output.append(ioe.getMessage()+"\n");}
}
}
public JPanel InitPanel()
{
JPanel panel = new JPanel();
JPanel belka = new JPanel();
JScrollPane scrollPane = new JScrollPane (JB_output);
scrollPane.setPreferredSize(new Dimension (400, 300));
JB_output.setWrapStyleWord(true);
JB_output.setLineWrap(true);
JB_output.setEditable(false);
belka.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
panel.add(scrollPane,BorderLayout.CENTER);
belka.add(JB_send,BorderLayout.WEST);
belka.add(JB_input,BorderLayout.CENTER);
panel.add(belka,BorderLayout.SOUTH);
// JB_send.setMnemonic(KeyEvent.VK_ENTER);
JB_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try {
PrintStream PS = new PrintStream(BB_Socket.getOutputStream());
if (JB_input.getText().length()>0) PS.println(JB_input.getText());
JB_input.setText("");
} catch (IOException ioe) {JB_output.append(ioe.getMessage()+"\n");}
}
});
JB_input.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == 10)
{
try {
PrintStream PS = new PrintStream(BB_Socket.getOutputStream());
if (JB_input.getText().length()>0) PS.println(JB_input.getText());
JB_input.setText("");
} catch (IOException ioe) {JB_output.append(ioe.getMessage()+"\n");}
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
});
return panel;
}
public BBWindow(Socket BBS,int BBX,int BBY)
{
BB_Socket = BBS;
// JFrame.setDefaultLookAndFeelDecorated(true);
JFrame okno = new JFrame("BB gadu");
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// dodajemy panel
okno.getContentPane().add(InitPanel(), BorderLayout.CENTER);
//Wyswietl
okno.setSize(BBX, BBY);
okno.setVisible(true);
Thread server = new MyThread();
server.start();
}
}
public class BBClient
{
public static void main (String[] args)
{
Socket BB_socket;
String BB_host = "localhost";
String BB_KStream;
if (args.length>0) BB_host=args[0];
try
{
BB_socket = new Socket(BB_host,7777);
BBWindow BB_panel = new BBWindow(BB_socket,500,300);
}
catch(Exception e){
System.err.println(e);
e.printStackTrace();
System.exit(0);
}
// finally {BB_socket.close();}
}
}
Liczę na Waszą pomoc [soczek]
// spacje i taby mi sie pomieszały <-- efekt kopiowania ;)