import java.awt.*; |
= |
import java.awt.*; |
import java.awt.font.*; |
|
import java.awt.font.*; |
import java.awt.event.*; |
|
import java.awt.event.*; |
import javax.swing.*; |
|
import javax.swing.*; |
import java.io.*; |
|
import java.io.*; |
import java.net.*; |
|
import java.net.*; |
public class ClientFrame { |
<> |
public class byteClientFrame { |
static boolean is_listening=true; |
= |
static boolean is_listening=true; |
static Socket this_socket; |
|
static Socket this_socket; |
static PrintWriter out; |
<> |
static DataOutputStream out; |
static BufferedReader in; |
|
static DataInputStream in; |
static JTextField theMessage; |
= |
static JTextField theMessage; |
public static void main(String args[]) { |
|
public static void main(String args[]) { |
theMessage=new JTextField(); |
|
theMessage=new JTextField(); |
Font the_font = new Font("SansSerif", Font.BOLD, 20); |
|
Font the_font = new Font("SansSerif", Font.BOLD, 20); |
theMessage.setFont(the_font); |
|
theMessage.setFont(the_font); |
try { |
|
try { |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
int ChatPort=Integer.parseInt(args[0]); |
|
int ChatPort=Integer.parseInt(args[0]); |
InetAddress ip = InetAddress.getByName("localhost"); |
|
InetAddress ip = InetAddress.getByName("localhost"); |
try { |
|
try { |
this_socket = new Socket(ip, ChatPort); |
|
this_socket = new Socket(ip, ChatPort); |
out =new PrintWriter(this_socket.getOutputStream(), true); |
<> |
out =new DataOutputStream(this_socket.getOutputStream()); |
in = new BufferedReader(new InputStreamReader(this_socket.getInputStream())); |
|
in = new DataInputStream(this_socket.getInputStream()); |
} catch (Exception e1) { |
= |
} catch (Exception e1) { |
System.out.println("Port "+ChatPort+ " not Available"); |
|
System.out.println("Port "+ChatPort+ " not Available"); |
return; |
|
return; |
} |
|
} |
System.out.println("The Inet address is "+ip+ |
|
System.out.println("The Inet address is "+ip+ |
"\n listening on port "+this_socket.getLocalPort()+ |
|
"\n listening on port "+this_socket.getLocalPort()+ |
"\n sending on port "+this_socket.getPort()+"\n\n"); |
|
"\n sending on port "+this_socket.getPort()+"\n\n"); |
JFrame frame = new JFrame("Chat Frame using Port "+ChatPort); |
|
JFrame frame = new JFrame("Chat Frame using Port "+ChatPort); |
JButton button = new JButton("Ask Chat Server"); |
|
JButton button = new JButton("Ask Chat Server"); |
button.addActionListener((ev)->{send_chat(theMessage);}); |
|
button.addActionListener((ev)->{send_chat(theMessage);}); |
JButton cbutton = new JButton("Close Window"); |
|
JButton cbutton = new JButton("Close Window"); |
cbutton.addActionListener((ev)->{close();}); |
|
cbutton.addActionListener((ev)->{close();}); |
Container contentPane = frame.getContentPane(); |
|
Container contentPane = frame.getContentPane(); |
contentPane.add(theMessage, BorderLayout.CENTER); |
|
contentPane.add(theMessage, BorderLayout.CENTER); |
contentPane.add(button, BorderLayout.SOUTH); |
|
contentPane.add(button, BorderLayout.SOUTH); |
contentPane.add(cbutton, BorderLayout.NORTH); |
|
contentPane.add(cbutton, BorderLayout.NORTH); |
frame.setSize(500, 150); |
|
frame.setSize(500, 150); |
frame.setVisible(true); |
|
frame.setVisible(true); |
|
|
|
frame.addWindowListener(new WindowAdapter() { |
|
frame.addWindowListener(new WindowAdapter() { |
public void windowClosing(WindowEvent e) { |
|
public void windowClosing(WindowEvent e) { |
close(); |
|
close(); |
} |
|
} |
}); |
|
}); |
} catch (Exception e) { |
|
} catch (Exception e) { |
e.printStackTrace(); |
|
e.printStackTrace(); |
} |
|
} |
connect(theMessage); |
|
connect(theMessage); |
new Listener().start(); |
|
new Listener().start(); |
} |
|
} |
public static void send_chat(JTextField the_message){ |
|
public static void send_chat(JTextField the_message){ |
ClientFrame.out.println(the_message.getText()); |
<> |
try { |
} |
|
byte[] the_bytes=the_message.getText().getBytes("UTF8"); |
public static void close(){ |
|
byteClientFrame.out.writeInt(the_bytes.length); // write length of the message |
try { |
|
byteClientFrame.out.write(the_bytes); // write the message |
ClientFrame.out.println("shut_down123"); |
|
// byteClientFrame.out.println(the_message.getText()); |
is_listening=false; |
|
} catch (Exception ioe) { |
in.close(); |
|
System.out.println("Socket did not send_chat."); |
out.close(); |
|
} |
this_socket.close(); |
|
} |
System.out.println("Client Closing"); |
|
public static void close(){ |
System.exit(0); |
|
try { |
} catch (Exception ioe) { |
|
String shut="shut_down123"; |
System.out.println("Socket did not close properly."); |
|
byte[] the_bytes=shut.getBytes("UTF8"); |
} |
|
byteClientFrame.out.writeInt(the_bytes.length); // write length of the message |
} |
|
byteClientFrame.out.write(the_bytes); // write the message |
public static void connect(JTextField the_message){ |
|
is_listening=false; |
ClientFrame.out.println("start_up123"); |
|
in.close(); |
String the_response=""; |
|
out.close(); |
try { |
|
this_socket.close(); |
the_response=ClientFrame.in.readLine(); |
|
System.out.println("Client Closing"); |
the_message.setText(the_response); |
|
System.exit(0); |
the_message.repaint(); |
|
} catch (Exception ioe) { |
} catch (Exception e) { |
|
System.out.println("Socket did not close properly."); |
e.printStackTrace(); |
|
} |
} |
|
} |
} |
|
public static void connect(JTextField the_message){ |
} |
|
try { |
|
|
String start="start_up123"; |
class Listener extends Thread { |
|
byte[] the_bytes=start.getBytes("UTF8"); |
public Listener( ) {} |
|
byteClientFrame.out.writeInt(the_bytes.length); // write length of the message |
@Override |
|
byteClientFrame.out.write(the_bytes); // write the message |
public void run() { |
|
String the_response=""; |
String the_response=""; |
|
int length = byteClientFrame.in.readInt(); // read length of incoming message |
while (ClientFrame.is_listening) { |
|
the_bytes = new byte[length]; |
try { |
|
byteClientFrame.in.readFully(the_bytes, 0, length); // read the message |
the_response=ClientFrame.in.readLine(); |
|
the_response = new String(the_bytes,"UTF8"); |
ClientFrame.theMessage.setText(the_response); |
|
theMessage.setText(the_response); |
ClientFrame.theMessage.repaint(); |
|
theMessage.repaint(); |
} catch (Exception e) { |
|
} catch (Exception e) { |
e.printStackTrace(); |
|
e.printStackTrace(); |
System.out.println("Exception"); |
|
} |
} |
|
} |
} |
|
} |
|
= |
|
} |
<> |
class Listener extends Thread { |
} |
|
public Listener( ) {} |
|
|
@Override |
|
|
public void run() { |
|
|
String the_response=""; |
|
|
while (byteClientFrame.is_listening) { |
|
|
try { |
|
|
int length = byteClientFrame.in.readInt(); // read length of incoming message |
|
|
byte[] the_bytes = new byte[length]; |
|
|
byteClientFrame.in.readFully(the_bytes, 0, length); // read the message |
|
|
the_response = new String(the_bytes,"UTF8"); |
|
|
byteClientFrame.theMessage.setText(the_response); |
|
|
byteClientFrame.theMessage.repaint(); |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
System.out.println("Exception"); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|