Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import javax.swing.JFrame;
- import javax.swing.JTextArea;
- public class Server {
- //GUI Objects:
- private JFrame frame = new JFrame("Server");
- private JTextArea txtArea = new JTextArea();
- //Server Objects:
- private ServerSocket serverSocket;
- private Socket socket;
- public Server() throws IOException {
- //Frame Attributes:
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.setResizable(false);
- frame.setSize(400, 600);
- frame.setLayout(null);
- //txtArea Attributes:
- txtArea.setSize(360, 500);
- txtArea.setLocation(15, 10);
- //Add Objects To Frame:
- frame.add(txtArea);
- //Open Server On Port 7777:
- serverSocket = new ServerSocket(7777);
- //Wait For Clients:
- while(true){
- socket = serverSocket.accept();
- ServerThread sThread = new ServerThread(socket, txtArea);
- sThread.start();
- }
- }
- public static void main(String[] args) throws IOException {
- new Server();
- }
- }
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import javax.swing.JTextArea;
- public class ServerThread extends Thread {
- private Socket socket;
- private JTextArea txt;
- private String message = null;
- private BufferedReader br;
- public ServerThread(Socket newSock, JTextArea newTxt) {
- this.socket = newSock;
- this.txt = newTxt;
- }
- public void run(){
- try {
- br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- message = br.readLine();
- txt.append("Client: " + message + "\n");
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- public class Client {
- //GUI Objects:
- private JFrame frame = new JFrame("Client");
- private JTextField txtMessage = new JTextField();
- //Client Objects:
- private Socket socket;
- private String message;
- private PrintWriter printWriter;
- public Client() {
- //Frame Attributes:
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.setResizable(false);
- frame.setSize(400, 600);
- frame.setLayout(null);
- //txtMessage Attributes:
- txtMessage.setSize(360, 25);
- txtMessage.setLocation(10, 15);
- //txtMessage Event:
- txtMessage.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- try {
- //Open Client On Port 7777:
- socket = new Socket("localhost", 7777);
- printWriter = new PrintWriter(socket.getOutputStream(), true);
- message = txtMessage.getText();
- printWriter.println(message);
- txtMessage.setText("");
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- //Add Objects To Frame:
- frame.add(txtMessage);
- }
- public static void main(String[] args) {
- new Client();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement