rendered paste bodyimport java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleChatClient {
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String[] args) {
SimpleChatClient scc = new SimpleChatClient();
scc.go();
}
public void go(){
JFrame frame = new JFrame("Dav1d Chat Client");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15,50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(true);
incoming.setText("Welcome to Dav1d Chat Client");
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
outgoing = new JTextField(20);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(600,450);
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 Complete");
}catch(Exception ex){ex.printStackTrace();}
}
public class SendButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
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)
{
//incoming.append("\n" + message);
incoming.setText(incoming.getText() + "\n" + message);
System.out.println("Read " + message);
}
}catch(Exception ex){ex.printStackTrace();}
}
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class SimpleChatServer {
ArrayList clientOutputStreams;
public class ClientHandler implements Runnable
{
BufferedReader br;
Socket sock;
public ClientHandler(Socket cSock)
{
try{
sock = cSock;
InputStreamReader isr = new InputStreamReader(sock.getInputStream());
br = new BufferedReader(isr);
}catch (Exception ex){ex.printStackTrace();}
}
public void run()
{
String message;
try{
while((message=br.readLine())!=null)
{
System.out.println("Read " + message);
sendAll(message);
}
}catch(Exception ex){ex.printStackTrace();}
}
}
public static void main(String[] args) {
new SimpleChatServer().go();
}
public void go(){
clientOutputStreams = new ArrayList();
try{
ServerSocket sSock = new ServerSocket(5000);
while(true)
{
Socket cSock = sSock.accept();
PrintWriter writer = new PrintWriter(cSock.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(cSock));
t.start();
System.out.println("Connection Acquired");
}
}catch(Exception ex){ex.printStackTrace();}
}
public void sendAll(String message){
Iterator it =clientOutputStreams.iterator();
while(it.hasNext());
try{
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
}catch(Exception ex){ex.printStackTrace();}
}
}