Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SERVER:
- import java.io.*;
- import java.net.*;
- public class Server {
- public static void main(String[] args) {
- try {
- while (true) {
- ServerSocket sock = new ServerSocket(6013);
- // now listen for connections
- Socket server = sock.accept();
- InputStream in = server.getInputStream();
- BufferedReader bin = new
- BufferedReader(new InputStreamReader(in));
- String line;
- while ( (line = bin.readLine()) != null)
- System.out.println("Server recieved: " + line);
- //PrintWriter pout = new
- //PrintWriter(client.getOutputStream(), true);
- // write the Date to the socket
- //pin.println(new java.util.Date().toString());
- //pout.
- //System.out.println("Server recieved: " + client.getOutputStream().);
- // close the socket and resume
- // listening for connections
- sock.close();
- }
- }
- catch (IOException ioe) {
- System.err.println(ioe);
- }
- }
- }
- ---------------------------------------------------------------------------------------------
- CLIENT:
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.net.*;
- import java.io.*;
- public class GUI_Client {
- //Variable Definitions for the class
- public static JFrame frame;
- public static GUI_Client Gui;
- public static String message;
- //Main method will be run once the program starts. This main calls makeFrame.
- public static void main(String[] args) {
- //GUI_Client Gui = new GUI_Client();
- makeFrame();
- }
- //makeFrame gets called by the main. This method builds te GUI.
- public static void makeFrame() {
- frame = new JFrame("Client");
- Container contentPane = frame.getContentPane();
- GridLayout gridLayout = new GridLayout(1,2);
- contentPane.setLayout(gridLayout);
- final JTextField JTmessage = new JTextField("Message");
- JTmessage.setEditable(true);
- contentPane.add(JTmessage);
- JButton sendMessage = new JButton("Send Message");
- contentPane.add(sendMessage);
- sendMessage.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- String message = JTmessage.getText();
- try {
- //make connection to server socket
- Socket client = new Socket("127.0.0.1",6013);
- PrintWriter pout = new
- PrintWriter(client.getOutputStream(), true);
- // read the date from the socket
- String line = message;
- pout.println(line);
- // close the socket connection
- client.close();
- }
- catch (IOException ioe) {
- System.err.println(ioe);
- }
- }
- });
- frame.pack();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement