Skip to content

Instantly share code, notes, and snippets.

Created November 25, 2014 04:58
Show Gist options
  • Save anonymous/d60cdc036dee66d1e1a4 to your computer and use it in GitHub Desktop.
Save anonymous/d60cdc036dee66d1e1a4 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String serverIP;
private Socket connection;
public Client(String host){
super("Client");
serverIP = host;
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow), BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
}
//connect to server
public void startRunning(){
try{
connectToServer();
setupStreams();
//Ask for files
////Receive IP/Port of file holder
//message File holder asking for password
whileChatting();
}catch(EOFException oefException){
showMessage("\n Client Terminated the connection");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
closeCrap();
}
}
//connect to server
private void connectToServer() throws IOException{
showMessage("Attempting Connection..\n");
connection = new Socket(InetAddress.getByName(serverIP),6789);
showMessage("Connected to: " + connection.getInetAddress().getHostName() );
}
// setup streams
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Your streams are now good to go \n");
}
// while chatting with server
private void whileChatting() throws IOException{
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n I dont know that object");
}
}while(!message.equals("SERVER - END"));
}
private void closeCrap(){
showMessage("\n Closing stuff down");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send message to server
private void sendMessage(String message){
try{
output.writeObject("CLIENT - " + message);
output.flush();
showMessage("\nCLIENT - " + message);
}catch(IOException ioException){
chatWindow.append("\n Something messed up sending message");
}
}
// show message
private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(m);
}
}
);
}
//gives user permission to type
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
}
import javax.swing.JFrame;
public class ClientTest {
public static void main(String[] args){
Client client;
client = new Client("127.0.0.1");
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.startRunning();
}
}
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
//constructor
public Server(){
super("Instant Messenger");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
setSize(300,150);
setVisible(true);
}
//set up and run the server
public void startRunning(){
try{
server = new ServerSocket(6789);
while(true){
try{
waitForConnection();
setupStreams();
//Receive Request for file
//check which client has file
//send ip of client with file
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}finally{
closeCrap();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException{
showMessage("Waiting for someone to connect...\n");
connection = server.accept();
showMessage("Now connected to" + connection.getInetAddress().getHostName());
}
//get stream to send and receive data
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup! \n");
}
// during the chat
private void whileChatting() throws IOException{
String message = "You are now connected! ";
sendMessage(message);
ableToType(true);
do{
//have a conversation
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n I dont know that user sent");
}
}while(!message.equals("CLIENT - END"));
}
//close streams and sockets after you are done chatting
private void closeCrap(){
showMessage("\n Closing connection...\n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send message to client
private void sendMessage(String message){
try{
output.writeObject("Server - " + message);
output.flush();
showMessage("\n SERVER - " + message);
}catch(IOException ioException){
chatWindow.append("\n ERROR");
}
}
//updates chatWindow
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(text);
}
}
);
}
//let the user type stuff into the box
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
}
import javax.swing.JFrame;
public class ServerTest {
public static void main(String[] args){
Server iM = new Server();
iM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
iM.startRunning();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment