Skip to content

Instantly share code, notes, and snippets.

/Client Secret
Created Aug 3, 2014

Embed
What would you like to do?
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client{
//socket,stream
private Socket socket;
private ObjectInputStream is;
private ObjectOutputStream os;
private String host;
private Boolean connected;
public Client(){
connected=false;
}
public void connect(String host)throws Exception{
this.host=host;
this.connected=true;
//connect to server
socket=new Socket(InetAddress.getByName(this.host),6789);
//setup stream
os=new ObjectOutputStream(socket.getOutputStream());
os.flush();
is=new ObjectInputStream(socket.getInputStream());
}
public void disconnect(){
connected=false;
}
public boolean is_connected(){
if (connected){
return true;
}
return false;
}
public ObjectOutputStream get_OS(){
return this.os;
}
public ObjectInputStream get_IS(){
return this.is;
}
}
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Client_GUI extends JFrame{
private JButton connect_bt;
private JLabel num_of_connection;
private JTextField client_text;
private JTextArea chat_area;
private JPanel content;
private JPanel top_content;
private JPanel text_content;
private Client client;
private String server_message;
private String client_message;
public static void main(String[] args) {
Client_GUI GUI=new Client_GUI();
GUI.start();
}
public Client_GUI(){
super("client_side");
}
public void start(){
client=new Client();
set_GUI();
}
private void set_GUI(){
content=new JPanel();
content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
this.add(content);
top_content=new JPanel();
top_content.setLayout(new FlowLayout(FlowLayout.LEFT));
connect_bt=new JButton("connect");
num_of_connection=new JLabel("X connections established");
top_content.add(connect_bt);
top_content.add(num_of_connection);
text_content=new JPanel();
text_content.setLayout(new BoxLayout(text_content,BoxLayout.Y_AXIS));
client_text=new JTextField();
client_text.setMaximumSize(new Dimension(400,30));
chat_area=new JTextArea();
text_content.add(client_text);
text_content.add(chat_area);
client_text.setEditable(false);
chat_area.setEditable(false);
content.add(top_content);
content.add(text_content);
this.setSize(500,350);
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setListeners();
}
private void setListeners(){
//enter pressed on textField
client_text.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent evt){
System.out.println("message sent");
client_text.setText("");
client_message=(evt.getActionCommand()+"\n");
try {
client.get_OS().writeObject(client_message);
client.get_OS().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
connect_bt.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent evt){
if(!client.is_connected()){
//connect to server method
try{
client.connect("127.0.0.1");
showMessage("connected to server\n");
connect_bt.setText("disconnect");
client_text.setEditable(true);
//non-blocking fct. swingWorker inside.
whileConnected();
}catch(Exception exception){
System.out.println("unable to connect");
}
}else{
//disconnect from server method
client.disconnect();
connect_bt.setText("connect");
client_text.setText("");
client_text.setEditable(false);
}
}
}
);
}
private void whileConnected(){
//swingUtilities thread
SwingWorker<Void,Void> worker=new SwingWorker<Void,Void>(){
protected Void doInBackground() throws Exception {
int num=0;
while(client.is_connected()){
System.out.println("plus "+num++);
//looking for inputStream
server_message=(String)client.get_IS().readObject();
System.out.println("server sent "+server_message);
//if message received, pass work to event thread
showMessage(server_message);
}
//close all connections
return null;
}
};
worker.execute();
}
private void showMessage(final String message){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
chat_area.append(message);
}
});
}
}
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class ClientManager implements Runnable {
private Socket socket;
private ObjectInputStream is;
private ObjectOutputStream os;
private String message;
public ClientManager(Socket sock){
socket=sock;
try {
setupStream();
} catch (Exception e) {
System.out.println("cant connect stream");
e.printStackTrace();
}
}
private void setupStream()throws Exception{
is=new ObjectInputStream(socket.getInputStream());
os=new ObjectOutputStream(socket.getOutputStream());
os.flush();
}
public void run() {
while(socket.isConnected()){
try {
message=(String)is.readObject();
System.out.println("message received is "+message);
//envoyer le message à tous les clients
for(Socket sock : Server.client_sock_list){
ObjectOutputStream tmp_os=new ObjectOutputStream(sock.getOutputStream());
System.out.println(tmp_os.toString());
tmp_os.writeObject("Anonymous - "+message);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//close connections and remove from arrayList
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private ServerSocket server_socket;
private Boolean connected;
public static ArrayList<Socket> client_sock_list;
public static void main(String[] args){
Server server=new Server();
System.out.println("server started");
server.runServer();
}
public Server(){
client_sock_list=new ArrayList<Socket>();
}
private void runServer(){
//new serverSocket to listen to any port
try {
server_socket=new ServerSocket(6789);
connected=true;
//listen for connections. blocking cmd.
while(connected){
Socket client_socket=server_socket.accept();
System.out.println("new client connected");
//put nw client in arrayList
client_sock_list.add(client_socket);
//start new thread to manage client every new connection
new Thread(new ClientManager(client_socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
//close all connections and stop server
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.