-
Notifications
You must be signed in to change notification settings - Fork 13
/
SocketClient.java
109 lines (90 loc) · 3.77 KB
/
SocketClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
@SuppressWarnings("serial")
public class SocketClient extends JFrame implements ActionListener, Runnable {
JTextArea textArea = new JTextArea();
JScrollPane jp = new JScrollPane(textArea);
JTextField input_Text = new JTextField();
JMenuBar menuBar = new JMenuBar();
Socket sk;
BufferedReader br;
PrintWriter pw;
public SocketClient() {
super("Joto Iccha Chat Koro");
setFont(new Font("Arial Black", Font.PLAIN, 12));
setForeground(new Color(0, 0, 51));
setBackground(new Color(51, 0, 0));
textArea.setToolTipText("Chat History");
textArea.setForeground(new Color(50, 205, 50));
textArea.setEditable(false);
textArea.setFont(new Font("Monospaced", Font.BOLD, 13));
textArea.setBackground(new Color(0, 0, 0));
/*
JMenu helpMenu = new JMenu("Help");
JMenuItem update = new JMenuItem("Update Information");
JMenuItem connect_List = new JMenuItem("Visitor List");
helpMenu.add(update);
helpMenu.add(connect_List);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
*/
getContentPane().add(jp, "Center");
input_Text.setText("Enter your Message:");
input_Text.setToolTipText("Enter your Message");
input_Text.setForeground(new Color(0, 0, 0));
input_Text.setFont(new Font("Tahoma", Font.BOLD, 11));
input_Text.setBackground(new Color(230, 230, 250));
getContentPane().add(input_Text, "South");
setSize(325, 411);
setVisible(true);
input_Text.requestFocus(); //Place cursor at run time, work after screen is shown
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
input_Text.addActionListener(this); //Event registration
}
public void serverConnection() {
try {
String IP = JOptionPane.showInputDialog(this, "Please enter a server IP.", JOptionPane.INFORMATION_MESSAGE);
sk = new Socket(IP, 1234);
String name = JOptionPane.showInputDialog(this, "Please enter a nickname", JOptionPane.INFORMATION_MESSAGE);
/* while (name.length() > 7) {
name = JOptionPane.showInputDialog(this, "Please enter a nickname.(7 characters or less)", JOptionPane.INFORMATION_MESSAGE);
}
*/
//read
br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
//writing
pw = new PrintWriter(sk.getOutputStream(), true);
pw.println(name); // Send to server side
new Thread(this).start();
} catch (Exception e) {
System.out.println(e + " Socket Connection error");
}
}
public static void main(String[] args) {
new SocketClient().serverConnection(); //Method call at the same time object creation
}
@Override
public void run() {
String data = null;
try {
while ((data = br.readLine()) != null) {
textArea.append(data + "\n"); //textArea Decrease the position of the box's scroll bar by the length of the text entered
textArea.setCaretPosition(textArea.getText().length());
}
} catch (Exception e) {
System.out.println(e + "--> Client run fail");
}
}
@Override
public void actionPerformed(ActionEvent e) {
String data = input_Text.getText();
pw.println(data); // Send to server side
input_Text.setText("");
}
}