Hi,
My java is really bad and I haven't had to use it in a few years but I want to make it better so I've decided to try building it up slowly but at the moment I'm a little stuck and was wondering if anyone could help point me in the right direction.
I'm trying to make a Chat program, similar to msn and yahoo. It would be a client that can send and receive messages between its self on running on two client machines. The machines can either be wired or unwired and the messages sent via a IP local area network.
To start, I'm just working on a basic client-server idea and then to build up to this but I've come to a glitch on the client side (my server side has no bugs but until the client side is bug-free I can't test them).
This is the code, and I'll underline the bits that are showing an error message on my screen. If anybody can help, that would be great, just a little nudge in the right direction. Oh and some of this has been used from a textbook which is why it could be a little dodgy - I know from past experience that the code in textbooks isn't always the greatest.
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BasicChatClient {
JTextArea imcoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String[] args) {
BasicChatClient client = new BasicChatClient();
client.go();
}
public void go() {
JFrame frame = new JFrame("A Basic Chat Client");
JPanel mainPanel = new JPanel();
imcoming = new JTextArea(15,50);
[U]incoming[/U].setLineWrap(true);
[U]incoming[/U].setWrapStyleWord(true);
[U]incoming[/U].setEditable(false);
JScrollPane qScroller = new JScrollPane([U]incoming[/U]);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
JButton sendButton = [U]JButton[/U]("Send");
sendButton.addActionListener(new SendButtonListener());
JButton exitButton = [U]JButton[/U]("Exit");
exitButton.addActionListener(new ExitButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
mainPanel.add(exitButton);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.[U]getContent()[/U].add(BorderLayout.CENTER, mainPanel);
frame.setSize(400,500);
frame.setVisible(true);
}
private void setUpNetworking() {
try {
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
} catch(IOException ex) {
ex.printStackTrace();
}
}
public class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
try {
writer.println(outgoing.getText());
writer.flush();
} catch(Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
public class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
System.exit(0); //exit the program
}
}
public class IncomingReader implements Runnable {
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("read " + message);
imcoming.append(message + "\n");
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}