-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerapp.java
More file actions
73 lines (66 loc) · 1.35 KB
/
Serverapp.java
File metadata and controls
73 lines (66 loc) · 1.35 KB
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
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class Serverapp implements ActionListener,Runnable {
JFrame jf;
JButton jb;
JTextField tf;
JTextArea ta;
BufferedReader br;
PrintWriter pw;
ServerSocket ss;
Socket s;
Container c;
Thread t;
public Serverapp(){
jf = new JFrame("Server");
c = jf.getContentPane();
c.setLayout(new FlowLayout());
try{
ss = new ServerSocket(2000);
s = ss.accept();
System.out.println("Connected");
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(s.getOutputStream(), true);
}
catch(Exception ae){
ae.printStackTrace();
}
jb = new JButton("Send");
ta = new JTextArea(30,15);
tf = new JTextField(15);
c.add(tf);
c.add(jb);
c.add(ta);
jf.setVisible(true);
jf.setSize(400,350);
jb.addActionListener(this);
t = new Thread(this);
t.start();
}
public void actionPerformed(ActionEvent ae){
try {
String msg = tf.getText();
pw.println(msg);
tf.setText("");
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
while(true){
try{
String msg = (String)br.readLine();
ta.append(msg.toString());
}
catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]){
new Serverapp();
}
}