My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Project Information
Members

import java.io.; import java.net.; import java.util.; import javax.swing.; import java.awt.; import java.awt.event.;

public class SimpleChatClient {

JTextArea incoming; JTextField outgoing; BufferedReader reader; PrintWriter writer; Socket sock;
public static void main(String args) {
SimpleChatClient client=new SimpleChatClient();
client.go();
}
public void go() {
JFrame frame = new JFrame("Simple chat Client"); JPanel mainPanel = new JPanel(); incoming = new JTextArea(15,30); incoming.setLineWrap(true); incoming.setWrapStyleWord(true); incoming.setEditable(false);
JScrollPane aScroller=new JScrollPane(incoming); //aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
outgoing = new JTextField(20);
JButton sendButton =new JButton("send"); sendButton.addActionListener(new SendButtonListener());
mainPanel.add(aScroller); mainPanel.add(outgoing); mainPanel.add(sendButton);
setUpNetworking();
Thread readerThread=new Thread(new IncomingReader()); readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER,mainPanel); frame.setSize(400,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);
}
public void setUpNetworking() {
try{
sock=new Socket("140.114.201.51",5000);//teporary
InputStreamReader streamReader=new InputStreamReader(sock.getInputStream()); reader=new BufferedReader(streamReader); writer=new PrintWriter(sock.getOutputStream()); System.out.println("net working 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 IncomingReader implements Runnable {
public void run() {
String message;
try{
while ((message=reader.readLine())!=null) {
System.out.println("read"+message); incoming.append(message+"\n");
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
}

Powered by Google Project Hosting