Repository navigation
Expand file tree
/
Copy pathServerGUI.java
More file actions
153 lines (124 loc) · 3.69 KB
/
Copy pathServerGUI.java
File metadata and controls
153 lines (124 loc) · 3.69 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
////////////////////////////////////////////////////////////////////////
//
// File Name : ServerGUI.java
//
// Description : Listens for incoming client connections and
// handles message communication over a socket.
//
// Author : Riddhi Dattatraya Sonawane
//
// Date : 21/11/2025
//
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//
// Header files inclusion
//
///////////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.Image;
import java.awt.event.*;
///////////////////////////////////////////////////////////////////////
//
// Server side GUI
//
///////////////////////////////////////////////////////////////////////
class Server implements ActionListener
{
//Characteristics
JFrame fobj;
JLabel Lbl1, ResultLabel, imageLabel;
JButton bobj1;
JTextField tobj1;
Socket sobj;
PrintStream pobj;
BufferedReader bobj;
String str1,str2;
static int no;
static
{
no = 200;
}
public Server(String title, int width , int height, Socket sobj)
{
this.sobj = sobj;
try
{
pobj = new PrintStream(sobj.getOutputStream());
bobj = new BufferedReader(new InputStreamReader(sobj.getInputStream()));
}
catch(Exception eobj)
{}
str1 = null;
str2 = null;
fobj = new JFrame(title);
Lbl1 = new JLabel("Message :");
Lbl1.setBounds(50,50,100,30);
tobj1 = new JTextField();
tobj1.setBounds(150,50,100,30);
bobj1 = new JButton("SEND");
bobj1.setBounds(100,100,100,30);
fobj.add(Lbl1);
fobj.add(tobj1);
fobj.add(bobj1);
bobj1.addActionListener(this);
fobj.setLayout(null);
fobj.setSize(width,height);
fobj.setVisible(true);
fobj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent aobj)
{
str2 = tobj1.getText();
pobj.println(str2);
ResultLabel = new JLabel("");
ResultLabel.setBounds(50,(int)no,440,30);
fobj.add(ResultLabel);
if(str2.equals("end"))
{
ResultLabel.setText("You have ended the chat");
return;
}
ResultLabel.setText("Myself : "+ str2);
no = no + 20;
}
public void display() throws IOException
{
while(true)
{
str1 = bobj.readLine();
ResultLabel = new JLabel("");
ResultLabel.setBounds(50,(int)no,440,30);
fobj.add(ResultLabel);
if(str1.equals("end"))
{
ResultLabel.setText("Client has ended the chat");
break;
}
ResultLabel.setText("Client : "+ str1);
no = no + 20;
}
}
}
///////////////////////////////////////////////////////////////////////
//
// Entry point function of project (main)
//
///////////////////////////////////////////////////////////////////////
class ServerGUI
{
public static void main(String A[])
{
try
{
ServerSocket ssobj = new ServerSocket(5100);
Socket sobj = ssobj.accept();
Server mobj = new Server("Server",500,600,sobj);
mobj.display();
}
catch(Exception eobj)
{}
}
}