Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
lab10server
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
9731088
lab10server
Commits
7534414b
Commit
7534414b
authored
May 26, 2019
by
Roham
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lab10 new
parent
2baf75d8
Pipeline
#666
failed with stages
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
0 deletions
+81
-0
ClientHandler.java
src/ClientHandler.java
+32
-0
Main.java
src/Main.java
+12
-0
Server.java
src/Server.java
+37
-0
No files found.
src/ClientHandler.java
0 → 100644
View file @
7534414b
import
java.io.DataInputStream
;
import
java.io.DataOutputStream
;
import
java.io.IOException
;
import
java.net.Socket
;
public
class
ClientHandler
{
private
Socket
socket
;
private
DataOutputStream
dataOutputStream
;
private
DataInputStream
dataInputStream
;
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
();
}
private
void
handleIncomingMessages
(
String
message
)
{
System
.
out
.
println
(
message
);
}
public
void
run
()
{
try
{
String
input
=
dataInputStream
.
readUTF
();
handleIncomingMessages
(
input
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/Main.java
0 → 100644
View file @
7534414b
import
java.io.IOException
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
try
{
Thread
t
=
new
Server
(
1234
);
t
.
start
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
src/Server.java
0 → 100644
View file @
7534414b
import
java.io.IOException
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.util.ArrayList
;
import
java.net.*
;
import
java.io.*
;
public
class
Server
extends
Thread
{
private
ServerSocket
serverSocket
;
public
Server
(
int
port
)
throws
IOException
{
serverSocket
=
new
ServerSocket
(
port
);
serverSocket
.
setSoTimeout
(
10000
);
}
public
void
run
()
{
while
(
true
)
{
try
{
System
.
out
.
println
(
"Waiting for client on port "
+
serverSocket
.
getLocalPort
()
+
"..."
);
Socket
client
=
serverSocket
.
accept
();
ClientHandler
clientHandler
=
new
ClientHandler
(
client
);
client
.
close
();
}
catch
(
SocketTimeoutException
s
)
{
System
.
out
.
println
(
"Socket timed out!"
);
break
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
break
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
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