Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
project-Lab11
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Amirhosein Rajabpour
project-Lab11
Commits
7f22e073
Commit
7f22e073
authored
May 27, 2019
by
Amirhosein Rajabpour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
network class modified but there are still some errors
parent
ac02d338
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
18 deletions
+45
-18
Network.java
src/chatRoom/client/Network.java
+16
-6
MessageArea.java
src/chatRoom/client/view/MessageArea.java
+12
-0
ClientHandler.java
src/chatRoom/server/ClientHandler.java
+17
-12
No files found.
src/chatRoom/client/Network.java
View file @
7f22e073
...
...
@@ -7,15 +7,18 @@ import java.util.Scanner;
public
class
Network
{
public
static
void
main
(
String
[]
args
)
{
String
serverName
=
args
[
0
];
int
port
=
Integer
.
parseInt
(
args
[
1
]);
try
(
Socket
client
=
new
Socket
(
serverName
,
port
);
private
static
final
int
port
=
4321
;
private
static
final
String
host
=
"127.0.0.1"
;
Scanner
socketin
;
Formatter
socketOut
;
public
Network
()
throws
IOException
{
try
(
Socket
client
=
new
Socket
(
host
,
port
);
Scanner
socketin
=
new
Scanner
(
client
.
getInputStream
());
Formatter
socketOut
=
new
Formatter
(
client
.
getOutputStream
());)
{
String
text
;
socketOut
.
format
(
text
);
socketOut
.
flush
();
}
catch
(
UnknownHostException
e
)
{
e
.
printStackTrace
();
...
...
@@ -23,4 +26,11 @@ public class Network {
e
.
printStackTrace
();
}
}
public
void
sendMessage
(
String
text
){
socketOut
.
format
(
text
);
socketOut
.
flush
();
}
}
src/chatRoom/client/view/MessageArea.java
View file @
7f22e073
package
chatRoom
.
client
.
view
;
import
chatRoom.client.Network
;
import
javax.swing.*
;
import
java.awt.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.io.IOException
;
public
class
MessageArea
extends
JPanel
{
JTextField
textField
;
JButton
btn
;
ChatRoomGUI
chatRoomGUI
;
Network
network
;
public
MessageArea
(
ChatRoomGUI
chatRoomGUI
){
...
...
@@ -25,6 +29,13 @@ public class MessageArea extends JPanel {
doClickAction
();
}
});
add
(
btn
,
BorderLayout
.
EAST
);
try
{
network
=
new
Network
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
this
.
setVisible
(
true
);
}
void
reset
()
{
...
...
@@ -35,6 +46,7 @@ public class MessageArea extends JPanel {
void
doClickAction
(){
chatRoomGUI
.
addNewMessage
(
textField
.
getText
());
network
.
sendMessage
(
textField
.
getText
());
reset
();
}
}
src/chatRoom/server/ClientHandler.java
View file @
7f22e073
...
...
@@ -3,20 +3,30 @@ import java.io.DataInputStream;
import
java.io.DataOutputStream
;
import
java.io.IOException
;
import
java.net.Socket
;
import
java.util.Formatter
;
import
java.util.Scanner
;
public
class
ClientHandler
{
private
Socket
socket
;
private
DataOutputStream
dataOutputStream
;
private
DataInputStream
dataInputStream
;
// private DataOutputStream dataOutputStream;
// private DataInputStream dataInputStream;
Scanner
in
;
Formatter
out
;
public
ClientHandler
(
Socket
client
)
throws
Exception
{
if
(
client
==
null
)
throw
new
Exception
(
"client can't be null"
);
this
.
socket
=
client
;
dataOutputStream
=
new
DataOutputStream
(
client
.
getOutputStream
());
dataInputStream
=
new
DataInputStream
(
client
.
getInputStream
());
run
();
// dataOutputStream = new DataOutputStream(client.getOutputStream());
// dataInputStream = new DataInputStream(client.getInputStream());
try
(
Scanner
in
=
new
Scanner
(
client
.
getInputStream
());
Formatter
out
=
new
Formatter
(
client
.
getOutputStream
());){
run
();
}
}
private
void
handleIncomingMessages
(
String
message
){
System
.
out
.
println
(
message
);
...
...
@@ -24,13 +34,8 @@ public class ClientHandler {
public
void
run
()
{
while
(
true
){
try
{
String
input
=
dataInputStream
.
readUTF
();
handleIncomingMessages
(
input
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
String
input
=
in
.
next
()
;
handleIncomingMessages
(
input
);
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment