Ошибка в приложении чата [закрыто]
Я новичок в Java. Я пытался создать приложение чата, но при запуске хотя бы одного клиента возникает какая-то ошибка. Почему текстовая область и текстовое поле не отображаются. Что я получаю, это происходит из-запринять функциюКогда компилятор достигает функции принятия, приложение становится занятым. то есть на экране приложения ничего не показывается.
Код клиента один:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
public class ChatAppOne{
JFrame jframe;
JTextArea jtextarea;
JTextField jtextfield;
JButton jbutton;
ServerSocket server;
Socket sSocket,cSocket;
InputStream inStream;
ObjectInputStream objInStream;
OutputStream outStream;
ObjectOutputStream objOutStream;
ChatAppOne(){
jframe=new JFrame();
jframe.setLayout(new FlowLayout(FlowLayout.LEFT));
jtextarea=new JTextArea("",28,49);
jtextarea.setEditable(false);
jframe.add(new JScrollPane(jtextarea));
jtextfield=new JTextField();
jtextfield.setPreferredSize(new Dimension(440,30));
jframe.add(jtextfield);
jbutton=new JButton("Send");
jbutton.setPreferredSize(new Dimension(100,35));
jframe.add(jbutton);
jbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
cSocket=new Socket("localhost",1340);
outStream=cSocket.getOutputStream();
objOutStream=new ObjectOutputStream(outStream);
objOutStream.writeObject(jtextfield.getText());
jtextarea.setText(jtextarea.getText() +" \n " +"Me: " +jtextfield.getText());
jtextfield.setText("");
cSocket.close();
outStream.close();
objOutStream.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE);
}
}
});
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(600,600);
jframe.setTitle("Chat Application");
jframe.setVisible(true);
startServer();
}
void startServer(){
try{
server=new ServerSocket(1550);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE);
}
while(true){
try{
sSocket=server.accept();
inStream=sSocket.getInputStream();
objInStream=new ObjectInputStream(inStream);
String msg=(String) objInStream.readObject();
jtextarea.setText("App Two : "+ msg);
sSocket.close();
inStream.close();
objInStream.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ChatAppOne();
}
});
}
}
Клиент Два такой же, как Клиент, за исключением разницы портов на сервере и клиенте.
Код клиента два
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
public class ChatAppTwo{
JFrame jframe;
JTextArea jtextarea;
JTextField jtextfield;
JButton jbutton;
ServerSocket server;
Socket sSocket,cSocket;
InputStream inStream;
ObjectInputStream objInStream;
OutputStream outStream;
ObjectOutputStream objOutStream;
ChatAppTwo(){
jframe=new JFrame();
jframe.setLayout(new FlowLayout(FlowLayout.LEFT));
jtextarea=new JTextArea("",28,49);
jtextarea.setEditable(false);
jframe.add(new JScrollPane(jtextarea));
jtextfield=new JTextField();
jtextfield.setPreferredSize(new Dimension(440,30));
jframe.add(jtextfield);
jbutton=new JButton("Send");
jbutton.setPreferredSize(new Dimension(100,35));
jframe.add(jbutton);
jbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
cSocket=new Socket("localhost",1550);
outStream=cSocket.getOutputStream();
objOutStream=new ObjectOutputStream(outStream);
objOutStream.writeObject(jtextfield.getText());
jtextarea.setText(jtextarea.getText() +" \n " +"Me: " +jtextfield.getText());
jtextfield.setText("");
cSocket.close();
outStream.close();
objOutStream.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE);
}
}
});
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(600,600);
jframe.setTitle("Chat Application");
jframe.setVisible(true);
startServer();
}
void startServer(){
try{
server=new ServerSocket(1340);
JOptionPane.showMessageDialog(null, "Server Started", "Error", JOptionPane.PLAIN_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE);
}
while(true){
try{
sSocket=server.accept();
inStream=sSocket.getInputStream();
objInStream=new ObjectInputStream(inStream);
String msg=(String) objInStream.readObject();
jtextarea.setText("App Two : "+ msg);
sSocket.close();
inStream.close();
objInStream.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e, "Error", JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ChatAppTwo();
}
});
}
}