import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class chatClient implements ActionListener{
Socket socket;
static JButton sendButton;
static JTextArea messageArea;
static BufferedReader reader;
static PrintWriter writer;
static JTextField messageField;
static JTextField nameField;
static String message ;
public static void main(String[] args) {
chatClient client = new chatClient();
client.go();
setNet net = new setNet();
net.start();
}
public void go() {
JFrame frame = new JFrame();
sendButton = new JButton("Send");
sendButton.addActionListener(this);
messageArea = new JTextArea(10, 10);
nameField = new JTextField(5);
nameField = new JTextField("Me");
messageField = new JTextField(20);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(messageArea);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
mainPanel.add(nameField);
mainPanel.add(scrollPane);
mainPanel.add(messageField);
mainPanel.add(sendButton);
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
nameField.requestFocus();
messageField.setText("Hello world!");
}
public static class setNet extends Thread{
public void run() {
try {
while (true) {
Socket socket = new Socket("127.0.0.1", 5000);
writer = new PrintWriter(socket.getOutputStream());
InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
reader = new BufferedReader(streamReader);
message = reader.readLine();
if (message != "") {
messageArea.append(nameField.getText() + ": " + message + "\n");
System.out.println("reader working");
} else {
messageArea.append("message is null");
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("run catch");
}
}
}
//BUTTON
public void actionPerformed(ActionEvent ev){
try {
writer.println(messageField.getText() + "\r");
System.out.println("writer working");
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("button catch");
}
messageField.setText("");
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by user on 28.07.2017.
*/
public class chatServ {
static PrintWriter writer;
static String msg = "";
public static void main(String[] args) throws IOException {
System.out.println("net is work");
Net net = new Net();
net.start();
}
public static class Net extends Thread{
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(5000);
Socket socket = serverSocket.accept();
while (true) {
System.out.println("while working");
InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
while ((msg = reader.readLine()) != "") {
System.out.println(msg);
System.out.println("reader working");
writer = new PrintWriter(socket.getOutputStream());
writer.println(msg);
System.out.println("writer working");
writer.flush();
writer.close();
}
System.out.println("msg is empty");
}
} catch (IOException e) {
System.out.println("net catch");
} finally {
System.out.println("net is down");
}
}
}
}
public class SendButton implements ActionListener{
public void actionPerformed(ActionEvent ev){
try {
Socket socket = new Socket("127.0.0.1",5000);
writer = new PrintWriter(socket.getOutputStream());
writer.println(messageField.getText());
System.out.println("writer working");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
messageField.setText("");
}
}